1
0
mirror of https://github.com/actix/examples synced 2025-02-17 07:23:29 +01:00

Improve the static files example. (#317)

* Improve the static files example.

* Replace the broken websocket example with a simple static HTML page.
* Show how to serve a second directory which is located inside the main
  directory but has file listing enabled.
* Demonstrate that a hierarchy of static files can be served by placing the
  JS and CSS files in their respective subfolders.
* Document how to use the example in the README.

* Improve formatting.
This commit is contained in:
Pieter Frenssen 2020-05-18 01:28:47 +03:00 committed by GitHub
parent 20301845fc
commit bc6f614f78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 55 additions and 96 deletions

View File

@ -1 +1,16 @@
# static_index
Demonstrates how to serve static files. Inside the `./static` folder you will find 2 subfolders:
* `root`: A tree of files that will be served at the web root `/`. This includes the `css` and `js` folders, each
containing an example file.
* `images`: A list of images that will be served at `/images` path, with file listing enabled.
## Usage
```bash
$ cd examples/static_index
$ cargo run
```
This will start the server on port 8080, it can be viewed at [http://localhost:8080](http://localhost:8080).

View File

@ -1,4 +1,4 @@
use actix_files as fs;
use actix_files::Files;
use actix_web::{middleware, App, HttpServer};
#[actix_rt::main]
@ -8,12 +8,15 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
// enable logger
// Enable the logger.
.wrap(middleware::Logger::default())
.service(
// static files
fs::Files::new("/", "./static/").index_file("index.html"),
)
// We allow the visitor to see an index of the images at `/images`.
.service(Files::new("/images", "static/images/").show_files_listing())
// Serve a tree of static files at the web root and specify the index file.
// Note that the root path should always be defined as the last item. The paths are
// resolved in the order they are defined. If this would be placed before the `/images`
// path then the service for the static images would never be reached.
.service(Files::new("/", "./static/root/").index_file("index.html"))
})
.bind("127.0.0.1:8080")?
.run()

View File

Before

Width:  |  Height:  |  Size: 8.7 KiB

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -1,90 +0,0 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script language="javascript" type="text/javascript">
$(function() {
var conn = null;
function log(msg) {
var control = $('#log');
control.html(control.html() + msg + '<br/>');
control.scrollTop(control.scrollTop() + 1000);
}
function connect() {
disconnect();
var wsUri = (window.location.protocol=='https:'&&'wss://'||'ws://')+window.location.host + '/ws/';
conn = new WebSocket(wsUri);
log('Connecting...');
conn.onopen = function() {
log('Connected.');
update_ui();
};
conn.onmessage = function(e) {
log('Received: ' + e.data);
};
conn.onclose = function() {
log('Disconnected.');
conn = null;
update_ui();
};
}
function disconnect() {
if (conn != null) {
log('Disconnecting...');
conn.close();
conn = null;
update_ui();
}
}
function update_ui() {
var msg = '';
if (conn == null) {
$('#status').text('disconnected');
$('#connect').html('Connect');
} else {
$('#status').text('connected (' + conn.protocol + ')');
$('#connect').html('Disconnect');
}
}
$('#connect').click(function() {
if (conn == null) {
connect();
} else {
disconnect();
}
update_ui();
return false;
});
$('#send').click(function() {
var text = $('#text').val();
log('Sending: ' + text);
conn.send(text);
$('#text').val('').focus();
return false;
});
$('#text').keyup(function(e) {
if (e.keyCode === 13) {
$('#send').click();
return false;
}
});
});
</script>
</head>
<body>
<h3>Chat!</h3>
<div>
<button id="connect">Connect</button>&nbsp;|&nbsp;Status:
<span id="status">disconnected</span>
</div>
<div id="log"
style="width:20em;height:15em;overflow:auto;border:1px solid black">
</div>
<form id="chatform" onsubmit="return false;">
<input id="text" type="text" />
<input id="send" type="button" value="Send" />
</form>
</body>
</html>

View File

@ -0,0 +1,11 @@
body {
text-align: center;
}
h1 {
font-family: sans-serif;
}
img {
transition: .5s ease-in-out;
}

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<html>
<head>
<link rel="stylesheet" href="css/example.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="/js/example.js"></script>
</head>
<body>
<h1>Actix-web</h1>
<img src="/images/logo.png" alt="Actix logo" />
</body>
</html>

View File

@ -0,0 +1,7 @@
jQuery(document).ready(function () {
let rotation = 0;
jQuery("img").click(function () {
rotation += 360;
jQuery("img").css({'transform': 'rotate(' + rotation + 'deg)'});
});
});