feat: move static files to directory
parent
1d910bc421
commit
0375f76b65
|
@ -4,3 +4,4 @@
|
||||||
!Cargo.lock
|
!Cargo.lock
|
||||||
!src/**
|
!src/**
|
||||||
!templates/**
|
!templates/**
|
||||||
|
!static/**
|
||||||
|
|
|
@ -568,6 +568,12 @@ dependencies = [
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "http-range-header"
|
||||||
|
version = "0.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9171a2ea8a68358193d15dd5d70c1c10a2afc3e7e4c5bc92bc9f025cebd7359c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "httparse"
|
name = "httparse"
|
||||||
version = "1.9.5"
|
version = "1.9.5"
|
||||||
|
@ -742,6 +748,16 @@ version = "0.3.17"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mime_guess"
|
||||||
|
version = "2.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
|
||||||
|
dependencies = [
|
||||||
|
"mime",
|
||||||
|
"unicase",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "miniz_oxide"
|
name = "miniz_oxide"
|
||||||
version = "0.8.2"
|
version = "0.8.2"
|
||||||
|
@ -1373,13 +1389,21 @@ dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"bytes",
|
"bytes",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-util",
|
||||||
"http",
|
"http",
|
||||||
"http-body",
|
"http-body",
|
||||||
|
"http-body-util",
|
||||||
|
"http-range-header",
|
||||||
|
"httpdate",
|
||||||
|
"mime",
|
||||||
|
"mime_guess",
|
||||||
|
"percent-encoding",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
"tower-layer",
|
"tower-layer",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1514,6 +1538,12 @@ dependencies = [
|
||||||
"unic-common",
|
"unic-common",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicase"
|
||||||
|
version = "2.8.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.14"
|
version = "1.0.14"
|
||||||
|
|
|
@ -18,6 +18,6 @@ rusqlite = { version = "0.32.1", features = ["chrono", "bundled"] }
|
||||||
serde = { version = "1.0.217", features = ["derive"] }
|
serde = { version = "1.0.217", features = ["derive"] }
|
||||||
tera = "1.20.0"
|
tera = "1.20.0"
|
||||||
tokio = { version = "1.42.0", features = ["full"] }
|
tokio = { version = "1.42.0", features = ["full"] }
|
||||||
tower-http = { version = "0.6.2", features = ["compression-br", "compression-gzip", "set-header"] }
|
tower-http = { version = "0.6.2", features = ["compression-br", "compression-gzip", "set-header", "fs"] }
|
||||||
tracing = "0.1.41"
|
tracing = "0.1.41"
|
||||||
tracing-subscriber = "0.3.19"
|
tracing-subscriber = "0.3.19"
|
||||||
|
|
|
@ -27,6 +27,7 @@ FROM alpine:3.21
|
||||||
COPY --from=builder /app/target/release/calathea /app/calathea
|
COPY --from=builder /app/target/release/calathea /app/calathea
|
||||||
COPY --from=builder /app/dumb-init /app/dumb-init
|
COPY --from=builder /app/dumb-init /app/dumb-init
|
||||||
COPY templates /app/templates
|
COPY templates /app/templates
|
||||||
|
COPY static /app/static
|
||||||
|
|
||||||
# Create a non-root user & make sure it can write to the data directory
|
# Create a non-root user & make sure it can write to the data directory
|
||||||
RUN set -x && \
|
RUN set -x && \
|
||||||
|
@ -36,7 +37,8 @@ RUN set -x && \
|
||||||
|
|
||||||
WORKDIR /data
|
WORKDIR /data
|
||||||
|
|
||||||
ENV TEMPLATE_DIR=/app/templates
|
ENV TEMPLATE_DIR=/app/templates \
|
||||||
|
STATIC_DIR=/app/static
|
||||||
|
|
||||||
USER www-data:www-data
|
USER www-data:www-data
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,6 @@ const MIGRATIONS: [&str; 4] = [
|
||||||
include_str!("migrations/002_comments.sql"),
|
include_str!("migrations/002_comments.sql"),
|
||||||
include_str!("migrations/003_events.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)]
|
#[derive(Clone)]
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
|
@ -35,11 +31,13 @@ async fn main() {
|
||||||
let template_dir = std::env::var("TEMPLATE_DIR").unwrap_or(String::from("./templates"));
|
let template_dir = std::env::var("TEMPLATE_DIR").unwrap_or(String::from("./templates"));
|
||||||
let tera = Tera::new(&format!("{template_dir}/**/*")).unwrap();
|
let tera = Tera::new(&format!("{template_dir}/**/*")).unwrap();
|
||||||
|
|
||||||
|
let static_dir = std::env::var("STATIC_DIR").unwrap_or(String::from("./static"));
|
||||||
|
|
||||||
let ctx = Context {
|
let ctx = Context {
|
||||||
pool,
|
pool,
|
||||||
tera: Arc::new(tera),
|
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";
|
let address = "0.0.0.0:8000";
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ use axum::{
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use tera::Context;
|
use tera::Context;
|
||||||
use tower_http::set_header::SetResponseHeaderLayer;
|
use tower_http::{services::ServeDir, set_header::SetResponseHeaderLayer};
|
||||||
|
|
||||||
use crate::db::Plant;
|
use crate::db::Plant;
|
||||||
|
|
||||||
|
@ -48,18 +48,15 @@ pub fn render_view(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn app(ctx: crate::Context) -> axum::Router {
|
pub fn app(ctx: crate::Context, static_dir: &str) -> axum::Router {
|
||||||
let mut router = Router::new()
|
let router = Router::new()
|
||||||
.route("/", get(get_index))
|
.route("/", get(get_index))
|
||||||
.nest("/plants", plants::app())
|
.nest("/plants", plants::app())
|
||||||
.nest("/comments", comments::app())
|
.nest("/comments", comments::app())
|
||||||
.nest("/events", events::app())
|
.nest("/events", events::app())
|
||||||
|
.nest_service("/static", ServeDir::new(static_dir))
|
||||||
.with_state(ctx.clone());
|
.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
|
// 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
|
// HTMX or just as a plain HTTP request. Adding the Vary header ensures caches don't mix
|
||||||
// partial and full responses.
|
// partial and full responses.
|
||||||
|
|
Loading…
Reference in New Issue