aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs85
-rw-r--r--src/router.rs27
2 files changed, 70 insertions, 42 deletions
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
+ }
+ }
+}
+