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/main.rs | |
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/main.rs')
-rw-r--r-- | src/main.rs | 64 |
1 files changed, 5 insertions, 59 deletions
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) |