From d410b27e85ffa653a64d10bcd6b43cc5f64fe5d9 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Wed, 13 Feb 2019 18:51:51 +0100 Subject: Removed config.rs and put back actix config in main.rs --- src/main.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 633ab84..080124d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use actix_web::{middleware, server, App}; +use actix_web::{fs, middleware, server, App}; use clap::crate_version; use simplelog::{Config, LevelFilter, TermLogger}; use std::io::{self, Write}; @@ -9,9 +9,22 @@ use yansi::{Color, Paint}; mod args; mod auth; -mod config; mod listing; +#[derive(Clone, Debug)] +pub struct MiniserveConfig { + pub verbose: bool, + pub path: std::path::PathBuf, + pub port: u16, + pub interfaces: Vec, + pub auth: Option, + pub path_explicitly_chosen: bool, + pub no_symlinks: bool, + pub random_route: Option, + pub sort_method: listing::SortingMethods, + pub reverse_sort: bool, +} + fn main() { if cfg!(windows) && !Paint::enable_windows_ascii() { Paint::disable(); @@ -43,7 +56,7 @@ fn main() { App::with_state(inside_config.clone()) .middleware(auth::Auth) .middleware(middleware::Logger::default()) - .configure(config::configure_app) + .configure(configure_app) }) .bind( miniserve_config @@ -138,3 +151,41 @@ fn main() { let _ = sys.run(); } + +pub fn configure_app(app: App) -> App { + let s = { + let path = &app.state().path; + let no_symlinks = app.state().no_symlinks; + let random_route = app.state().random_route.clone(); + let sort_method = app.state().sort_method; + let reverse_sort = app.state().reverse_sort; + if path.is_file() { + None + } else { + Some( + fs::StaticFiles::new(path) + .expect("Couldn't create path") + .show_files_listing() + .files_listing_renderer(move |dir, req| { + listing::directory_listing( + dir, + req, + no_symlinks, + random_route.clone(), + sort_method, + reverse_sort, + ) + }), + ) + } + }; + + let random_route = app.state().random_route.clone().unwrap_or_default(); + let full_route = format!("/{}", random_route); + + if let Some(s) = s { + app.handler(&full_route, s) + } else { + app.resource(&full_route, |r| r.f(listing::file_handler)) + } +} -- cgit v1.2.3