diff options
-rw-r--r-- | Cargo.lock | 27 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | assets/post1.html | 9 | ||||
-rw-r--r-- | assets/post2.html | 9 | ||||
-rw-r--r-- | src/main.rs | 74 | ||||
-rw-r--r-- | templates/head.html | 6 | ||||
-rw-r--r-- | templates/header.html | 18 | ||||
-rw-r--r-- | templates/index.html | 41 | ||||
-rw-r--r-- | templates/pico.min.css (renamed from assets/pico.min.css) | 0 | ||||
-rw-r--r-- | templates/post.html | 13 | ||||
-rw-r--r-- | templates/simple_header.html | 12 |
11 files changed, 168 insertions, 42 deletions
@@ -3,6 +3,15 @@ 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", ] @@ -363,6 +373,23 @@ dependencies = [ ] [[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" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -7,6 +7,7 @@ edition = "2021" 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/post1.html b/assets/post1.html new file mode 100644 index 0000000..6eafb91 --- /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 0000000..077734e --- /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 0c3565b..92f51a7 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], +} + +#[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"), + ), +]; + +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) } -const PICO_CSS: &str = include_str!("../assets/pico.min.css"); -async fn hello_world(_req: Request<Body>) -> Result<Response<Body>, Infallible> { - let body = IndexTemplate { pico: PICO_CSS } +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 0000000..cc1bdb8 --- /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 0000000..e55cebe --- /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 90826de..3558e06 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,49 +1,18 @@ <!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> </html>
\ No newline at end of file diff --git a/assets/pico.min.css b/templates/pico.min.css index 1179471..1179471 100644 --- a/assets/pico.min.css +++ b/templates/pico.min.css diff --git a/templates/post.html b/templates/post.html new file mode 100644 index 0000000..68211c1 --- /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 0000000..0132334 --- /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> |