diff options
author | Lukas Stabe <lukas@stabe.de> | 2020-09-26 02:45:31 +0000 |
---|---|---|
committer | Lukas Stabe <lukas@stabe.de> | 2020-09-26 02:45:31 +0000 |
commit | dab13c9bb3336aac4e44171f3074c4ce973b9782 (patch) | |
tree | 1e4592b6c38e9d230b76d9e0d485cb4be7c4f6f2 /src | |
parent | [wip] client-side color-scheme handling (diff) | |
download | miniserve-dab13c9bb3336aac4e44171f3074c4ce973b9782.tar.gz miniserve-dab13c9bb3336aac4e44171f3074c4ce973b9782.zip |
move css out of html into its own route
Diffstat (limited to 'src')
-rw-r--r-- | src/args.rs | 6 | ||||
-rw-r--r-- | src/auth.rs | 1 | ||||
-rw-r--r-- | src/file_upload.rs | 7 | ||||
-rw-r--r-- | src/listing.rs | 3 | ||||
-rw-r--r-- | src/main.rs | 24 | ||||
-rw-r--r-- | src/renderer.rs | 21 |
6 files changed, 46 insertions, 16 deletions
diff --git a/src/args.rs b/src/args.rs index 25a3503..2ffbdc5 100644 --- a/src/args.rs +++ b/src/args.rs @@ -175,9 +175,10 @@ pub fn parse_args() -> crate::MiniserveConfig { None }; - // Generate some random route for the favicon so that it is very unlikely to conflict with a - // real file. + // Generate some random routes for the favicon and css so that they are very unlikely to conflict with + // real files. let favicon_route = nanoid::nanoid!(10, &ROUTE_ALPHABET); + let css_route = nanoid::nanoid!(10, &ROUTE_ALPHABET); let default_color_scheme = args.color_scheme; @@ -198,6 +199,7 @@ pub fn parse_args() -> crate::MiniserveConfig { no_symlinks: args.no_symlinks, random_route, favicon_route, + css_route, default_color_scheme, index: args.index, overwrite_files: args.overwrite_files, diff --git a/src/auth.rs b/src/auth.rs index 5d01568..3233e22 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -123,6 +123,7 @@ fn build_unauthorized_response( false, false, &state.favicon_route, + &state.css_route, ) .into_string() } diff --git a/src/file_upload.rs b/src/file_upload.rs index 3f713cd..3afa755 100644 --- a/src/file_upload.rs +++ b/src/file_upload.rs @@ -110,6 +110,7 @@ pub fn upload_file( payload: actix_web::web::Payload, uses_random_route: bool, favicon_route: String, + css_route: String, ) -> Pin<Box<dyn Future<Output = Result<HttpResponse, actix_web::Error>>>> { let conf = req.app_data::<crate::MiniserveConfig>().unwrap(); let return_path = if let Some(header) = req.headers().get(header::REFERER) { @@ -136,6 +137,7 @@ pub fn upload_file( query_params.order, uses_random_route, &favicon_route, + &css_route, )); } }; @@ -155,6 +157,7 @@ pub fn upload_file( query_params.order, uses_random_route, &favicon_route, + &css_route, )); } }; @@ -174,6 +177,7 @@ pub fn upload_file( query_params.order, uses_random_route, &favicon_route, + &css_route, )); } }; @@ -198,6 +202,7 @@ pub fn upload_file( query_params.order, uses_random_route, &favicon_route, + &css_route, ), }), ) @@ -213,6 +218,7 @@ fn create_error_response( sorting_order: Option<SortingOrder>, uses_random_route: bool, favicon_route: &str, + css_route: &str, ) -> future::Ready<Result<HttpResponse, actix_web::Error>> { errors::log_error_chain(description.to_string()); future::ok( @@ -228,6 +234,7 @@ fn create_error_response( true, !uses_random_route, &favicon_route, + &css_route, ) .into_string(), ), diff --git a/src/listing.rs b/src/listing.rs index 40c84b3..322de73 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -151,6 +151,7 @@ pub fn directory_listing( file_upload: bool, random_route: Option<String>, favicon_route: String, + css_route: String, show_qrcode: bool, upload_route: String, tar_enabled: bool, @@ -336,6 +337,7 @@ pub fn directory_listing( false, false, &favicon_route, + &css_route, ) .into_string(), ), @@ -394,6 +396,7 @@ pub fn directory_listing( file_upload, &upload_route, &favicon_route, + &css_route, &encoded_dir, breadcrumbs, tar_enabled, diff --git a/src/main.rs b/src/main.rs index ffb393b..5b53070 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,6 +56,9 @@ pub struct MiniserveConfig { /// Randomly generated favicon route pub favicon_route: String, + /// Randomly generated css route + pub css_route: String, + /// Default color scheme pub default_color_scheme: themes::ColorScheme, @@ -252,6 +255,10 @@ async fn run() -> Result<(), ContextualError> { &format!("/{}", inside_config.favicon_route), web::get().to(favicon), ) + .route( + &format!("/{}", inside_config.css_route), + web::get().to(css), + ) .configure(|c| configure_app(c, &inside_config)) .default_service(web::get().to(error_404)) }) @@ -284,6 +291,7 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) { let no_symlinks = conf.no_symlinks; let random_route = conf.random_route.clone(); let favicon_route = conf.favicon_route.clone(); + let css_route = conf.css_route.clone(); let show_qrcode = conf.show_qrcode; let file_upload = conf.file_upload; let tar_enabled = conf.tar_enabled; @@ -313,6 +321,7 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) { file_upload, random_route.clone(), favicon_route.clone(), + css_route.clone(), show_qrcode, u_r.clone(), tar_enabled, @@ -326,12 +335,13 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) { }; let favicon_route = conf.favicon_route.clone(); + let css_route = conf.css_route.clone(); if let Some(serve_path) = serve_path { if conf.file_upload { // Allow file upload app.service( web::resource(&upload_route).route(web::post().to(move |req, payload| { - file_upload::upload_file(req, payload, uses_random_route, favicon_route.clone()) + file_upload::upload_file(req, payload, uses_random_route, favicon_route.clone(), css_route.clone()) })), ) // Handle directories @@ -351,6 +361,7 @@ async fn error_404(req: HttpRequest) -> HttpResponse { let conf = req.app_data::<MiniserveConfig>().unwrap(); let uses_random_route = conf.random_route.is_some(); let favicon_route = conf.favicon_route.clone(); + let css_route = conf.css_route.clone(); let query_params = listing::extract_query_parameters(&req); errors::log_error_chain(err_404.to_string()); @@ -365,6 +376,7 @@ async fn error_404(req: HttpRequest) -> HttpResponse { false, !uses_random_route, &favicon_route, + &css_route, ) .into_string(), ) @@ -376,3 +388,13 @@ async fn favicon() -> impl Responder { .set(ContentType(mime::IMAGE_SVG)) .message_body(logo.into()) } + +lazy_static::lazy_static! { + static ref CSS_TEXT: String = grass::from_string(include_str!("../data/style.scss").to_string(), &grass::Options::default()).unwrap(); +} + +async fn css() -> impl Responder { + web::HttpResponse::Ok() + .set(ContentType(mime::TEXT_CSS)) + .body(&*CSS_TEXT) +} diff --git a/src/renderer.rs b/src/renderer.rs index dedd028..0f11c25 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -20,6 +20,7 @@ pub fn page( file_upload: bool, upload_route: &str, favicon_route: &str, + css_route: &str, encoded_dir: &str, breadcrumbs: Vec<Breadcrumb>, tar_enabled: bool, @@ -36,7 +37,7 @@ pub fn page( html! { (DOCTYPE) html { - (page_header(&title_path, file_upload, favicon_route)) + (page_header(&title_path, file_upload, favicon_route, css_route)) body#drop-container { (PreEscaped(r#" <script> @@ -340,14 +341,6 @@ fn entry_row( } } -lazy_static::lazy_static! { - static ref CSS_TEXT: String = grass::from_string(include_str!("../data/style.scss").to_string(), &grass::Options::default()).unwrap(); -} -/// Partial: CSS -fn css() -> Markup { - PreEscaped(CSS_TEXT.clone()) -} - /// Partial: up arrow fn arrow_up() -> Markup { PreEscaped("⇪".to_string()) @@ -369,16 +362,17 @@ fn chevron_down() -> Markup { } /// Partial: page header -fn page_header(title: &str, file_upload: bool, favicon_route: &str) -> Markup { +fn page_header(title: &str, file_upload: bool, favicon_route: &str, css_route: &str) -> Markup { html! { head { meta charset="utf-8"; meta http-equiv="X-UA-Compatible" content="IE=edge"; meta name="viewport" content="width=device-width, initial-scale=1"; + link rel="icon" type="image/svg+xml" href={ "/" (favicon_route) }; - title { (title) } + link rel="stylesheet" href={ "/" (css_route) }; - style { (css()) } + title { (title) } @if file_upload { (PreEscaped(r#" @@ -452,6 +446,7 @@ pub fn render_error( has_referer: bool, display_back_link: bool, favicon_route: &str, + css_route: &str, ) -> Markup { let link = if has_referer { return_address.to_string() @@ -462,7 +457,7 @@ pub fn render_error( html! { (DOCTYPE) html { - (page_header(&error_code.to_string(), false, favicon_route)) + (page_header(&error_code.to_string(), false, favicon_route, css_route)) body { div.error { p { (error_code.to_string()) } |