From 4436eff7def08ccc946ad838aee63cd8b1d67138 Mon Sep 17 00:00:00 2001 From: Cameron Dershem Date: Wed, 26 Jun 2019 04:27:25 -0400 Subject: [PATCH] Static Files is done-ish. --- content/docs/static-files.md | 16 +++---- examples/static-files/src/configuration.rs | 43 +++++++++---------- .../static-files/src/configuration_two.rs | 38 +++++++--------- examples/static-files/src/directory.rs | 10 ++++- examples/static-files/src/main.rs | 11 +++-- 5 files changed, 58 insertions(+), 60 deletions(-) diff --git a/content/docs/static-files.md b/content/docs/static-files.md index 2038286..f3512d5 100644 --- a/content/docs/static-files.md +++ b/content/docs/static-files.md @@ -29,20 +29,18 @@ index file. Use the [*Files::index_file()*][indexfile] method to configure this # Configuration -Generic trait `StaticFileConfig` can be used to specify various options -for serving files: +`NamedFiles` can specify various options for serving files: -- `content_disposition_map` - function to be used for mapping file's mime to corresponding `Content-Disposition` type -- `is_use_etag` - specifies whether `ETag` shall be calculated and included in headers. -- `is_use_last_modifier` - specifies whether file modified timestamp should be used and added to `Last-Modified` header. -- `is_method_allowed` - allows to control which HTTP methods are allowed to be used when accessing file. +- `set_content_dispostion` - function to be used for mapping file's mime to corresponding `Content-Disposition` type +- `use_etag` - specifies whether `ETag` shall be calculated and included in headers. +- `use_last_modifier` - specifies whether file modified timestamp should be used and added to `Last-Modified` header. -All of the above methods are optional and provided with the best defaults. -But it is possible to customize any of them by implementing the trait onto own struct. +All of the above methods are optional and provided with the best defaults, But it is +possible to customize any of them. {{< include-example example="static-files" file="configuration.rs" section="config-one" >}} -The Configuration cal also be applied to directory service: +The Configuration can also be applied to directory service: {{< include-example example="static-files" file="configuration_two.rs" section="config-two" >}} diff --git a/examples/static-files/src/configuration.rs b/examples/static-files/src/configuration.rs index b608e0c..54b5d2a 100644 --- a/examples/static-files/src/configuration.rs +++ b/examples/static-files/src/configuration.rs @@ -1,27 +1,24 @@ // -// extern crate actix_web; -// extern crate mime; -// use actix_files::{FileConfig, NamedFile}; -// use actix_web::http::header::DispositionType; -// use actix_web::{http::Method, App, HttpRequest, Result}; +use actix_files as fs; +use actix_web::http::header::{ContentDisposition, DispositionType}; +use actix_web::{web, App, Error, HttpRequest, HttpServer}; -// use std::path::PathBuf; +fn index(req: HttpRequest) -> Result { + let path: std::path::PathBuf = req.match_info().query("filename").parse().unwrap(); + let file = fs::NamedFile::open(path)?; + Ok(file + .use_last_modified(true) + .set_content_disposition(ContentDisposition { + disposition: DispositionType::Attachment, + parameters: vec![], + })) +} -// #[derive(Default)] -// struct MyConfig; - -// impl FileConfig for MyConfig { -// fn content_disposition_map(typ: mime::Name) -> DispositionType { -// DispositionType::Attachment -// } -// } - -// fn index(req: &HttpRequest) -> Result> { -// let path: PathBuf = req.match_info().query("tail")?; -// Ok(NamedFile::open_with_config(path, MyConfig)?) -// } - -// fn main() { -// App::new().resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index)); -// } +pub fn main() { + HttpServer::new(|| App::new().route("/{filename:.*}", web::get().to(index))) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); +} // diff --git a/examples/static-files/src/configuration_two.rs b/examples/static-files/src/configuration_two.rs index c9c0850..669e625 100644 --- a/examples/static-files/src/configuration_two.rs +++ b/examples/static-files/src/configuration_two.rs @@ -1,26 +1,18 @@ // -// use actix_files::{FileConfig, Files}; -// use actix_web::App; +use actix_files as fs; +use actix_web::{App, HttpServer}; -// #[derive(Default)] -// struct MyConfig; - -// impl FileConfig for MyConfig { -// fn is_use_etag() -> bool { -// false -// } - -// fn is_use_last_modifier() -> bool { -// false -// } -// } - -// fn main() { -// App::new().service( -// "/static", -// Files::with_config(".", MyConfig) -// .unwrap() -// .show_files_listing(), -// ); -// } +pub fn main() { + HttpServer::new(|| { + App::new().service( + fs::Files::new("/static", ".") + .show_files_listing() + .use_last_modified(true), + ) + }) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); +} // diff --git a/examples/static-files/src/directory.rs b/examples/static-files/src/directory.rs index 3fcbbe4..6da7ea4 100644 --- a/examples/static-files/src/directory.rs +++ b/examples/static-files/src/directory.rs @@ -1,8 +1,14 @@ // use actix_files as fs; -use actix_web::App; +use actix_web::{App, HttpServer}; pub fn main() { - App::new().service(fs::Files::new("/static", ".").show_files_listing()); + HttpServer::new(|| { + App::new().service(fs::Files::new("/static", ".").show_files_listing()) + }) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); } // diff --git a/examples/static-files/src/main.rs b/examples/static-files/src/main.rs index 907afff..1e06d23 100644 --- a/examples/static-files/src/main.rs +++ b/examples/static-files/src/main.rs @@ -1,17 +1,22 @@ pub mod configuration; pub mod configuration_two; pub mod directory; + // use actix_files::NamedFile; -use actix_web::{web, App, HttpRequest, Result}; +use actix_web::{web, App, HttpRequest, HttpServer, Result}; use std::path::PathBuf; fn index(req: HttpRequest) -> Result { - let path: PathBuf = req.match_info().query("tail").parse().unwrap(); + let path: PathBuf = req.match_info().query("filename").parse().unwrap(); Ok(NamedFile::open(path)?) } fn main() { - App::new().route("/", web::get().to(index)); + HttpServer::new(|| App::new().route("/{filename:.*}", web::get().to(index))) + .bind("127.0.0.1:8088") + .unwrap() + .run() + .unwrap(); } //