From ea058a851098bf81cb645249e02d26a8c253db90 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 8 May 2022 21:47:45 +0200 Subject: 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. --- Cargo.lock | 126 +++++++++++++++++++++ Cargo.toml | 5 + assets/post1.html | 9 -- assets/post2.html | 9 -- content/posts/Friz.box_turned_off_DHCP.html | 18 +++ .../K8S_private_gitlab_registry_using_podman.html | 55 +++++++++ src/main.rs | 85 +++++++------- src/router.rs | 27 +++++ templates/header.html | 4 +- templates/index.html | 2 +- templates/pico.min.css | 55 ++------- templates/post.html | 9 +- templates/simple_header.html | 4 +- tests/test_router.rs | 28 +++++ watch | 2 +- 15 files changed, 320 insertions(+), 118 deletions(-) delete mode 100644 assets/post1.html delete mode 100644 assets/post2.html create mode 100644 content/posts/Friz.box_turned_off_DHCP.html create mode 100644 content/posts/K8S_private_gitlab_registry_using_podman.html create mode 100644 src/router.rs create mode 100644 tests/test_router.rs diff --git a/Cargo.lock b/Cargo.lock index 9f33acf..b687dc4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,15 @@ version = "1.3.2" 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" @@ -35,6 +44,24 @@ 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" version = "0.2.16" @@ -92,6 +119,16 @@ dependencies = [ "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" @@ -252,6 +289,7 @@ version = "0.1.0" dependencies = [ "hyper", "regex", + "rust-embed", "sailfish", "tokio", ] @@ -310,6 +348,12 @@ 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" version = "0.12.0" @@ -389,6 +433,40 @@ version = "0.6.25" 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" @@ -433,6 +511,15 @@ dependencies = [ "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" @@ -459,6 +546,19 @@ dependencies = [ "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" @@ -599,6 +699,12 @@ version = "0.2.3" 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" @@ -611,6 +717,17 @@ version = "0.9.4" 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" @@ -643,6 +760,15 @@ 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" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 093a4d3..de00d11 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 6eafb91..0000000 --- a/assets/post1.html +++ /dev/null @@ -1,9 +0,0 @@ -
-

Title

-

- Nice content - - My code here - -

-
diff --git a/assets/post2.html b/assets/post2.html deleted file mode 100644 index 077734e..0000000 --- a/assets/post2.html +++ /dev/null @@ -1,9 +0,0 @@ -
-

Title

-

- Nice content 2 - - My code here - -

-
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 0000000..569604b --- /dev/null +++ b/content/posts/Friz.box_turned_off_DHCP.html @@ -0,0 +1,18 @@ +
+

Friz.box turned off DHCP

+

+ If you turned off your DHCP server follow these steps to connect to FritzBox settings. +
+

+

+

+ On gnome turn the wired connection off on again to apply the settings. +

+

+ Note: why in the hell does FritzBox 7490 require a land-line telephone to be physically factory + reset? +

+
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 0000000..3672755 --- /dev/null +++ b/content/posts/K8S_private_gitlab_registry_using_podman.html @@ -0,0 +1,55 @@ +
+

K8S private gitlab registry using podman

+

+ This is based on Log in to + Docker Hub. It is just a bit different to use podman. +

+

+ First we should take a look at podman-login man page: +

+        man podman login
+    
+

+

+ It will give some valueable information like the location of auth.json file. Now we can login using podman: +

+        podman login registry.gitlab.com
+    
+

+

Then check the auth.json file located at ${XDG_RUNTIME_DIR}/containers/auth.json (as described + by the manual). It will contain your auth config: +

+        
+{
+	"auths": {
+		"registry.gitlab.com": {
+			"auth": "..."
+		}
+	}
+}
+        
+    
+

+

+ Now copy that file over to the server and register it in k8s with the following command: +

+        
+kubectl create secret generic regcred \
+    --from-file=.dockerconfigjson=auth.json \
+    --type=kubernetes.io/dockerconfigjson
+
+        
+    
+

+

+ Once you have created you can list by kubectl get secret: +

+        
+NAME     TYPE                                  DATA   AGE
+regcred  kubernetes.io/dockerconfigjson        1      53s
+        
+    
+

+ +
diff --git a/src/main.rs b/src/main.rs index 1fb0e90..cabff0e 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, } #[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_path_id(path: &str) -> usize { - let re = Regex::new(r"(?P\d+)").unwrap(); - let caps = re.captures(path).unwrap(); - let id = &caps["id"]; - - return id.parse::().unwrap(); +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 { + PostAsset::iter() + .map(|e| format!("{}", e)) + .collect() +} + + +async fn not_found() -> Result, Infallible> { + let resp: Response = Response::builder() + .status(404) + .body("Not Found".into()) + .unwrap(); + Ok(resp) +} + + async fn index() -> Result, Infallible> { - let body = IndexTemplate { posts: POSTS } + let files = get_post_title(); + let body = IndexTemplate { posts: files } .render_once() .unwrap(); @@ -62,8 +67,8 @@ async fn index() -> Result, Infallible> { } -async fn post(index: usize) -> Result, Infallible> { - let body = PostTemplate { content: POSTS[index].2 } +async fn post(path: &String) -> Result, Infallible> { + let body = PostTemplate { content: get_file_content(path) } .render_once() .unwrap(); @@ -78,16 +83,12 @@ async fn post(index: usize) -> Result, Infallible> { async fn request(req: Request) -> Result, 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; + match Router::new(path) { + Router::Index => index().await, + Router::Post { page } => post(&page).await, + Router::NotFound => not_found().await + } } diff --git a/src/router.rs b/src/router.rs new file mode 100644 index 0000000..0bba091 --- /dev/null +++ b/src/router.rs @@ -0,0 +1,27 @@ +use regex::{Regex}; + +const ACTION_REGEX: &str = r"/{0,1}(?P\w*)/(?P.+)"; + +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 e55cebe..1005e66 100644 --- a/templates/header.html +++ b/templates/header.html @@ -4,9 +4,7 @@
  • Yet Another Blog
  • diff --git a/templates/index.html b/templates/index.html index 3558e06..6d0c139 100644 --- a/templates/index.html +++ b/templates/index.html @@ -9,7 +9,7 @@
    diff --git a/templates/pico.min.css b/templates/pico.min.css index 1179471..7713ba9 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 @@ address, blockquote, dl, figure, form, ol, p, pre, table, ul { [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 @@ p { } ul li { - list-style: none + list-style: square; } mark { @@ -503,63 +503,24 @@ fieldset legend, label { 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 { background-color: var(--code-kbd-background-color); color: var(--code-kbd-color); diff --git a/templates/post.html b/templates/post.html index 68211c1..410cfbf 100644 --- a/templates/post.html +++ b/templates/post.html @@ -1,13 +1,16 @@ - <% include!("head.html"); %> + <% include!("head.html"); %> <% include!("simple_header.html"); %>
    - <%- content %> - + <%- content %> +
    + + diff --git a/templates/simple_header.html b/templates/simple_header.html index 0132334..4382df4 100644 --- a/templates/simple_header.html +++ b/templates/simple_header.html @@ -4,9 +4,7 @@
  • Yet Another Blog
  • diff --git a/tests/test_router.rs b/tests/test_router.rs new file mode 100644 index 0000000..0158d78 --- /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 9f5acdb..5a5076c 100755 --- a/watch +++ b/watch @@ -11,6 +11,6 @@ trap sigint_handler SIGINT 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 -- cgit v1.2.3