1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-07-27 18:28:31 +00:00
continuwuity/src/web/mod.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
1.7 KiB
Rust
Raw Permalink Normal View History

2025-04-25 02:47:48 +01:00
use askama::Template;
use axum::{
Router,
extract::State,
2025-04-25 02:47:48 +01:00
http::{StatusCode, header},
response::{Html, IntoResponse, Response},
routing::get,
};
use conduwuit_build_metadata::{GIT_REMOTE_COMMIT_URL, GIT_REMOTE_WEB_URL, version_tag};
use conduwuit_service::state;
2025-04-25 02:47:48 +01:00
pub fn build() -> Router<state::State> {
let router = Router::<state::State>::new();
router.route("/", get(index_handler))
}
2025-04-25 02:47:48 +01:00
async fn index_handler(
State(services): State<state::State>,
) -> Result<impl IntoResponse, WebError> {
2025-04-25 02:47:48 +01:00
#[derive(Debug, Template)]
#[template(path = "index.html.j2")]
struct Tmpl<'a> {
nonce: &'a str,
server_name: &'a str,
2025-04-25 02:47:48 +01:00
}
let nonce = rand::random::<u64>().to_string();
let template = Tmpl {
nonce: &nonce,
server_name: services.config.server_name.as_str(),
};
2025-04-25 02:47:48 +01:00
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()
}
}
}