Files
continuwuity/src/web/mod.rs
T

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

54 lines
1.1 KiB
Rust
Raw Normal View History

2025-04-25 02:47:48 +01:00
use askama::Template;
use axum::{
Router,
http::{HeaderValue, StatusCode, header},
2025-04-25 02:47:48 +01:00
response::{Html, IntoResponse, Response},
};
use conduwuit_service::state;
use tower_http::set_header::SetResponseHeaderLayer;
2025-04-25 02:47:48 +01:00
mod pages;
2025-04-25 02:47:48 +01:00
type State = state::State;
2026-02-13 09:58:35 -05:00
2025-04-25 02:47:48 +01:00
#[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 Error {
2025-04-25 02:47:48 +01:00
err: WebError,
}
let status = match &self {
| Self::Render(_) => StatusCode::INTERNAL_SERVER_ERROR,
};
let tmpl = Error { err: self };
2025-04-25 02:47:48 +01:00
if let Ok(body) = tmpl.render() {
(status, Html(body)).into_response()
2025-04-25 02:47:48 +01:00
} else {
(status, "Something went wrong").into_response()
}
}
}
pub fn build() -> Router<state::State> {
#[allow(clippy::wildcard_imports)]
use pages::*;
Router::new()
.merge(index::build())
.merge(resources::build())
.layer(SetResponseHeaderLayer::if_not_present(
header::CONTENT_SECURITY_POLICY,
HeaderValue::from_static("default-src 'self'"),
))
}