mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
First pass at Static Files.
This commit is contained in:
@ -22,4 +22,5 @@ exclude = [
|
||||
"responses",
|
||||
"testing",
|
||||
"middleware",
|
||||
"static-files",
|
||||
]
|
||||
|
10
examples/static-files/Cargo.toml
Normal file
10
examples/static-files/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "static-files"
|
||||
version = "0.1.0"
|
||||
authors = ["Cameron Dershem <cameron@pinkhatbeard.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0"
|
||||
actix-files = "0.1"
|
||||
mime = "*"
|
27
examples/static-files/src/configuration.rs
Normal file
27
examples/static-files/src/configuration.rs
Normal file
@ -0,0 +1,27 @@
|
||||
// <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 std::path::PathBuf;
|
||||
|
||||
// #[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));
|
||||
// }
|
||||
// </config-one>
|
26
examples/static-files/src/configuration_two.rs
Normal file
26
examples/static-files/src/configuration_two.rs
Normal file
@ -0,0 +1,26 @@
|
||||
// <config-two>
|
||||
// use actix_files::{FileConfig, Files};
|
||||
// use actix_web::App;
|
||||
|
||||
// #[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(),
|
||||
// );
|
||||
// }
|
||||
// </config-two>
|
8
examples/static-files/src/directory.rs
Normal file
8
examples/static-files/src/directory.rs
Normal file
@ -0,0 +1,8 @@
|
||||
// <directory>
|
||||
use actix_files as fs;
|
||||
use actix_web::App;
|
||||
|
||||
fn main() {
|
||||
App::new().service(fs::Files::new("/static", ".").show_files_listing());
|
||||
}
|
||||
// </directory>
|
17
examples/static-files/src/main.rs
Normal file
17
examples/static-files/src/main.rs
Normal file
@ -0,0 +1,17 @@
|
||||
mod configuration;
|
||||
mod configuration_two;
|
||||
mod directory;
|
||||
// <individual-file>
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::{web, App, HttpRequest, Result};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn index(req: HttpRequest) -> Result<NamedFile> {
|
||||
let path: PathBuf = req.match_info().query("tail").parse().unwrap();
|
||||
Ok(NamedFile::open(path)?)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new().route("/", web::get().to(index));
|
||||
}
|
||||
// </individual-file>
|
Reference in New Issue
Block a user