diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2022-05-14 19:56:06 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2022-05-14 19:56:06 +0200 |
commit | e964ec8f74b644d66ca166a7524adcc3a82709c9 (patch) | |
tree | b1a87d6a1333ae87fda5121b1993a050079afa43 /src | |
parent | 16ef06ffb363b6304d0d5269b30dedbdd07288df (diff) | |
download | macroblog.rs-e964ec8f74b644d66ca166a7524adcc3a82709c9.tar.gz macroblog.rs-e964ec8f74b644d66ca166a7524adcc3a82709c9.tar.bz2 macroblog.rs-e964ec8f74b644d66ca166a7524adcc3a82709c9.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/blog.rs | 74 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 64 |
3 files changed, 81 insertions, 59 deletions
diff --git a/src/blog.rs b/src/blog.rs new file mode 100644 index 0000000..6c190a9 --- /dev/null +++ b/src/blog.rs @@ -0,0 +1,74 @@ +use rust_embed::RustEmbed; +use sailfish::TemplateOnce; +use chrono::NaiveDate; +use regex::{Regex}; +use std::str; + +const BLOG_REGEX: &str = r"(?P<date>[\d]{4}-[\d]{2}-[\d]{2})(?P<title>[a-zA-Z0-9_]*)"; + +#[derive(RustEmbed)] +#[folder = "content/posts/"] +struct PostAsset; + + +#[derive(TemplateOnce)] +#[template(path = "index.html")] +struct IndexTemplate { + posts: Vec<BlogEntry>, +} + +#[derive(TemplateOnce)] +#[template(path = "post.html")] +struct PostTemplate { + content: String, +} + +pub struct BlogEntry { + pub title: String, + pub datetime: NaiveDate, + pub file: String, +} + +impl BlogEntry { + pub fn new(path: &String) -> BlogEntry { + let re = Regex::new(BLOG_REGEX).unwrap(); + let caps = re.captures(path).unwrap(); + let date = &caps["date"]; + let title = str::replace(&caps["title"], "_", " "); + + BlogEntry { + title: String::from(title), + file: String::from(path), + datetime: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap() + } + } + + pub fn read_assets() -> Vec<BlogEntry> { + PostAsset::iter() + .map(|e| format!("{}", e)) + .map(|e| BlogEntry::new(&e)) + .collect() + } +} + +fn get_file_content(path: &str) -> String { + let buffer = PostAsset::get(path) + .unwrap() + .data + .into_owned(); + + return String::from_utf8(buffer).unwrap(); +} + + +pub fn render_post_page(path: &String) -> String { + PostTemplate { content: get_file_content(path) } + .render_once() + .unwrap() +} + +pub fn render_index_page() -> String { + IndexTemplate { posts: BlogEntry::read_assets() } + .render_once() + .unwrap() +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..0c69888 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod blog; +pub mod router; 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<PostEntry>, -} - -#[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<PostEntry> { - 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<Response<Body>, Infallible> { @@ -67,10 +18,7 @@ async fn not_found() -> Result<Response<Body>, Infallible> { async fn index() -> Result<Response<Body>, Infallible> { - let files = get_post_title(); - let body = IndexTemplate { posts: files } - .render_once() - .unwrap(); + let body = render_index_page(); let resp: Response<Body> = Response::builder() .status(200) @@ -83,9 +31,7 @@ async fn index() -> Result<Response<Body>, Infallible> { async fn post(path: &String) -> Result<Response<Body>, Infallible> { - let body = PostTemplate { content: get_file_content(path) } - .render_once() - .unwrap(); + let body = render_post_page(path); let resp: Response<Body> = Response::builder() .status(200) |