aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2022-05-08 21:47:45 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2022-05-08 21:47:45 +0200
commitea058a851098bf81cb645249e02d26a8c253db90 (patch)
treec245a133119b1a4bf6168a16c89b22b6e04d319e
parent189166e2f44ca69537fa632032ec7ab252595d1b (diff)
downloadmacroblog.rs-ea058a851098bf81cb645249e02d26a8c253db90.tar.gz
macroblog.rs-ea058a851098bf81cb645249e02d26a8c253db90.tar.bz2
macroblog.rs-ea058a851098bf81cb645249e02d26a8c253db90.zip
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.
-rw-r--r--Cargo.lock126
-rw-r--r--Cargo.toml5
-rw-r--r--assets/post1.html9
-rw-r--r--assets/post2.html9
-rw-r--r--content/posts/Friz.box_turned_off_DHCP.html18
-rw-r--r--content/posts/K8S_private_gitlab_registry_using_podman.html55
-rw-r--r--src/main.rs85
-rw-r--r--src/router.rs27
-rw-r--r--templates/header.html4
-rw-r--r--templates/index.html2
-rw-r--r--templates/pico.min.css55
-rw-r--r--templates/post.html9
-rw-r--r--templates/simple_header.html4
-rw-r--r--tests/test_router.rs28
-rwxr-xr-xwatch2
15 files changed, 320 insertions, 118 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 9f33acf..b687dc4 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"
@@ -36,6 +45,24 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -93,6 +120,16 @@ dependencies = [
]
[[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",
]
@@ -311,6 +349,12 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -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 @@ dependencies = [
]
[[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 @@ dependencies = [
]
[[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"
@@ -644,6 +761,15 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
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 @@
-<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 077734e..0000000
--- 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 0000000..569604b
--- /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 0000000..3672755
--- /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 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<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_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();
+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()
+}
+
+
+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 @@ async fn index() -> Result<Response<Body>, Infallible> {
}
-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 post(index: usize) -> Result<Response<Body>, Infallible> {
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;
+ 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<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 e55cebe..1005e66 100644
--- a/templates/header.html
+++ b/templates/header.html
@@ -4,9 +4,7 @@
<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 3558e06..6d0c139 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -9,7 +9,7 @@
<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 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 @@
<!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 0132334..4382df4 100644
--- a/templates/simple_header.html
+++ b/templates/simple_header.html
@@ -4,9 +4,7 @@
<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 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