Skip to main content

bobashare_web/views/
mod.rs

1//! Frontend views (as opposed to REST API)
2
3use std::path::Path;
4
5use askama::Template;
6use axum::{
7    http,
8    response::{Html, IntoResponse, Response},
9    routing::get,
10    Router,
11};
12use chrono::TimeDelta;
13use http::header::{HeaderName, HeaderValue};
14use hyper::StatusCode;
15use tower_http::set_header::SetResponseHeaderLayer;
16use tracing::{event, Level};
17use url::Url;
18
19use crate::AppState;
20
21pub mod about;
22pub mod display;
23pub mod filters;
24pub mod upload;
25
26mod prelude {
27    pub use super::CurrentNavigation;
28}
29
30// 's is for &AppState
31// TODO: should this be Copy
32#[derive(Debug, Clone)]
33pub struct TemplateState<'s> {
34    version: &'static str,
35    base_url: &'s Url,
36    max_file_size: u64,
37    max_expiry: Option<TimeDelta>,
38    extra_footer_text: Option<&'s str>,
39    about_page: Option<&'s Path>,
40
41    // None if the current page is not a navbar item
42    current_navigation: Option<CurrentNavigation>,
43}
44impl<'s> From<&'s AppState> for TemplateState<'s> {
45    fn from(state: &'s AppState) -> Self {
46        Self {
47            version: env!("CARGO_PKG_VERSION"),
48            base_url: &state.base_url,
49            max_file_size: state.max_file_size,
50            max_expiry: state.max_expiry,
51            extra_footer_text: state.extra_footer_text.as_deref(),
52            about_page: state.about_page.as_deref(),
53            current_navigation: None, // will be set to Some in individual handlers
54        }
55    }
56}
57
58// which page is current navigated to, for navbar formatting
59#[derive(Debug, Clone, Copy)]
60#[non_exhaustive]
61pub enum CurrentNavigation {
62    Upload,
63    Paste,
64    About,
65}
66
67#[derive(Template)]
68#[template(path = "error.html.jinja", blocks = ["title"])]
69pub struct ErrorTemplate<'s> {
70    pub state: TemplateState<'s>,
71    pub code: StatusCode,
72    pub message: String,
73}
74
75pub struct ErrorResponse(Response);
76impl From<ErrorTemplate<'_>> for ErrorResponse {
77    fn from(tmpl: ErrorTemplate) -> Self {
78        let error_msg = &tmpl.message;
79        match tmpl.render() {
80            Ok(s) => {
81                let status_num = tmpl.code.as_u16();
82                if tmpl.code.is_server_error() {
83                    event!(Level::ERROR, status = status_num, error_msg);
84                } else if tmpl.code.is_client_error() {
85                    event!(Level::WARN, status = status_num, error_msg);
86                } else {
87                    event!(Level::INFO, status = status_num, error_msg);
88                }
89                Self((tmpl.code, Html(s)).into_response())
90            }
91            Err(e) => {
92                let status = tmpl.code.as_u16();
93                event!(Level::ERROR, status, error_msg, render_error = ?e, "error rendering error page template, so HTTP 500 returned:");
94                Self(
95                    (
96                        StatusCode::INTERNAL_SERVER_ERROR,
97                        format!("internal error rendering error page template: {:?}", e),
98                    )
99                        .into_response(),
100                )
101            }
102        }
103    }
104}
105impl From<askama::Error> for ErrorResponse {
106    fn from(err: askama::Error) -> Self {
107        event!(Level::ERROR, render_error = ?err, "error rendering template");
108        Self(
109            (
110                StatusCode::INTERNAL_SERVER_ERROR,
111                format!("internal error rendering template: {:?}", err),
112            )
113                .into_response(),
114        )
115    }
116}
117impl IntoResponse for ErrorResponse {
118    fn into_response(self) -> axum::response::Response {
119        self.0
120    }
121}
122
123// The ErrorResponse is actually the same size as Response, and there's no
124// consumer of this api that would be "infected" by this large error value, so
125// this lint isn't necessary.
126//
127// See https://github.com/rust-lang/rust-clippy/issues/10211
128//
129// This is changed from expect to allow as the false positive doesn't appear on
130// arm targets.
131#[allow(clippy::result_large_err)]
132pub(crate) fn render_template<T: askama::Template>(tmpl: T) -> Result<Response, ErrorResponse> {
133    let rendered = tmpl.render()?;
134    Ok((StatusCode::OK, Html(rendered)).into_response())
135}
136
137pub fn router() -> Router<&'static AppState> {
138    let x_robots_tag_no_index = SetResponseHeaderLayer::overriding(
139        HeaderName::from_static("x-robots-tag"),
140        HeaderValue::from_static("noindex"),
141    );
142    Router::new()
143        .route("/", get(upload::upload))
144        .route("/paste/", get(upload::paste))
145        .route("/about/", get(about::about))
146        .route(
147            "/{id}",
148            get(display::display).layer(x_robots_tag_no_index.clone()),
149        )
150        .route(
151            "/raw/{id}",
152            get(display::raw).layer(x_robots_tag_no_index.clone()),
153        )
154}