feat: move static files to directory

This commit is contained in:
Jef Roosens 2025-01-10 15:45:46 +01:00
parent 1d910bc421
commit 0375f76b65
Signed by: Jef Roosens
GPG key ID: 21FD3D77D56BAF49
7 changed files with 42 additions and 14 deletions

View file

@ -13,10 +13,6 @@ const MIGRATIONS: [&str; 4] = [
include_str!("migrations/002_comments.sql"),
include_str!("migrations/003_events.sql"),
];
const STATIC_FILES: [(&str, &'static str); 1] = [(
"htmx_2.0.4.min.js",
include_str!("static/htmx_2.0.4.min.js"),
)];
#[derive(Clone)]
pub struct Context {
@ -35,11 +31,13 @@ async fn main() {
let template_dir = std::env::var("TEMPLATE_DIR").unwrap_or(String::from("./templates"));
let tera = Tera::new(&format!("{template_dir}/**/*")).unwrap();
let static_dir = std::env::var("STATIC_DIR").unwrap_or(String::from("./static"));
let ctx = Context {
pool,
tera: Arc::new(tera),
};
let app = server::app(ctx).layer(CompressionLayer::new().br(true).gzip(true));
let app = server::app(ctx, &static_dir).layer(CompressionLayer::new().br(true).gzip(true));
let address = "0.0.0.0:8000";

View file

@ -11,7 +11,7 @@ use axum::{
Router,
};
use tera::Context;
use tower_http::set_header::SetResponseHeaderLayer;
use tower_http::{services::ServeDir, set_header::SetResponseHeaderLayer};
use crate::db::Plant;
@ -48,18 +48,15 @@ pub fn render_view(
}
}
pub fn app(ctx: crate::Context) -> axum::Router {
let mut router = Router::new()
pub fn app(ctx: crate::Context, static_dir: &str) -> axum::Router {
let router = Router::new()
.route("/", get(get_index))
.nest("/plants", plants::app())
.nest("/comments", comments::app())
.nest("/events", events::app())
.nest_service("/static", ServeDir::new(static_dir))
.with_state(ctx.clone());
for (name, content) in crate::STATIC_FILES {
router = router.route(&format!("/static/{}", name), get(content))
}
// Routes return either partial or full pages depending on whether the request is done using
// HTMX or just as a plain HTTP request. Adding the Vary header ensures caches don't mix
// partial and full responses.

File diff suppressed because one or more lines are too long