macroblog.rs @ ea058a851098bf81cb645249e02d26a8c253db90

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.
diff --git a/Cargo.lock b/Cargo.lock
index 9f33acf3c98818217ece2468b7dbc88e4f9ab63a..b687dc4ba345533b859801322d8dd90f09f8eec1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -24,6 +24,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
 
 [[package]]
+name = "block-buffer"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
 name = "bytes"
 version = "1.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -34,6 +43,24 @@ name = "cfg-if"
 version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "cpufeatures"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "59a6001667ab124aebae2a495118e11d30984c3a653e99d86d58971708cf5e4b"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "digest"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
+dependencies = [
+ "generic-array",
+]
 
 [[package]]
 name = "filetime"
@@ -93,6 +120,16 @@  "pin-utils",
 ]
 
 [[package]]
+name = "generic-array"
+version = "0.14.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803"
+dependencies = [
+ "typenum",
+ "version_check",
+]
+
+[[package]]
 name = "h2"
 version = "0.3.13"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -252,6 +289,7 @@ version = "0.1.0"
 dependencies = [
  "hyper",
  "regex",
+ "rust-embed",
  "sailfish",
  "tokio",
 ]
@@ -309,6 +347,12 @@ name = "once_cell"
 version = "1.10.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
+
+[[package]]
+name = "opaque-debug"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
 
 [[package]]
 name = "parking_lot"
@@ -390,6 +434,40 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
 
 [[package]]
+name = "rust-embed"
+version = "6.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9a17e5ac65b318f397182ae94e532da0ba56b88dd1200b774715d36c4943b1c3"
+dependencies = [
+ "rust-embed-impl",
+ "rust-embed-utils",
+ "walkdir",
+]
+
+[[package]]
+name = "rust-embed-impl"
+version = "6.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "94e763e24ba2bf0c72bc6be883f967f794a019fafd1b86ba1daff9c91a7edd30"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "rust-embed-utils",
+ "syn",
+ "walkdir",
+]
+
+[[package]]
+name = "rust-embed-utils"
+version = "7.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "756feca3afcbb1487a1d01f4ecd94cf8ec98ea074c55a69e7136d29fb6166029"
+dependencies = [
+ "sha2",
+ "walkdir",
+]
+
+[[package]]
 name = "ryu"
 version = "1.0.9"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -434,6 +512,15 @@  "sailfish-compiler",
 ]
 
 [[package]]
+name = "same-file"
+version = "1.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
+dependencies = [
+ "winapi-util",
+]
+
+[[package]]
 name = "scopeguard"
 version = "1.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -460,6 +547,19 @@  "syn",
 ]
 
 [[package]]
+name = "sha2"
+version = "0.9.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800"
+dependencies = [
+ "block-buffer",
+ "cfg-if",
+ "cpufeatures",
+ "digest",
+ "opaque-debug",
+]
+
+[[package]]
 name = "signal-hook-registry"
 version = "1.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -600,6 +700,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
 
 [[package]]
+name = "typenum"
+version = "1.15.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
+
+[[package]]
 name = "unicode-xid"
 version = "0.2.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -612,6 +718,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
 
 [[package]]
+name = "walkdir"
+version = "2.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
+dependencies = [
+ "same-file",
+ "winapi",
+ "winapi-util",
+]
+
+[[package]]
 name = "want"
 version = "0.3.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -642,6 +759,15 @@ name = "winapi-i686-pc-windows-gnu"
 version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-util"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+dependencies = [
+ "winapi",
+]
 
 [[package]]
 name = "winapi-x86_64-pc-windows-gnu"
diff --git a/Cargo.toml b/Cargo.toml
index 093a4d3252d590fdd23a2c4a3834b9bc0e143381..de00d117e0465b66e6fd3f2616ffb608e3a6b7b6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,7 @@ sailfish = "0.4.0"
 hyper = { version = "0.14", features = ["full"] }
 tokio = { version = "1", features = ["full"] }
 regex = "1.5"
+rust-embed="6.4.0"
 
 [profile.release]
 opt-level = 'z'
@@ -15,3 +16,7 @@ lto = true
 codegen-units = 1
 panic = 'abort'
 strip = true
+
+[lib]
+name="router"
+path="src/router.rs"
diff --git a/assets/post1.html b/assets/post1.html
deleted file mode 100644
index 6eafb918836bf0d77fb26a94465247e9a58dbb6f..0000000000000000000000000000000000000000
--- a/assets/post1.html
+++ /dev/null
@@ -1,9 +0,0 @@
-<section>
-    <h2>Title</h2>
-    <p>
-        Nice content
-        <code>
-            My code here
-        </code>
-    </p>
-</section>
diff --git a/assets/post2.html b/assets/post2.html
deleted file mode 100644
index 077734ef45473f4c25aa2d5cf58446966e6a278f..0000000000000000000000000000000000000000
--- a/assets/post2.html
+++ /dev/null
@@ -1,9 +0,0 @@
-<section>
-    <h2>Title</h2>
-    <p>
-        Nice content 2
-        <code>
-            My code here
-        </code>
-    </p>
-</section>
diff --git a/content/posts/Friz.box_turned_off_DHCP.html b/content/posts/Friz.box_turned_off_DHCP.html
new file mode 100644
index 0000000000000000000000000000000000000000..569604b4a4169a0eca392de73071f6a436ded7b7
--- /dev/null
+++ b/content/posts/Friz.box_turned_off_DHCP.html
@@ -0,0 +1,18 @@
+<section>
+    <h2>Friz.box turned off DHCP</h2>
+    <p>
+        If you turned off your DHCP server follow these steps to connect to FritzBox settings.
+        <br/>
+    <ul>
+        <li> Set your computer IP to 170.254.1.2 and your mask to 255.255.0.0</li>
+        <li> Then go to 169.254.1.1, login and re-enable the DHCP server:</li>
+    </ul>
+    </p>
+    <p>
+        On gnome turn the wired connection off on again to apply the settings.
+    </p>
+    <p>
+        <strong>Note</strong>: why in the hell does FritzBox 7490 require a land-line telephone to be physically factory
+        reset?
+    </p>
+</section>
diff --git a/content/posts/K8S_private_gitlab_registry_using_podman.html b/content/posts/K8S_private_gitlab_registry_using_podman.html
new file mode 100644
index 0000000000000000000000000000000000000000..367275598b6ba9d73c5f6f44d10da9d16068d2ea
--- /dev/null
+++ b/content/posts/K8S_private_gitlab_registry_using_podman.html
@@ -0,0 +1,55 @@
+<section>
+    <h2>K8S private gitlab registry using podman</h2>
+    <p>
+        This is based on <a
+            href="https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/">Log in to
+        Docker Hub</a>. It is just a bit different to use podman.
+    </p>
+    <p>
+        First we should take a look at podman-login man page:
+    <pre>
+        <code>man podman login</code>
+    </pre>
+    </p>
+    <p>
+        It will give some valueable information like the location of auth.json file. Now we can login using podman:
+    <pre>
+        <code>podman login registry.gitlab.com</code>
+    </pre>
+    </p>
+    <p>Then check the <i> auth.json </i>file located at <i>${XDG_RUNTIME_DIR}/containers/auth.json</i> (as described
+        by the manual). It will contain your auth config:
+    <pre>
+        <code>
+{
+	"auths": {
+		"registry.gitlab.com": {
+			"auth": "..."
+		}
+	}
+}
+        </code>
+    </pre>
+    </p>
+    <p>
+        Now copy that file over to the server and register it in k8s with the following command:
+    <pre>
+        <code>
+kubectl create secret generic regcred \
+    --from-file=.dockerconfigjson=auth.json \
+    --type=kubernetes.io/dockerconfigjson
+
+        </code>
+    </pre>
+    </p>
+    <p>
+        Once you have created you can list by kubectl get secret:
+    <pre>
+        <code>
+NAME     TYPE                                  DATA   AGE
+regcred  kubernetes.io/dockerconfigjson        1      53s
+        </code>
+    </pre>
+    </p>
+
+</section>
diff --git a/src/main.rs b/src/main.rs
index 1fb0e9003272d0f3e5a537d5277227549600abee..cabff0e25c2cbc88ad464f30c968bdc5b0862441 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<String>,
 }
 
 #[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_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<String> {
+    PostAsset::iter()
+        .map(|e| format!("{}", e))
+        .collect()
+}
 
-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 not_found() -> Result<Response<Body>, Infallible> {
+    let resp: Response<Body> = Response::builder()
+        .status(404)
+        .body("Not Found".into())
+        .unwrap();
+    Ok(resp)
 }
+
 
 async fn index() -> Result<Response<Body>, Infallible> {
-    let body = IndexTemplate { posts: POSTS }
+    let files = get_post_title();
+    let body = IndexTemplate { posts: files }
         .render_once()
         .unwrap();
 
@@ -62,8 +67,8 @@     Ok(resp)
 }
 
 
-async fn post(index: usize) -> Result<Response<Body>, Infallible> {
-    let body = PostTemplate { content: POSTS[index].2 }
+async fn post(path: &String) -> Result<Response<Body>, Infallible> {
+    let body = PostTemplate { content: get_file_content(path) }
         .render_once()
         .unwrap();
 
@@ -78,16 +83,12 @@ }
 
 async fn request(req: Request<Body>) -> Result<Response<Body>, Infallible> {
     let path = req.uri().path();
-    if path == "/favicon.ico" {
-        return index().await
+
+    match Router::new(path) {
+        Router::Index => index().await,
+        Router::Post { page } => post(&page).await,
+        Router::NotFound => not_found().await
     }
-    if path != "/" {
-        let index = get_path_id(&path);
-        return post(index).await;
-    }
-
-
-    return index().await;
 }
 
 
diff --git a/src/router.rs b/src/router.rs
new file mode 100644
index 0000000000000000000000000000000000000000..0bba091cb8533672e794fd3b48c9dac9cdd2e6a0
--- /dev/null
+++ b/src/router.rs
@@ -0,0 +1,27 @@
+use regex::{Regex};
+
+const ACTION_REGEX: &str = r"/{0,1}(?P<action>\w*)/(?P<id>.+)";
+
+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
+        }
+    }
+}
+
diff --git a/templates/header.html b/templates/header.html
index e55cebef599eded1db5be972ddb071b808191601..1005e66b8875d2bf57e725bda2405dade9f6f61c 100644
--- a/templates/header.html
+++ b/templates/header.html
@@ -4,9 +4,7 @@         <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>
+            <li><a href="https://gitlab.com/gabrielgio/cv/-/raw/main/cv.pdf?inline=false" class="secondary">Resume</a></li>
         </ul>
     </nav>
     <hgroup>
diff --git a/templates/index.html b/templates/index.html
index 3558e0691376bc93ee0cc5e9d90ff8112e09802b..6d0c1399c2406964174131b8ce5adf1dc6a09cbc 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -9,7 +9,7 @@ <main class="container">
     <section>
         <ul>
         <% for p in &posts { %>
-            <li><a href="/post/<%- p.0 %>"><%- p.1 %></a></li>
+            <li><a href="/posts/<%- p %>"><%- p %></a></li>
         <% } %>
         </ul>
     </section>
diff --git a/templates/pico.min.css b/templates/pico.min.css
index 1179471453b3bb57f3935f37166f7f8a05655fb3..7713ba92ff1ed322eeebb8f2e994eeec4c0488c1 100644
--- a/templates/pico.min.css
+++ b/templates/pico.min.css
@@ -325,7 +325,7 @@ b, strong {
     font-weight: bolder
 }
 
-address, blockquote, dl, figure, form, ol, p, pre, table, ul {
+address, blockquote, dl, figure, form, ol, p, table, ul {
     margin-top: 0;
     margin-bottom: var(--typography-spacing-vertical);
     color: var(--color);
@@ -336,7 +336,7 @@ }
 
 [role=link], a {
     --color: var(--primary);
-    --background-color: transparent;
+    ckground-color: transparent;
     outline: 0;
     background-color: var(--background-color);
     color: var(--color);
@@ -413,7 +413,7 @@ h6 {
     --color: var(--h6-color)
 }
 
-:where(address,blockquote,dl,figure,form,ol,p,pre,table,ul) ~ :is(h1,h2,h3,h4,h5,h6) {
+:where(address,blockquote,dl,figure,form,ol,p,table,ul) ~ :is(h1,h2,h3,h4,h5,h6) {
     margin-top: var(--typography-spacing-vertical)
 }
 
@@ -454,7 +454,7 @@     margin: calc(var(--typography-spacing-vertical) * .25) 0 0
 }
 
 ul li {
-    list-style: none
+    list-style: square;
 }
 
 mark {
@@ -503,61 +503,22 @@ [type=color]::-webkit-color-swatch-wrapper {
     padding: 0
 }
 
-code, kbd, pre, samp {
+kbd,  samp {
     font-size: 0.875em;
     font-family: var(--font-family)
 }
 
-pre {
-    -ms-overflow-style: scrollbar;
-    overflow: auto
-}
-
-code, kbd, pre {
+kbd {
     border-radius: var(--border-radius);
-    background: var(--code-background-color);
+    background: var(--eode-background-color);
     color: var(--code-color);
     font-weight: var(--font-weight);
     line-height: initial
 }
 
-code, kbd {
+ kbd {
     display: inline-block;
     padding: 0.375rem 0.5rem
-}
-
-pre {
-    display: block;
-    margin-bottom: var(--spacing);
-    overflow-x: auto
-}
-
-pre > code {
-    display: block;
-    padding: var(--spacing);
-    background: 0 0;
-    font-size: 14px;
-    line-height: var(--line-height)
-}
-
-code b {
-    color: var(--code-tag-color);
-    font-weight: var(--font-weight)
-}
-
-code i {
-    color: var(--code-property-color);
-    font-style: normal
-}
-
-code u {
-    color: var(--code-value-color);
-    text-decoration: none
-}
-
-code em {
-    color: var(--code-comment-color);
-    font-style: normal
 }
 
 kbd {
diff --git a/templates/post.html b/templates/post.html
index 68211c1fd5b22ac6a6faa7f6dfa6b3994e52c2aa..410cfbfc7670a835541b07351ef7063fd0e2b40c 100644
--- a/templates/post.html
+++ b/templates/post.html
@@ -1,13 +1,16 @@
 <!DOCTYPE html>
 <html data-theme="light" lang="en">
 <head>
-  <% include!("head.html"); %>
+    <% include!("head.html"); %>
 </head>
 <body>
 <% include!("simple_header.html"); %>
 <main class="container">
-  <%- content %>
-</section>
+    <%- content %>
+    </section>
 </main>
 </body>
+
+<script>
+</script>
 </html>
diff --git a/templates/simple_header.html b/templates/simple_header.html
index 0132334d05dc3d402fe303fc7b4f483f53e0e7fc..4382df4342e8602d50ea29aca519284f3451e021 100644
--- a/templates/simple_header.html
+++ b/templates/simple_header.html
@@ -4,9 +4,7 @@         <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>
+            <li><a href="https://gitlab.com/gabrielgio/cv/-/raw/main/cv.pdf?inline=false" class="secondary">Resume</a></li>
         </ul>
     </nav>
 </header>
diff --git a/tests/test_router.rs b/tests/test_router.rs
new file mode 100644
index 0000000000000000000000000000000000000000..0158d782b5eb5b06f948be3b48417943a44180e6
--- /dev/null
+++ b/tests/test_router.rs
@@ -0,0 +1,28 @@
+use router::{Router};
+
+#[test]
+fn test_router_new_posts() {
+    match Router::new("/posts/k8s.html") {
+        Router::NotFound => assert!(false, "Wrong type parse"),
+        Router::Index => assert!(false, "Wrong type parse"),
+        Router::Post { page } => assert_eq!(page, "k8s.html".to_string())
+    };
+}
+
+#[test]
+fn test_router_new_index() {
+    match Router::new("/") {
+        Router::Index => assert!(true),
+        Router::NotFound => assert!(false, "Wrong type parse"),
+        Router::Post { page: _ } => assert!(false, "Wrong type parse")
+    };
+}
+
+#[test]
+fn test_router_new_not_found() {
+    match Router::new("/not_found") {
+        Router::NotFound => assert!(true),
+        Router::Index => assert!(false, "Wrong type parse"),
+        Router::Post { page: _ } => assert!(false, "Wrong type parse")
+    };
+}
diff --git a/watch b/watch
index 9f5acdb892dd59d940fab9a5b32a6539b2f79c2f..5a5076c72464d490604a9bfa6f44f6193fb000c2 100755
--- a/watch
+++ b/watch
@@ -11,6 +11,6 @@
 while true; do
   cargo run &
   PID=$!
-  inotifywait -e modify -e move -e create -e delete -e attrib -r  src/ templates/ assets/
+  inotifywait -e modify -e move -e create -e delete -e attrib -r  src/ templates/ content/
   kill $PID
 done