chore: first commit
This commit is contained in:
commit
4f811d88f2
30 changed files with 4301 additions and 0 deletions
70
src/router.rs
Normal file
70
src/router.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
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 crate::{AppState, Result, ResultTemplate, Tx};
|
||||
|
||||
pub fn new() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/", get(handler_home))
|
||||
.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)]
|
||||
struct ExampleRow {
|
||||
pub database: String,
|
||||
pub version: String,
|
||||
pub current_database: String,
|
||||
pub current_user: String,
|
||||
pub current_timestamp: chrono::DateTime<Utc>,
|
||||
}
|
||||
|
||||
async fn handler_home(
|
||||
State(state): State<AppState>,
|
||||
HxRequest(hx_request): HxRequest,
|
||||
mut tx: Tx,
|
||||
) -> ResultTemplate {
|
||||
let template = state.tmpl_env.get_template("index.html")?;
|
||||
|
||||
let rows = sqlx::query_as::<_, ExampleRow>("select 'Postgres' as database, setting as version, current_database(), current_user, current_timestamp from pg_settings where name = 'server_version'")
|
||||
.fetch_all(&mut tx)
|
||||
.await?;
|
||||
|
||||
println!("{:?}", rows);
|
||||
|
||||
let content = if hx_request {
|
||||
template
|
||||
.eval_to_state(context!(rows => rows))?
|
||||
.render_block("htmx")?
|
||||
} else {
|
||||
template.render(context!(rows => rows))?
|
||||
};
|
||||
|
||||
Ok(Html(content))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue