diff --git a/Cargo.lock b/Cargo.lock index 76b29ba..f0260a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -394,6 +394,7 @@ dependencies = [ "nom", "serde", "tokio", + "tower", "tower-http", ] diff --git a/Cargo.toml b/Cargo.toml index c02a1f7..680c09b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ axum = { git = "https://github.com/tokio-rs/axum.git", rev = "52a90390195e884bcc nom = "7.1.1" serde = { version = "1.0.183", features = ["derive"] } tokio = { version = "1.30.0", default-features = false, features = ["macros", "process", "rt", "rt-multi-thread"] } -tower-http = { version = "0.4.3", features = ["fs"] } +tower = "0.4.13" +tower-http = { version = "0.4.3", features = ["fs", "set-header"] } diff --git a/src/main.rs b/src/main.rs index 0438c54..01e8032 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,13 @@ #![feature(exit_status_error)] use axum::http::header::CACHE_CONTROL; -use axum::http::HeaderMap; +use axum::http::HeaderValue; use axum::response::IntoResponse; use axum::{http::StatusCode, routing::post, Json, Router}; use owner_tree::build_owner_tree; use parse::emacs_parse_org_document; +use tower::ServiceBuilder; use tower_http::services::{ServeDir, ServeFile}; +use tower_http::set_header::SetResponseHeaderLayer; mod error; mod owner_tree; @@ -14,10 +16,20 @@ mod sexp; #[tokio::main] async fn main() { - let serve_dir = ServeDir::new("static").not_found_service(ServeFile::new("static/index.html")); + let static_files_service = { + let serve_dir = + ServeDir::new("static").not_found_service(ServeFile::new("static/index.html")); + + ServiceBuilder::new() + .layer(SetResponseHeaderLayer::if_not_present( + CACHE_CONTROL, + HeaderValue::from_static("public, max-age=120"), + )) + .service(serve_dir) + }; let app = Router::new() .route("/parse", post(parse_org_mode)) - .fallback_service(serve_dir); + .fallback_service(static_files_service); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); @@ -34,9 +46,3 @@ async fn _parse_org_mode(body: String) -> Result HeaderMap { - let mut headers = HeaderMap::new(); - headers.insert(CACHE_CONTROL, "public, max-age=120".parse().unwrap()); - headers -}