cleanup http rendering
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Cadey Ratio 2020-08-02 03:13:01 +00:00
parent c6f15577bb
commit 0a3c6fb23f
1 changed files with 25 additions and 10 deletions

View File

@ -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<crate::server::Handler>)) -> Result<impl Rep
}
}
StatusCode::PermanentRedirect => {
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<u8>) -> (String, impl ToHtml) {
Text(body) => write!(buf, "{}\n<br />", body).unwrap(),
Link { to, name } => write!(
buf,
r#"<a href="{}">{}</a>"#,
r#"<a href="{}">{}</a><br />"#,
to,
name.as_ref().or(Some(&to.to_string())).unwrap()
)
@ -77,10 +92,10 @@ pub fn run(h: Arc<crate::server::Handler>, 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<crate::server::Handler>, port: u16) {
async fn static_file(name: String) -> Result<impl Reply, Rejection> {
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())