1
0
mirror of https://github.com/actix/actix-website synced 2024-11-23 16:31:08 +01:00

Static Files is done-ish.

This commit is contained in:
Cameron Dershem 2019-06-26 04:27:25 -04:00
parent 0934b77762
commit 4436eff7de
5 changed files with 58 additions and 60 deletions

View File

@ -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" >}}

View File

@ -1,27 +1,24 @@
// <config-one>
// 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<fs::NamedFile, Error> {
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<NamedFile<MyConfig>> {
// 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();
}
// </config-one>

View File

@ -1,26 +1,18 @@
// <config-two>
// 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();
}
// </config-two>

View File

@ -1,8 +1,14 @@
// <directory>
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();
}
// </directory>

View File

@ -1,17 +1,22 @@
pub mod configuration;
pub mod configuration_two;
pub mod directory;
// <individual-file>
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<NamedFile> {
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();
}
// </individual-file>