aboutsummaryrefslogtreecommitdiff
path: root/src/router.rs
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2022-06-11 00:00:27 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2022-06-11 00:00:27 +0200
commit4fb323f69c11557a51c7da0b2031029f63edf789 (patch)
tree0d08ce52abbe0c5d123b4051f50ec5bc386652ae /src/router.rs
parent0e147a780e74b54afbd56ff7438077d855d5c1c2 (diff)
downloadmacroblog.rs-4fb323f69c11557a51c7da0b2031029f63edf789.tar.gz
macroblog.rs-4fb323f69c11557a51c7da0b2031029f63edf789.tar.bz2
macroblog.rs-4fb323f69c11557a51c7da0b2031029f63edf789.zip
feat: Handle 404 result
Now gracefully handle 404, so instead of just panic now it will return a proper http 404 response.
Diffstat (limited to 'src/router.rs')
-rw-r--r--src/router.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/router.rs b/src/router.rs
index 35fdf3e..c196ab8 100644
--- a/src/router.rs
+++ b/src/router.rs
@@ -1,3 +1,6 @@
+use std::borrow::Borrow;
+
+use crate::assets::PostAsset;
use regex::Regex;
const ACTION_REGEX: &str = r"/{0,1}(?P<action>\w*)/(?P<id>.+)";
@@ -8,6 +11,10 @@ pub enum Router {
Post { page: String },
}
+pub fn blog_post_exists(name: &str) -> bool {
+ PostAsset::iter().any(|x| name.eq(&x.to_string()))
+}
+
impl Router {
pub fn new(path: &str) -> Router {
let re = Regex::new(ACTION_REGEX).unwrap();
@@ -17,6 +24,13 @@ impl Router {
None => "index",
};
+
+ // this 7 means the "/posts/" from the full path
+ let trimmed_path: String = path.chars().skip(7).collect();
+ if action.eq("posts") && !blog_post_exists(&trimmed_path) {
+ return Router::NotFound;
+ }
+
match action {
"posts" => Router::Post {
page: caps.unwrap()["id"].to_string(),