mirror of
https://github.com/actix/actix-web.git
synced 2024-11-25 11:01:14 +00:00
docs: add guarded-listings example
This commit is contained in:
parent
ce18f35e03
commit
32ddf972c6
3 changed files with 35 additions and 1 deletions
|
@ -47,4 +47,5 @@ actix-server = { version = "2.2", optional = true } # ensure matching tokio-urin
|
||||||
actix-rt = "2.7"
|
actix-rt = "2.7"
|
||||||
actix-test = "0.1"
|
actix-test = "0.1"
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
|
env_logger = "0.10"
|
||||||
tempfile = "3.2"
|
tempfile = "3.2"
|
||||||
|
|
33
actix-files/examples/guarded-listing.rs
Normal file
33
actix-files/examples/guarded-listing.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
use actix_files::Files;
|
||||||
|
use actix_web::{get, guard, middleware, App, HttpServer, Responder};
|
||||||
|
|
||||||
|
const EXAMPLES_DIR: &str = concat![env!("CARGO_MANIFEST_DIR"), "/examples"];
|
||||||
|
|
||||||
|
#[get("/")]
|
||||||
|
async fn index() -> impl Responder {
|
||||||
|
"Hello world!"
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||||
|
|
||||||
|
log::info!("starting HTTP server at http://localhost:8080");
|
||||||
|
|
||||||
|
HttpServer::new(|| {
|
||||||
|
App::new()
|
||||||
|
.service(index)
|
||||||
|
.service(
|
||||||
|
Files::new("/assets", EXAMPLES_DIR)
|
||||||
|
.show_files_listing()
|
||||||
|
.guard(guard::Header("show-listing", "?1")),
|
||||||
|
)
|
||||||
|
.service(Files::new("/assets", EXAMPLES_DIR))
|
||||||
|
.wrap(middleware::Compress::default())
|
||||||
|
.wrap(middleware::Logger::default())
|
||||||
|
})
|
||||||
|
.bind(("127.0.0.1", 8080))?
|
||||||
|
.workers(2)
|
||||||
|
.run()
|
||||||
|
.await
|
||||||
|
}
|
|
@ -235,7 +235,7 @@ impl Files {
|
||||||
/// request starts being handled by the file service, it will not be able to back-out and try
|
/// request starts being handled by the file service, it will not be able to back-out and try
|
||||||
/// the next service, you will simply get a 404 (or 405) error response.
|
/// the next service, you will simply get a 404 (or 405) error response.
|
||||||
///
|
///
|
||||||
/// To allow `POST` requests to retrieve files, see [`Files::use_guards`].
|
/// To allow `POST` requests to retrieve files, see [`Files::method_guard()`].
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
|
|
Loading…
Reference in a new issue