mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2025-07-28 02:38:30 +00:00
62 lines
1.4 KiB
Rust
62 lines
1.4 KiB
Rust
|
use askama::Template;
|
||
|
use axum::{
|
||
|
Router,
|
||
|
http::{StatusCode, header},
|
||
|
response::{Html, IntoResponse, Response},
|
||
|
routing::get,
|
||
|
};
|
||
|
|
||
|
pub fn build<S>() -> Router<()> { Router::new().route("/", get(index_handler)) }
|
||
|
|
||
|
async fn index_handler() -> Result<impl IntoResponse, WebError> {
|
||
|
#[derive(Debug, Template)]
|
||
|
#[template(path = "index.html.j2")]
|
||
|
struct Tmpl<'a> {
|
||
|
nonce: &'a str,
|
||
|
}
|
||
|
let nonce = rand::random::<u64>().to_string();
|
||
|
|
||
|
let template = Tmpl { nonce: &nonce };
|
||
|
Ok((
|
||
|
[(header::CONTENT_SECURITY_POLICY, format!("default-src 'none' 'nonce-{nonce}';"))],
|
||
|
Html(template.render()?),
|
||
|
))
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, thiserror::Error)]
|
||
|
enum WebError {
|
||
|
#[error("Failed to render template: {0}")]
|
||
|
Render(#[from] askama::Error),
|
||
|
}
|
||
|
|
||
|
impl IntoResponse for WebError {
|
||
|
fn into_response(self) -> Response {
|
||
|
#[derive(Debug, Template)]
|
||
|
#[template(path = "error.html.j2")]
|
||
|
struct Tmpl<'a> {
|
||
|
nonce: &'a str,
|
||
|
err: WebError,
|
||
|
}
|
||
|
|
||
|
let nonce = rand::random::<u64>().to_string();
|
||
|
|
||
|
let status = match &self {
|
||
|
| Self::Render(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||
|
};
|
||
|
let tmpl = Tmpl { nonce: &nonce, err: self };
|
||
|
if let Ok(body) = tmpl.render() {
|
||
|
(
|
||
|
status,
|
||
|
[(
|
||
|
header::CONTENT_SECURITY_POLICY,
|
||
|
format!("default-src 'none' 'nonce-{nonce}';"),
|
||
|
)],
|
||
|
Html(body),
|
||
|
)
|
||
|
.into_response()
|
||
|
} else {
|
||
|
(status, "Something went wrong").into_response()
|
||
|
}
|
||
|
}
|
||
|
}
|