From e964ec8f74b644d66ca166a7524adcc3a82709c9 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 14 May 2022 19:56:06 +0200 Subject: feat: Move code to blog - Move most logic of the blog to blog.rs, making it easier to test. - Now the file contains the creation date of the blog post to be parsed. - Add chrono to parse datetime, so later we can order by date. - Refactor gitlab pipeline, move `before_script` to a proper place. --- src/main.rs | 64 +++++-------------------------------------------------------- 1 file changed, 5 insertions(+), 59 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 63a5386..ed34713 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,60 +1,11 @@ -pub mod router; use std::convert::Infallible; -use rust_embed::RustEmbed; -use std::{env, str}; +use std::{env}; use std::net::SocketAddr; use hyper::{Body, Request, Response, Server}; use hyper::service::{make_service_fn, service_fn}; -use sailfish::TemplateOnce; -use ::router::Router; - -struct PostEntry { - title: String, - file: String, -} - -#[derive(TemplateOnce)] -#[template(path = "index.html")] -struct IndexTemplate { - posts: Vec, -} - -#[derive(TemplateOnce)] -#[template(path = "post.html")] -struct PostTemplate { - content: String, -} - -#[derive(RustEmbed)] -#[folder = "content/posts/"] -struct PostAsset; - - -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_entry(path: &String) -> PostEntry { - let sub_title = str::replace(path, "_", " "); - let title = str::replace(sub_title.as_str(), ".html", ""); - PostEntry { - title: String::from(title), - file: String::from(path), - } -} - -fn get_post_title() -> Vec { - PostAsset::iter() - .map(|e| format!("{}", e)) - .map(|e| get_post_entry(&e)) - .collect() -} +use macroblog::router::Router; +use macroblog::blog::{render_index_page, render_post_page}; async fn not_found() -> Result, Infallible> { @@ -67,10 +18,7 @@ async fn not_found() -> Result, Infallible> { async fn index() -> Result, Infallible> { - let files = get_post_title(); - let body = IndexTemplate { posts: files } - .render_once() - .unwrap(); + let body = render_index_page(); let resp: Response = Response::builder() .status(200) @@ -83,9 +31,7 @@ async fn index() -> Result, Infallible> { async fn post(path: &String) -> Result, Infallible> { - let body = PostTemplate { content: get_file_content(path) } - .render_once() - .unwrap(); + let body = render_post_page(path); let resp: Response = Response::builder() .status(200) -- cgit v1.2.3