mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
Static Files is done-ish.
This commit is contained in:
@ -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>
|
||||
|
@ -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>
|
||||
|
@ -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>
|
||||
|
@ -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>
|
||||
|
Reference in New Issue
Block a user