asset work

This commit is contained in:
Robin Appelman 2023-04-08 22:15:50 +02:00
commit 1648eeffab
6 changed files with 144 additions and 46 deletions

View file

@ -1,3 +1,4 @@
mod asset;
mod config;
mod data;
mod error;
@ -9,12 +10,12 @@ use crate::data::demo::{Demo, ListDemo};
use crate::pages::demo::DemoPage;
use crate::pages::index::Index;
use crate::pages::render;
use asset::{serve_compiled, serve_static};
use axum::extract::{MatchedPath, Path};
use axum::http::{HeaderValue, Request};
use axum::http::Request;
use axum::response::IntoResponse;
use axum::{extract::State, routing::get, Router, Server};
pub use error::Error;
use hyper::header::{CACHE_CONTROL, CONTENT_TYPE, ETAG};
use hyperlocal::UnixServerExt;
use maud::Markup;
use sqlx::PgPool;
@ -52,7 +53,9 @@ async fn main() -> Result<()> {
let app = Router::new()
.route("/", get(index))
.route("/style.:version.css", get(style))
.route("/style.css", get(serve_compiled!("style.css")))
.route("/images/logo.png", get(serve_static!("../images/logo.png")))
.route("/images/logo.svg", get(serve_static!("../images/logo.svg")))
.route("/:id", get(demo))
.layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
@ -94,26 +97,6 @@ async fn main() -> Result<()> {
Ok(())
}
async fn style() -> impl IntoResponse {
let style = include_str!(concat!(env!("OUT_DIR"), "/style.css"));
let etag = concat!(
r#"""#,
include_str!(concat!(env!("OUT_DIR"), "/style.md5")),
r#"""#
);
(
[
(CONTENT_TYPE, HeaderValue::from_static("text/css")),
(ETAG, HeaderValue::from_static(etag)),
(
CACHE_CONTROL,
HeaderValue::from_static("public, max-age=2592000, immutable"),
),
],
style,
)
}
async fn index(State(app): State<Arc<App>>) -> Result<Markup> {
let demos = ListDemo::list(&app.connection, None).await?;
Ok(render(Index { demos }))