diff options
author | 53hornet <atc@53hor.net> | 2021-08-28 22:18:45 -0400 |
---|---|---|
committer | 53hornet <atc@53hor.net> | 2021-08-28 22:18:45 -0400 |
commit | 1313ba8a5845cc361495fbc8b197f35336e02a16 (patch) | |
tree | c4f7b8ea83b35dd959ab2ffa8616a4c9ea08dd33 /src | |
parent | 7494b946920bf93fa12ac1fc71a07b1165a203e2 (diff) | |
download | carpentertutoring-1313ba8a5845cc361495fbc8b197f35336e02a16.tar.xz carpentertutoring-1313ba8a5845cc361495fbc8b197f35336e02a16.zip |
feat(evals): moved from PHP to Rust+Warpevals
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 56 |
1 files changed, 43 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs index cbabc10..5359661 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,7 @@ use handlebars::Handlebars; use serde::Serialize; -use serde_json::json; use std::sync::Arc; -use std::time::SystemTime; -use warp::{Filter, Rejection}; +use warp::{http::Uri, Filter, Rejection}; #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { @@ -11,10 +9,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { let mut hbs = Handlebars::new(); hbs.register_template_file("base", "templates/base.hbs")?; hbs.register_template_file("index", "templates/index.hbs")?; + hbs.register_template_file("policies", "templates/policies.hbs")?; + hbs.register_template_file("about", "templates/about.hbs")?; let hbs = Arc::new(hbs); // Build routes let routes = index_filter(hbs.clone()) + .or(template_filter(hbs.clone())) + .or(redirect_static_ext()) .or(evals_list()) .or(warp::fs::dir("static")); @@ -30,7 +32,44 @@ fn index_filter( ) -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone { warp::get() .and(warp::path::end()) - .map(move || render("index", hbs.clone(), json!({"year", year().unwrap()}))) + .map(move || render("index", hbs.clone(), "")) +} + +fn template_filter( + hbs: Arc<Handlebars<'static>>, +) -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone { + warp::path!(String).and_then(move |name: String| { + let hbs = hbs.clone(); + async move { + if hbs.has_template(&name) { + Ok(render(&name, hbs, "")) + } else { + Err(warp::reject::not_found()) + } + } + }) +} + +fn redirect_static_ext() -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone { + warp::path!(String).and_then(move |path: String| async move { + if let Some(prefix) = path.strip_suffix(".php") { + return Ok(warp::redirect( + prefix + .parse::<Uri>() + .unwrap_or_else(|_| Uri::from_static("/")), + )); + } + + if let Some(prefix) = path.strip_suffix(".html") { + return Ok(warp::redirect( + prefix + .parse::<Uri>() + .unwrap_or_else(|_| Uri::from_static("/")), + )); + } + + Err(warp::reject::not_found()) + }) } fn evals_list() -> impl Filter<Extract = (&'static str,), Error = Rejection> + Clone { @@ -49,12 +88,3 @@ where .unwrap_or_else(|err| err.to_string()); warp::reply::html(render) } - -const UNIX_YEAR: u64 = 31556926; - -fn year() -> Result<u64, Box<dyn std::error::Error>> { - let date = SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH)? - .as_secs(); - Ok(date / UNIX_YEAR) -} |