macroblog.rs @ a16e8a21bb83325f8e40c13ef2d052393e2f6489

feat: Add early blog implementation

It is just a really crappy implementation to get it running until a
figure the pieces of the project out.
diff --git a/Cargo.lock b/Cargo.lock
index d51075fd2fc1d0216c8a83c4629de538bf0762b3..9f33acf3c98818217ece2468b7dbc88e4f9ab63a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,6 +3,15 @@ # It is not intended for manual editing.
 version = 3
 
 [[package]]
+name = "aho-corasick"
+version = "0.7.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
 name = "autocfg"
 version = "1.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -242,6 +251,7 @@ name = "macroblog"
 version = "0.1.0"
 dependencies = [
  "hyper",
+ "regex",
  "sailfish",
  "tokio",
 ]
@@ -361,6 +371,23 @@ checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42"
 dependencies = [
  "bitflags",
 ]
+
+[[package]]
+name = "regex"
+version = "1.5.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
 
 [[package]]
 name = "ryu"
diff --git a/Cargo.toml b/Cargo.toml
index ecaba6d87d613106a7d5a542fd9e75529569d9f9..093a4d3252d590fdd23a2c4a3834b9bc0e143381 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,6 +7,7 @@ [dependencies]
 sailfish = "0.4.0"
 hyper = { version = "0.14", features = ["full"] }
 tokio = { version = "1", features = ["full"] }
+regex = "1.5"
 
 [profile.release]
 opt-level = 'z'
diff --git a/assets/pico.min.css b/templates/pico.min.css
rename from assets/pico.min.css
rename to templates/pico.min.css
diff --git a/assets/post1.html b/assets/post1.html
new file mode 100644
index 0000000000000000000000000000000000000000..6eafb918836bf0d77fb26a94465247e9a58dbb6f
--- /dev/null
+++ b/assets/post1.html
@@ -0,0 +1,9 @@
+<section>
+    <h2>Title</h2>
+    <p>
+        Nice content
+        <code>
+            My code here
+        </code>
+    </p>
+</section>
diff --git a/assets/post2.html b/assets/post2.html
new file mode 100644
index 0000000000000000000000000000000000000000..077734ef45473f4c25aa2d5cf58446966e6a278f
--- /dev/null
+++ b/assets/post2.html
@@ -0,0 +1,9 @@
+<section>
+    <h2>Title</h2>
+    <p>
+        Nice content 2
+        <code>
+            My code here
+        </code>
+    </p>
+</section>
diff --git a/src/main.rs b/src/main.rs
index 0c3565b8c097ceeabcf97b10987859bfa6e793b5..92f51a7e309faf481480fc0f6cb9329fe7978a9e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,38 +3,100 @@ use std::{include_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;
 
 
 #[derive(TemplateOnce)]
 #[template(path = "index.html")]
 struct IndexTemplate {
-    pico: &'static str,
+    posts: [Post; 2],
 }
 
-const PICO_CSS: &str = include_str!("../assets/pico.min.css");
+#[derive(TemplateOnce)]
+#[template(path = "post.html")]
+struct PostTemplate {
+    content: &'static str,
+}
+
+
+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"),
+    ),
+];
 
-async fn hello_world(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
-    let body = IndexTemplate { pico: PICO_CSS }
+fn get_path_id(path: &str) -> usize {
+    let re = Regex::new(r"(?P<id>\d+)").unwrap();
+    let caps = re.captures(path).unwrap();
+    let id = &caps["id"];
+
+    return id.parse::<usize>().unwrap();
+}
+
+async fn index() -> Result<Response<Body>, Infallible> {
+    let body = IndexTemplate { posts: POSTS }
+        .render_once()
+        .unwrap();
+
+    let resp: Response<Body> = Response::builder()
+        .status(200)
+        .header("posts-type", "text/html")
+        .body(body.into())
+        .unwrap();
+
+    Ok(resp)
+}
+
+
+async fn post(index: usize) -> Result<Response<Body>, Infallible> {
+    let body = PostTemplate { content: POSTS[index].2 }
         .render_once()
         .unwrap();
 
     let resp: Response<Body> = Response::builder()
         .status(200)
-        .header("content-type", "text/html")
+        .header("posts-type", "text/html")
         .body(body.into())
         .unwrap();
 
     Ok(resp)
 }
 
+async fn request(req: Request<Body>) -> Result<Response<Body>, 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;
+}
+
 
 #[tokio::main]
 async fn main() {
     let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
 
     let make_svc = make_service_fn(|_conn| async {
-        Ok::<_, Infallible>(service_fn(hello_world))
+        Ok::<_, Infallible>(service_fn(request))
     });
 
     let server = Server::bind(&addr).serve(make_svc);
diff --git a/templates/head.html b/templates/head.html
new file mode 100644
index 0000000000000000000000000000000000000000..cc1bdb859110ca636673fbed766097ab0f81dae7
--- /dev/null
+++ b/templates/head.html
@@ -0,0 +1,6 @@
+<meta charset="UTF-8">
+<meta name="viewport" content="width=device-width, initial-scale=1">
+<title>Yet Another Blog</title>
+<style>
+    <% include!("pico.min.css"); %>
+</style>
diff --git a/templates/header.html b/templates/header.html
new file mode 100644
index 0000000000000000000000000000000000000000..e55cebef599eded1db5be972ddb071b808191601
--- /dev/null
+++ b/templates/header.html
@@ -0,0 +1,18 @@
+<header class="container">
+    <nav class="container-fluid">
+        <ul>
+            <li><strong>Yet Another Blog</strong></li>
+        </ul>
+        <ul>
+            <li><a class="secondary">Posts</a></li>
+            <li><a class="secondary">Projects</a></li>
+            <li><a class="secondary">Resume</a></li>
+        </ul>
+    </nav>
+    <hgroup>
+        <h2>
+            A gathering of information about some things I do on my spare time. You can find me on gitlab , twitter and
+            linkedin .
+        </h2>
+    </hgroup>
+</header>
diff --git a/templates/index.html b/templates/index.html
index 90826de8e10534c0ce4fd7e9d947d98225630fe8..3558e0691376bc93ee0cc5e9d90ff8112e09802b 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -1,48 +1,17 @@
 <!DOCTYPE html>
 <html data-theme="light" lang="en">
 <head>
-    <meta charset="UTF-8">
-    <meta name="viewport" content="width=device-width, initial-scale=1">
-    <title>Yet Another Blog</title>
-    <style>
-        <%- pico %>
-    </style>
+    <% include!("head.html"); %>
 </head>
 <body>
-<header class="container">
-    <nav class="container-fluid">
-        <ul>
-            <li><strong>Yet Another Blog</strong></li>
-        </ul>
-        <ul>
-            <li><a class="secondary">Posts</a></li>
-            <li><a class="secondary">Projects</a></li>
-            <li><a class="secondary">Resume</a></li>
-        </ul>
-    </nav>
-    <hgroup>
-        <h2>
-            A gathering of information about some things I do on my spare time. You can find me on gitlab , twitter and
-            linkedin .
-        </h2>
-    </hgroup>
-</header>
-
+<% include!("header.html"); %>
 <main class="container">
     <section>
         <ul>
-            <li><a>K8S private gitlab registry using podman</a></li>
-            <li><a>K8S private gitlab registry using podman</a></li>
+        <% for p in &posts { %>
+            <li><a href="/post/<%- p.0 %>"><%- p.1 %></a></li>
+        <% } %>
         </ul>
-    </section>
-    <section>
-        <h2>Title</h2>
-        <p>
-            Nice content
-            <code>
-                My code here
-            </code>
-        </p>
     </section>
 </main>
 </body>
diff --git a/templates/post.html b/templates/post.html
new file mode 100644
index 0000000000000000000000000000000000000000..68211c1fd5b22ac6a6faa7f6dfa6b3994e52c2aa
--- /dev/null
+++ b/templates/post.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html data-theme="light" lang="en">
+<head>
+  <% include!("head.html"); %>
+</head>
+<body>
+<% include!("simple_header.html"); %>
+<main class="container">
+  <%- content %>
+</section>
+</main>
+</body>
+</html>
diff --git a/templates/simple_header.html b/templates/simple_header.html
new file mode 100644
index 0000000000000000000000000000000000000000..0132334d05dc3d402fe303fc7b4f483f53e0e7fc
--- /dev/null
+++ b/templates/simple_header.html
@@ -0,0 +1,12 @@
+<header class="container">
+    <nav class="container-fluid">
+        <ul>
+            <li><strong><a href="/">Yet Another Blog</a></strong></li>
+        </ul>
+        <ul>
+            <li><a class="secondary">Posts</a></li>
+            <li><a class="secondary">Projects</a></li>
+            <li><a class="secondary">Resume</a></li>
+        </ul>
+    </nav>
+</header>