diff --git a/src/bin/cyrkensia.rs b/src/bin/cyrkensia.rs index e5d2b7c..5e07ec5 100644 --- a/src/bin/cyrkensia.rs +++ b/src/bin/cyrkensia.rs @@ -3,6 +3,7 @@ use std::process::exit; use cyrkensia::Config; use cyrkensia::meta::WIKI_HELP_URL; use cyrkensia::server::CyrkensiaState; +use cyrkensia::server::redirect::trail_slash; use cyrkensia::server::routes::{index, hostinfo}; use cyrkensia::server::middleware::{cors_everywhere, source_headers, license_headers}; use actix_web::{web, App, HttpServer}; @@ -31,11 +32,12 @@ fn init() -> io::Result { /// Server startup. async fn server(cfg: Config) -> io::Result<()> { // ---- Server Init ---- + let mut printer = Printer::default(); let bindaddr = cfg.bindaddr.clone(); let unbound_server = HttpServer::new(move || { // Initialize state let Ok(state) = CyrkensiaState::new(cfg.clone()) else { - Printer::default().errorln("Cyrkensia failed trying to initialize!", Colors::YellowBright); + eprintln!("Cyrkensia failed trying to initialize!"); exit(1); }; @@ -49,7 +51,8 @@ async fn server(cfg: Config) -> io::Result<()> { .wrap(license_headers()) //Routes .route("/", web::get().to(hostinfo)) - .route("/{album}", web::get().to(index)) + .route("/{album}/", web::get().to(index)) + .route("/{album}", web::get().to(trail_slash)) }); // ---- Server Bind ---- @@ -64,6 +67,7 @@ async fn server(cfg: Config) -> io::Result<()> { let server = unbound_server.bind(bindaddr)?; // ---- Ignite ---- + printer.println("Cyrkensia server successfully started!", Colors::CyanBright); server.run().await } @@ -73,17 +77,17 @@ async fn main() { let mut console = Printer::default(); let Ok(config) = init() else { console.errorln("Failed to read the config file for Cyrkensia!", Colors::RedBright); - console.errorln(&("See ".to_owned() + WIKI_HELP_URL + " for more."), Colors::Yellow); + console.errorln(&("See ".to_owned() + WIKI_HELP_URL + " for more."), Colors::YellowBright); exit(1); }; // Start if let Err(serv) = server(config).await { - console.errorln("An error occured while running the server:", Colors::Red); + console.errorln("An error occured while running the server:", Colors::RedBright); eprintln!("{serv}"); } // Exit - console.println("Successfully stopped the Cyrkensia server!", Colors::Cyan); + console.println("Cyrkensia server successfully stopped!", Colors::CyanBright); exit(0) } \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 62474dc..cb2ec10 100644 --- a/src/config.rs +++ b/src/config.rs @@ -53,7 +53,7 @@ pub struct Config { /// Maximum Age /// - /// The maximum age of the [Hostinfo] in milliseconds as a [u64]. If [None], the Hostinfo will always be regenerated when its route is accessed. + /// The maximum age of the [Hostinfo] in *seconds* as a [u64]. If [None], the Hostinfo will always be regenerated when its route is accessed. /// This basically activates caching. pub max_age: Option } diff --git a/src/server/redirect.rs b/src/server/redirect.rs index de0fca7..91e5b3b 100644 --- a/src/server/redirect.rs +++ b/src/server/redirect.rs @@ -1,5 +1,6 @@ -use actix_web::{HttpResponse, Responder, http::header::LOCATION}; +use actix_web::{HttpResponse, Responder, http::header::LOCATION, HttpRequest}; use crate::meta; +use super::uri_noquery; /// Redirect /// @@ -22,4 +23,14 @@ pub fn repository() -> impl Responder { /// Redirects to the license text. pub fn license() -> impl Responder { redirect(meta::LICENSE_URL) +} + +/// Trailing slash redirect +/// +/// Redirects the user to a URL with a trailing slash. +pub async fn trail_slash(req: HttpRequest) -> impl Responder { + let uri = uri_noquery(req.uri()); + HttpResponse::Found() + .insert_header(("Location", uri + "/")) + .finish() } \ No newline at end of file diff --git a/src/server/routes.rs b/src/server/routes.rs index e9d3918..4c74143 100644 --- a/src/server/routes.rs +++ b/src/server/routes.rs @@ -66,7 +66,7 @@ pub async fn hostinfo(req: HttpRequest, data: web::Data) -> impl /// /// Simple struct containing the param-name and param-type needed for the [index] route. pub struct IndexParams { - album: String + pub album: String } /// Album Index Route @@ -106,11 +106,16 @@ pub async fn index(p: web::Path, data: web::Data) - }; // Codegen - let headstr = format!("

{} ({})

\n", meta.0, meta.1); + let headmeta = r""; + let headstr = format!("

{} ({})

", meta.0, meta.1); let bodystr = meta.2.into_iter().fold(String::new(), |total, item| total + &format!("{}
\n", item, item)); // Send response HttpResponse::Ok() .content_type(ContentType::html()) - .body(headstr + &bodystr) + .body(format!("{}{}{}", headmeta, headstr, bodystr)) } \ No newline at end of file