From 696ae495aca701c3431710e5dfc03e15aba2f74e Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 17 May 2021 04:12:48 +0300 Subject: Refactoring, easter egg, healthcheck endpoint, support for rel= indieweb APIs and preparation for onboarding --- src/frontend/mod.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) (limited to 'src/frontend/mod.rs') diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index aaaa2b2..891e944 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -1,4 +1,4 @@ -use serde::Deserialize; +use serde::{Serialize, Deserialize}; use tide::{Request, Response, Result, StatusCode, Next}; use log::{info,error}; use crate::ApplicationState; @@ -11,6 +11,7 @@ mod templates { use http_types::StatusCode; use ellipse::Ellipse; use chrono; + use super::IndiewebEndpoints; /// Return a pretty location specifier from a geo: URI. fn decode_geo_uri(uri: &str) -> String { @@ -30,7 +31,7 @@ mod templates { } markup::define! { - Template<'a>(title: &'a str, content: String) { + Template<'a>(title: &'a str, endpoints: IndiewebEndpoints, content: String) { @markup::doctype() html { head { @@ -38,6 +39,10 @@ mod templates { link[rel="preconnect", href="https://fonts.gstatic.com"]; link[rel="stylesheet", href="/static/style.css"]; meta[name="viewport", content="initial-scale=1, width=device-width"]; + // TODO: link rel= for common IndieWeb APIs: webmention, microsub + link[rel="micropub", href="/micropub"]; // Static, because it's built into the server itself + link[rel="authorization_endpoint", href=&endpoints.authorization_endpoint]; + link[rel="token_endpoint", href=&endpoints.token_endpoint]; } body { nav#headerbar { @@ -379,6 +384,14 @@ mod templates { use templates::{Template,ErrorPage,MainPage}; +#[derive(Clone, Serialize, Deserialize)] +pub struct IndiewebEndpoints { + authorization_endpoint: String, + token_endpoint: String, + webmention: Option, + microsub: Option +} + #[derive(Deserialize)] struct QueryParams { after: Option @@ -437,9 +450,16 @@ async fn get_post_from_database(db: &S, url: &str, after: Option(_: Request>) -> Result { + Err(FrontendError::with_code(StatusCode::ImATeapot, "Someone asked this website to brew them some coffee..."))?; + return Ok(Response::builder(500).build()) // unreachable +} + pub async fn mainpage(req: Request>) -> Result { let backend = &req.state().storage; let query = req.query::()?; + let authorization_endpoint = req.state().authorization_endpoint.to_string(); + let token_endpoint = req.state().token_endpoint.to_string(); let user: Option = None; #[cfg(any(not(debug_assertions), test))] @@ -470,6 +490,10 @@ pub async fn mainpage(req: Request>) -> Result { .content_type("text/html; charset=utf-8") .body(Template { title: &format!("{} - Main page", url.host().unwrap().to_string()), + endpoints: IndiewebEndpoints { + authorization_endpoint, token_endpoint, + webmention: None, microsub: None + }, content: MainPage { feed: &feed?, card: &card? @@ -481,6 +505,8 @@ pub async fn mainpage(req: Request>) -> Result { pub async fn render_post(req: Request>) -> Result { let query = req.query::()?; + let authorization_endpoint = req.state().authorization_endpoint.to_string(); + let token_endpoint = req.state().token_endpoint.to_string(); let user: Option = None; #[cfg(any(not(debug_assertions), test))] @@ -501,6 +527,10 @@ pub async fn render_post(req: Request>) -> Resul .content_type("text/html; charset=utf-8") .body(Template { title: post["properties"]["name"][0].as_str().unwrap_or(&format!("Note at {}", url.host().unwrap().to_string())), + endpoints: IndiewebEndpoints { + authorization_endpoint, token_endpoint, + webmention: None, microsub: None + }, content: template }.to_string() ).build()) @@ -513,6 +543,8 @@ impl tide::Middleware> for ErrorHandlerMiddleware where S: crate::database::Storage { async fn handle(&self, request: Request>, next: Next<'_, ApplicationState>) -> Result { + let authorization_endpoint = request.state().authorization_endpoint.to_string(); + let token_endpoint = request.state().token_endpoint.to_string(); let mut res = next.run(request).await; let mut code: Option = None; if let Some(err) = res.downcast_error::() { @@ -527,7 +559,14 @@ impl tide::Middleware> for ErrorHandlerMiddleware where if let Some(code) = code { res.set_status(code); res.set_content_type("text/html; charset=utf-8"); - res.set_body(Template { title: "Error", content: ErrorPage { code }.to_string()}.to_string()); + res.set_body(Template { + title: "Error", + endpoints: IndiewebEndpoints { + authorization_endpoint, token_endpoint, + webmention: None, microsub: None + }, + content: ErrorPage { code }.to_string() + }.to_string()); } Ok(res) } -- cgit 1.4.1