mirror of
https://github.com/actix/actix-website
synced 2024-11-24 00:41:07 +01:00
Static Files is done-ish.
This commit is contained in:
parent
0934b77762
commit
4436eff7de
@ -29,20 +29,18 @@ index file. Use the [*Files::index_file()*][indexfile] method to configure this
|
|||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
|
|
||||||
Generic trait `StaticFileConfig` can be used to specify various options
|
`NamedFiles` can specify various options for serving files:
|
||||||
for serving files:
|
|
||||||
|
|
||||||
- `content_disposition_map` - function to be used for mapping file's mime to corresponding `Content-Disposition` type
|
- `set_content_dispostion` - 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.
|
- `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.
|
- `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.
|
|
||||||
|
|
||||||
All of the above methods are optional and provided with the best defaults.
|
All of the above methods are optional and provided with the best defaults, But it is
|
||||||
But it is possible to customize any of them by implementing the trait onto own struct.
|
possible to customize any of them.
|
||||||
|
|
||||||
{{< include-example example="static-files" file="configuration.rs" section="config-one" >}}
|
{{< 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" >}}
|
{{< include-example example="static-files" file="configuration_two.rs" section="config-two" >}}
|
||||||
|
|
||||||
|
@ -1,27 +1,24 @@
|
|||||||
// <config-one>
|
// <config-one>
|
||||||
// extern crate actix_web;
|
use actix_files as fs;
|
||||||
// extern crate mime;
|
use actix_web::http::header::{ContentDisposition, DispositionType};
|
||||||
// use actix_files::{FileConfig, NamedFile};
|
use actix_web::{web, App, Error, HttpRequest, HttpServer};
|
||||||
// use actix_web::http::header::DispositionType;
|
|
||||||
// use actix_web::{http::Method, App, HttpRequest, Result};
|
|
||||||
|
|
||||||
// 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)]
|
pub fn main() {
|
||||||
// struct MyConfig;
|
HttpServer::new(|| App::new().route("/{filename:.*}", web::get().to(index)))
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
// impl FileConfig for MyConfig {
|
.unwrap()
|
||||||
// fn content_disposition_map(typ: mime::Name) -> DispositionType {
|
.run()
|
||||||
// DispositionType::Attachment
|
.unwrap();
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
// }
|
|
||||||
// </config-one>
|
// </config-one>
|
||||||
|
@ -1,26 +1,18 @@
|
|||||||
// <config-two>
|
// <config-two>
|
||||||
// use actix_files::{FileConfig, Files};
|
use actix_files as fs;
|
||||||
// use actix_web::App;
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
// #[derive(Default)]
|
pub fn main() {
|
||||||
// struct MyConfig;
|
HttpServer::new(|| {
|
||||||
|
App::new().service(
|
||||||
// impl FileConfig for MyConfig {
|
fs::Files::new("/static", ".")
|
||||||
// fn is_use_etag() -> bool {
|
.show_files_listing()
|
||||||
// false
|
.use_last_modified(true),
|
||||||
// }
|
)
|
||||||
|
})
|
||||||
// fn is_use_last_modifier() -> bool {
|
.bind("127.0.0.1:8088")
|
||||||
// false
|
.unwrap()
|
||||||
// }
|
.run()
|
||||||
// }
|
.unwrap();
|
||||||
|
}
|
||||||
// fn main() {
|
|
||||||
// App::new().service(
|
|
||||||
// "/static",
|
|
||||||
// Files::with_config(".", MyConfig)
|
|
||||||
// .unwrap()
|
|
||||||
// .show_files_listing(),
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
// </config-two>
|
// </config-two>
|
||||||
|
@ -1,8 +1,14 @@
|
|||||||
// <directory>
|
// <directory>
|
||||||
use actix_files as fs;
|
use actix_files as fs;
|
||||||
use actix_web::App;
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
pub fn main() {
|
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>
|
// </directory>
|
||||||
|
@ -1,17 +1,22 @@
|
|||||||
pub mod configuration;
|
pub mod configuration;
|
||||||
pub mod configuration_two;
|
pub mod configuration_two;
|
||||||
pub mod directory;
|
pub mod directory;
|
||||||
|
|
||||||
// <individual-file>
|
// <individual-file>
|
||||||
use actix_files::NamedFile;
|
use actix_files::NamedFile;
|
||||||
use actix_web::{web, App, HttpRequest, Result};
|
use actix_web::{web, App, HttpRequest, HttpServer, Result};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
fn index(req: HttpRequest) -> Result<NamedFile> {
|
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)?)
|
Ok(NamedFile::open(path)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
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>
|
// </individual-file>
|
||||||
|
Loading…
Reference in New Issue
Block a user