From 0a3c6fb23f10da3edc251d687a1f58386c791dc6 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sun, 2 Aug 2020 03:13:01 +0000 Subject: [PATCH] cleanup http rendering --- site/src/http.rs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/site/src/http.rs b/site/src/http.rs index 97a2bda..07530c5 100644 --- a/site/src/http.rs +++ b/site/src/http.rs @@ -1,10 +1,10 @@ -use crate::templates::{self, Html, RenderRucte, ToHtml, statics::StaticFile}; +use crate::templates::{self, statics::StaticFile, Html, RenderRucte, ToHtml}; use maj::server::Handler; use maj::{gemini, server::Request as GemRequest, StatusCode}; use std::io::Write; use std::sync::Arc; use url::Url; -use warp::{filters::path::FullPath, http::Response, Filter, Rejection, Reply,path}; +use warp::{filters::path::FullPath, http::Response, path, Filter, Rejection, Reply}; const HOST: &'static str = "cetacean.club"; // XXX(cadey): HACK const APPLICATION_NAME: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); @@ -34,6 +34,21 @@ async fn route(args: (FullPath, Arc)) -> Result { + let uu = Url::parse(&resp.meta).expect("url parsing to work"); + log::info!("uu: {}", uu.to_string()); + Response::builder() + .status(warp::http::StatusCode::PERMANENT_REDIRECT) + .header("Location", uu.path()) + .html(|o| { + templates::error_html( + o, + u.to_string(), + format!("forwarding you to {}", uu.path()), + ) + }) + } + _ => Response::builder() .status(warp::http::StatusCode::INTERNAL_SERVER_ERROR) .html(|o| templates::error_html(o, u.to_string(), resp.meta)), @@ -59,7 +74,7 @@ fn gemtext_to_html(inp: Vec) -> (String, impl ToHtml) { Text(body) => write!(buf, "{}\n
", body).unwrap(), Link { to, name } => write!( buf, - r#"{}"#, + r#"{}
"#, to, name.as_ref().or(Some(&to.to_string())).unwrap() ) @@ -77,10 +92,10 @@ pub fn run(h: Arc, port: u16) { smol::run(async { let h = h.clone(); let handler = warp::path::full() - .map(move |path: FullPath| (path, h.clone())).and_then(route); + .map(move |path: FullPath| (path, h.clone())) + .and_then(route); let statics = path("static").and(path::param()).and_then(static_file); - let site = statics.or(handler) - .with(warp::log(APPLICATION_NAME)); + let site = statics.or(handler).with(warp::log(APPLICATION_NAME)); warp::serve(site).run(([0, 0, 0, 0], port)).await; }); } @@ -91,10 +106,10 @@ pub fn run(h: Arc, port: u16) { async fn static_file(name: String) -> Result { if let Some(data) = StaticFile::get(&name) { Ok(Response::builder() - .status(warp::http::StatusCode::OK) - .header("content-type", data.mime.as_ref()) - // TODO .header("expires", _far_expires) - .body(data.content)) + .status(warp::http::StatusCode::OK) + .header("content-type", data.mime.as_ref()) + // TODO .header("expires", _far_expires) + .body(data.content)) } else { println!("Static file {} not found", name); Err(warp::reject::not_found())