Actix v4 (#11)
Updated to: * actix-web 4.0.0 * actix-files 0.6.0 * clap 3.1.0 * rust edition 2021
This commit is contained in:
parent
d3fd28461c
commit
24ee4f3f09
4 changed files with 520 additions and 1054 deletions
1525
Cargo.lock
generated
1525
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
18
Cargo.toml
18
Cargo.toml
|
@ -1,23 +1,23 @@
|
|||
[package]
|
||||
name = "http-server"
|
||||
version = "0.12.0"
|
||||
version = "0.13.0"
|
||||
authors = ["Damjan Georgievski <gdamjan@gmail.com>"]
|
||||
license = "MIT"
|
||||
readme = "README.md"
|
||||
homepage = "https://github.com/gdamjan/http-server-rs"
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3.0"
|
||||
actix-files = "0.3.0"
|
||||
bytes = "0.5.6"
|
||||
clap = "2"
|
||||
actix-web = "4.0.0"
|
||||
actix-files = "0.6.0"
|
||||
bytes = "1.1.0"
|
||||
clap = { version = "3.1.0", features = ["cargo"] }
|
||||
env_logger = "*"
|
||||
log = "*"
|
||||
futures = "0.3"
|
||||
futures = "0.3.19"
|
||||
tar = "0.4"
|
||||
percent-encoding = "2.0"
|
||||
v_htmlescape = "0.10"
|
||||
percent-encoding = "2.1.0"
|
||||
v_htmlescape = "0.14.1"
|
||||
|
||||
[profile.release]
|
||||
opt-level = 'z'
|
||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -5,13 +5,13 @@ mod web;
|
|||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let app = clap::App::new(clap::crate_name!())
|
||||
let app = clap::Command::new(clap::crate_name!())
|
||||
.author(clap::crate_authors!("\n"))
|
||||
.version(clap::crate_version!())
|
||||
.about(clap::crate_description!())
|
||||
.arg(
|
||||
clap::Arg::with_name("chdir")
|
||||
.short("w")
|
||||
clap::Arg::new("chdir")
|
||||
.short('w')
|
||||
.long("chdir")
|
||||
.value_name("DIRECTORY")
|
||||
.help("directory to serve")
|
||||
|
@ -19,8 +19,8 @@ async fn main() -> std::io::Result<()> {
|
|||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::with_name("addr")
|
||||
.short("b")
|
||||
clap::Arg::new("addr")
|
||||
.short('b')
|
||||
.long("bind")
|
||||
.value_name("ADDRESS")
|
||||
.help("bind address")
|
||||
|
@ -28,7 +28,7 @@ async fn main() -> std::io::Result<()> {
|
|||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::with_name("port")
|
||||
clap::Arg::new("port")
|
||||
.value_name("PORT")
|
||||
.help("Specify alternate port")
|
||||
.default_value("8000")
|
||||
|
|
19
src/web.rs
19
src/web.rs
|
@ -1,5 +1,4 @@
|
|||
use actix_web::{get, error, middleware, web, App, Error, HttpServer, HttpRequest, HttpResponse, Responder};
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{get, middleware, web, App, Error, HttpServer, HttpRequest, HttpResponse, Responder};
|
||||
use actix_files::{Files, NamedFile};
|
||||
use futures::StreamExt;
|
||||
|
||||
|
@ -16,7 +15,7 @@ pub async fn run(bind_addr: &str, root: &PathBuf) -> std::io::Result<()> {
|
|||
.files_listing_renderer(crate::directory_listing::directory_listing);
|
||||
|
||||
App::new()
|
||||
.data(root_.clone())
|
||||
.app_data(root_.clone())
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(favicon_ico)
|
||||
.service(handle_tar)
|
||||
|
@ -30,24 +29,24 @@ pub async fn run(bind_addr: &str, root: &PathBuf) -> std::io::Result<()> {
|
|||
}
|
||||
|
||||
#[get("/{tail:.*}.tar")]
|
||||
async fn handle_tar(req: HttpRequest, root: web::Data<PathBuf>, web::Path(tail): web::Path<String>) -> impl Responder {
|
||||
async fn handle_tar(req: HttpRequest, root: web::Data<PathBuf>, tail: web::Path<String>) -> impl Responder {
|
||||
let relpath = PathBuf::from(tail.trim_end_matches('/'));
|
||||
let fullpath = root.join(&relpath)
|
||||
.canonicalize()
|
||||
.map_err(|err| error::InternalError::new(err, StatusCode::NOT_FOUND))?;
|
||||
.unwrap();
|
||||
|
||||
|
||||
// if a .tar already exists, just return it as-is
|
||||
let mut fullpath_tar = fullpath.clone();
|
||||
fullpath_tar.set_extension("tar");
|
||||
if fullpath_tar.is_file() {
|
||||
return NamedFile::open(fullpath_tar)
|
||||
.map_err(|err| error::InternalError::new(err, StatusCode::INTERNAL_SERVER_ERROR))?
|
||||
return NamedFile::open_async(fullpath_tar).await
|
||||
.unwrap()
|
||||
.into_response(&req);
|
||||
}
|
||||
|
||||
if !(fullpath.is_dir()) {
|
||||
return Ok(HttpResponse::NotFound().body("Directory not found\n"));
|
||||
return HttpResponse::NotFound().body("Directory not found\n");
|
||||
}
|
||||
|
||||
let stream = crate::threaded_archiver::stream_tar_in_thread(fullpath)
|
||||
|
@ -56,7 +55,7 @@ async fn handle_tar(req: HttpRequest, root: web::Data<PathBuf>, web::Path(tail):
|
|||
.content_type("application/x-tar")
|
||||
.streaming(stream);
|
||||
|
||||
Ok(response)
|
||||
response
|
||||
}
|
||||
|
||||
const FAVICON_ICO: &[u8] = include_bytes!("favicon.png");
|
||||
|
@ -65,6 +64,6 @@ const FAVICON_ICO: &[u8] = include_bytes!("favicon.png");
|
|||
async fn favicon_ico() -> impl Responder {
|
||||
HttpResponse::Ok()
|
||||
.content_type("image/png")
|
||||
.header("Cache-Control", "only-if-cached, max-age=86400")
|
||||
.append_header(("Cache-Control", "only-if-cached, max-age=86400"))
|
||||
.body(FAVICON_ICO)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue