generated from alecodes/base-template
refactor: change asset handling
read assets from the template state instead of path to be able to embed them into the binary instead of reading them form a path
This commit is contained in:
parent
3ff1f26cc4
commit
1871610770
4 changed files with 53 additions and 9 deletions
|
|
@ -1,17 +1,39 @@
|
|||
use axum::{extract::State, response::Html, routing::get, Router};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::{header, HeaderMap, HeaderValue},
|
||||
response::Html,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use axum_htmx::HxRequest;
|
||||
use chrono::Utc;
|
||||
use minijinja::context;
|
||||
use serde::Serialize;
|
||||
use sqlx::prelude::FromRow;
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
use crate::{AppState, ResultTemplate, Tx};
|
||||
use crate::{AppState, Result, ResultTemplate, Tx};
|
||||
|
||||
pub fn new() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/", get(handler_home))
|
||||
.nest_service("/assets", ServeDir::new("dist/assets"))
|
||||
.route("/assets/{*asset}", get(handle_assets))
|
||||
}
|
||||
|
||||
async fn handle_assets(
|
||||
State(state): State<AppState>,
|
||||
Path(asset): Path<String>,
|
||||
) -> Result<(HeaderMap, String)> {
|
||||
let mut headers = HeaderMap::new();
|
||||
|
||||
let mime = mime_guess::from_path(&asset).first_raw();
|
||||
|
||||
if let Some(mime) = mime {
|
||||
headers.insert(header::CONTENT_TYPE, HeaderValue::from_static(mime));
|
||||
}
|
||||
|
||||
let template = state.tmpl_env.get_template(&format!("assets/{}", asset))?;
|
||||
|
||||
Ok((headers, template.render(context!())?))
|
||||
}
|
||||
|
||||
#[derive(FromRow, Debug, Serialize)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue