diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-01 20:34:22 -0500 |
---|---|---|
committer | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-01 20:34:22 -0500 |
commit | 2026a8f4579b1db0f6e5e7b11ac33c13969adb6c (patch) | |
tree | daa13419b7227462775e325a4f5f60f2ed33c1da /src/public/mod.rs | |
parent | remove b64 imgs (diff) | |
download | xssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.tar.gz xssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.tar.bz2 xssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.zip |
static serve refactor
Diffstat (limited to 'src/public/mod.rs')
-rw-r--r-- | src/public/mod.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/public/mod.rs b/src/public/mod.rs new file mode 100644 index 0000000..cf8156d --- /dev/null +++ b/src/public/mod.rs @@ -0,0 +1,47 @@ +use axum::{ + body::Body, + http::{Request, StatusCode}, + response::{IntoResponse, Response}, + routing::get, + Router, +}; +use tower::ServiceExt; +use tower_http::services::ServeFile; + +use crate::types::http::ResponseCode; + +pub mod admin; +pub mod console; +pub mod file; +pub mod pages; + +pub fn router() -> Router { + Router::new() + .nest("/", pages::router()) + .route("/js/*path", get(file::js)) + .route("/css/*path", get(file::css)) + .route("/fonts/*path", get(file::fonts)) + .route("/image/*path", get(file::image)) + .route("/image/avatar", get(file::avatar)) + .route("/image/banner", get(file::banner)) +} + +pub async fn serve(path: &str) -> Response { + if !path.chars().any(|c| c == '.') { + return ResponseCode::BadRequest.text("Invalid file path"); + } + + let path = format!("public{path}"); + let file = ServeFile::new(path); + + let Ok(res) = file.oneshot(Request::new(Body::empty())).await else { + tracing::error!("Error while fetching file"); + return ResponseCode::InternalServerError.text("Error while fetching file"); + }; + + if res.status() != StatusCode::OK { + return ResponseCode::NotFound.text("File not found"); + } + + res.into_response() +} |