mirror of
https://github.com/actix/examples
synced 2025-06-29 02:10:36 +02: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:
@ -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()
|
||||
|
Reference in New Issue
Block a user