diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-05 23:08:09 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-05 23:08:09 -0400 |
commit | bb85374b79086cd8efde24d23a1bffeb97cae26b (patch) | |
tree | a36e2df6a89e567822820ac110afec6a13eacbf6 /src/web/http.rs | |
parent | finish dns and start webserver (diff) | |
download | wrapper-bb85374b79086cd8efde24d23a1bffeb97cae26b.tar.gz wrapper-bb85374b79086cd8efde24d23a1bffeb97cae26b.tar.bz2 wrapper-bb85374b79086cd8efde24d23a1bffeb97cae26b.zip |
new c version
Diffstat (limited to 'src/web/http.rs')
-rw-r--r-- | src/web/http.rs | 50 |
1 files changed, 0 insertions, 50 deletions
diff --git a/src/web/http.rs b/src/web/http.rs deleted file mode 100644 index 7ab1b11..0000000 --- a/src/web/http.rs +++ /dev/null @@ -1,50 +0,0 @@ -use axum::{ - body::Body, - http::{header::HeaderName, HeaderValue, Request, StatusCode}, - response::{IntoResponse, Response}, -}; -use std::str; -use tower::ServiceExt; -use tower_http::services::ServeFile; - -pub fn text(code: u16, msg: &str) -> Response { - (status_code(code), msg.to_owned()).into_response() -} - -pub fn json(code: u16, json: &str) -> Response { - let mut res = (status_code(code), json.to_owned()).into_response(); - res.headers_mut().insert( - HeaderName::from_static("content-type"), - HeaderValue::from_static("application/json"), - ); - res -} - -pub async fn serve(path: &str) -> Response { - if !path.chars().any(|c| c == '.') { - return text(403, "Invalid file path"); - } - - let path = format!("public{path}"); - let file = ServeFile::new(path); - - let Ok(mut res) = file.oneshot(Request::new(Body::empty())).await else { - tracing::error!("Error while fetching file"); - return text(500, "Error when fetching file") - }; - - if res.status() != StatusCode::OK { - return text(404, "File not found"); - } - - res.headers_mut().insert( - HeaderName::from_static("cache-control"), - HeaderValue::from_static("max-age=300"), - ); - - res.into_response() -} - -fn status_code(code: u16) -> StatusCode { - StatusCode::from_u16(code).map_or(StatusCode::OK, |code| code) -} |