From ea058a851098bf81cb645249e02d26a8c253db90 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 8 May 2022 21:47:45 +0200 Subject: ref: Add embded rust and router - Use embed rust to load and resolve file from `content/post` folder, so the whole process is a bit more dynamic. - Add router to to resolve the path. It is the first step to try to get the code a bit cleaner. --- src/main.rs | 85 ++++++++++++++++++++++++++++++----------------------------- src/router.rs | 27 +++++++++++++++++++ 2 files changed, 70 insertions(+), 42 deletions(-) create mode 100644 src/router.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 1fb0e90..cabff0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,54 +1,59 @@ +pub mod router; + use std::convert::Infallible; -use std::{include_str, env}; +use rust_embed::RustEmbed; +use std::{env, str}; use std::net::SocketAddr; use hyper::{Body, Request, Response, Server}; use hyper::service::{make_service_fn, service_fn}; -use regex::Regex; use sailfish::TemplateOnce; - +use ::router::Router; #[derive(TemplateOnce)] #[template(path = "index.html")] struct IndexTemplate { - posts: [Post; 2], + posts: Vec, } #[derive(TemplateOnce)] #[template(path = "post.html")] struct PostTemplate { - content: &'static str, + content: String, } +#[derive(RustEmbed)] +#[folder = "content/posts/"] +struct PostAsset; + -struct Post( - u8, - &'static str, - &'static str, -); - -const POSTS: [Post; 2] = [ - Post( - 0, - "K8S private gitlab registry using podman", - include_str!("../assets/post1.html"), - ), - Post( - 1, - "Automation part 1", - include_str!("../assets/post2.html"), - ), -]; - -fn get_path_id(path: &str) -> usize { - let re = Regex::new(r"(?P\d+)").unwrap(); - let caps = re.captures(path).unwrap(); - let id = &caps["id"]; - - return id.parse::().unwrap(); +fn get_file_content(path: &str) -> String { + let buffer = PostAsset::get(path) + .unwrap() + .data + .into_owned(); + + return String::from_utf8(buffer).unwrap(); } +fn get_post_title() -> Vec { + PostAsset::iter() + .map(|e| format!("{}", e)) + .collect() +} + + +async fn not_found() -> Result, Infallible> { + let resp: Response = Response::builder() + .status(404) + .body("Not Found".into()) + .unwrap(); + Ok(resp) +} + + async fn index() -> Result, Infallible> { - let body = IndexTemplate { posts: POSTS } + let files = get_post_title(); + let body = IndexTemplate { posts: files } .render_once() .unwrap(); @@ -62,8 +67,8 @@ async fn index() -> Result, Infallible> { } -async fn post(index: usize) -> Result, Infallible> { - let body = PostTemplate { content: POSTS[index].2 } +async fn post(path: &String) -> Result, Infallible> { + let body = PostTemplate { content: get_file_content(path) } .render_once() .unwrap(); @@ -78,16 +83,12 @@ async fn post(index: usize) -> Result, Infallible> { async fn request(req: Request) -> Result, Infallible> { let path = req.uri().path(); - if path == "/favicon.ico" { - return index().await - } - if path != "/" { - let index = get_path_id(&path); - return post(index).await; - } - - return index().await; + match Router::new(path) { + Router::Index => index().await, + Router::Post { page } => post(&page).await, + Router::NotFound => not_found().await + } } diff --git a/src/router.rs b/src/router.rs new file mode 100644 index 0000000..0bba091 --- /dev/null +++ b/src/router.rs @@ -0,0 +1,27 @@ +use regex::{Regex}; + +const ACTION_REGEX: &str = r"/{0,1}(?P\w*)/(?P.+)"; + +pub enum Router { + NotFound, + Index, + Post { page: String }, +} + +impl Router { + pub fn new(path: &str) -> Router { + let re = Regex::new(ACTION_REGEX).unwrap(); + let caps = re.captures(path); + let action = match caps { + Some(ref value) => &value["action"], + None => "index" + }; + + match action { + "posts" => Router::Post { page: caps.unwrap()["id"].to_string() }, + "index" => Router::Index, + _ => Router::NotFound + } + } +} + -- cgit v1.2.3