aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/assets.rs11
-rw-r--r--src/bin/actix.rs12
-rw-r--r--src/bin/hyper.rs36
-rw-r--r--src/blog.rs21
-rw-r--r--src/router.rs9
5 files changed, 71 insertions, 18 deletions
diff --git a/src/assets.rs b/src/assets.rs
index 2c39d1b..32d26e9 100644
--- a/src/assets.rs
+++ b/src/assets.rs
@@ -11,12 +11,23 @@ pub const BLOG_REGEX: &str = r"(?P<date>[\d]{4}-[\d]{2}-[\d]{2})(?P<title>[a-zA-
#[folder = "content/posts/"]
pub struct PostAsset;
+#[derive(RustEmbed)]
+#[folder = "content/projects/"]
+pub struct ProjectsAsset;
+
#[derive(TemplateOnce)]
#[template(path = "index.html")]
pub struct IndexTemplate {
pub posts: Vec<BlogEntry>,
}
+
+#[derive(TemplateOnce)]
+#[template(path = "projects.html")]
+pub struct ProjectsTemplate {
+ pub content: String,
+}
+
#[derive(TemplateOnce)]
#[template(path = "post.html")]
pub struct PostTemplate {
diff --git a/src/bin/actix.rs b/src/bin/actix.rs
index 101fe2e..978e8ed 100644
--- a/src/bin/actix.rs
+++ b/src/bin/actix.rs
@@ -1,5 +1,5 @@
use actix_web::{get, web, middleware, App, HttpResponse, HttpServer, Responder, http::header::ContentType};
-use macroblog::blog::{render_index_page, render_post_page};
+use macroblog::blog::{render_index_page, render_post_page, render_projects};
use macroblog::router::blog_post_exists;
use std::env;
@@ -12,6 +12,15 @@ async fn index() -> impl Responder {
.body(body)
}
+#[get("/projects")]
+async fn projects() -> impl Responder {
+ let body = render_projects();
+
+ HttpResponse::Ok()
+ .content_type(ContentType::html())
+ .body(body)
+}
+
#[get("/posts/{name}")]
async fn posts(name: web::Path<String>) -> impl Responder {
@@ -36,6 +45,7 @@ async fn main() -> std::io::Result<()> {
App::new()
.wrap(middleware::Compress::default())
.service(index)
+ .service(projects)
.service(posts)
})
.bind(("0.0.0.0", port))?
diff --git a/src/bin/hyper.rs b/src/bin/hyper.rs
index 3f23f18..f24cbe4 100644
--- a/src/bin/hyper.rs
+++ b/src/bin/hyper.rs
@@ -1,11 +1,10 @@
-use std::convert::Infallible;
-use std::{env};
-use std::net::SocketAddr;
-use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
+use hyper::{Body, Request, Response, Server};
+use macroblog::blog::{render_index_page, render_post_page, render_projects};
use macroblog::router::Router;
-use macroblog::blog::{render_index_page, render_post_page};
-
+use std::convert::Infallible;
+use std::env;
+use std::net::SocketAddr;
async fn not_found() -> Result<Response<Body>, Infallible> {
let resp: Response<Body> = Response::builder()
@@ -15,7 +14,6 @@ async fn not_found() -> Result<Response<Body>, Infallible> {
Ok(resp)
}
-
async fn index() -> Result<Response<Body>, Infallible> {
let body = render_index_page();
@@ -28,6 +26,17 @@ async fn index() -> Result<Response<Body>, Infallible> {
Ok(resp)
}
+async fn projects() -> Result<Response<Body>, Infallible> {
+ let body = render_projects();
+
+ let resp: Response<Body> = Response::builder()
+ .status(200)
+ .header("posts-type", "text/html")
+ .body(body.into())
+ .unwrap();
+
+ Ok(resp)
+}
async fn post(path: &String) -> Result<Response<Body>, Infallible> {
let body = render_post_page(path);
@@ -46,20 +55,21 @@ async fn request(req: Request<Body>) -> Result<Response<Body>, Infallible> {
match Router::new(path) {
Router::Index => index().await,
+ Router::Projects => projects().await,
Router::Post { page } => post(&page).await,
- Router::NotFound => not_found().await
+ Router::NotFound => not_found().await,
}
}
-
#[tokio::main]
async fn main() {
- let port = env::var("PORT").unwrap_or("3000".into()).parse::<u16>().unwrap_or(3000);
+ let port = env::var("PORT")
+ .unwrap_or("3000".into())
+ .parse::<u16>()
+ .unwrap_or(3000);
let addr = SocketAddr::from(([0, 0, 0, 0], port));
- let make_svc = make_service_fn(|_conn| async {
- Ok::<_, Infallible>(service_fn(request))
- });
+ let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(request)) });
let server = Server::bind(&addr).serve(make_svc);
diff --git a/src/blog.rs b/src/blog.rs
index a1586f8..8c3af52 100644
--- a/src/blog.rs
+++ b/src/blog.rs
@@ -1,8 +1,7 @@
+use crate::assets::{BlogEntry, IndexTemplate, PostAsset, PostTemplate, ProjectsAsset, ProjectsTemplate};
use pulldown_cmark::{html, Options, Parser};
use sailfish::TemplateOnce;
use std::str;
-use crate::assets::{BlogEntry, PostAsset, IndexTemplate, PostTemplate};
-
pub fn read_assets() -> Vec<BlogEntry> {
let mut entries: Vec<BlogEntry> = PostAsset::iter()
@@ -26,6 +25,24 @@ fn get_file_content(path: &str) -> String {
return html_output.to_string();
}
+fn get_projects_content() -> String {
+ let buffer = ProjectsAsset::get("index.md").unwrap().data.into_owned();
+ let md = String::from_utf8(buffer).unwrap();
+ let mut options = Options::empty();
+ options.insert(Options::ENABLE_FOOTNOTES);
+ let parser = Parser::new_ext(&md, options);
+ let mut html_output = &mut String::new();
+ html::push_html(&mut html_output, parser);
+ return html_output.to_string();
+}
+
+pub fn render_projects() -> String {
+ ProjectsTemplate {
+ content: get_projects_content(),
+ }
+ .render_once()
+ .unwrap()
+}
pub fn render_post_page(path: &String) -> String {
let blog = BlogEntry::new(path);
diff --git a/src/router.rs b/src/router.rs
index 3227d66..c5efd9c 100644
--- a/src/router.rs
+++ b/src/router.rs
@@ -1,11 +1,12 @@
use crate::assets::PostAsset;
use regex::Regex;
-const ACTION_REGEX: &str = r"/{0,1}(?P<action>\w*)/(?P<id>.+)";
+const ACTION_REGEX: &str = r"/{0,1}(?P<action>\w*)/{0,1}(?P<id>.*)";
pub enum Router {
NotFound,
Index,
+ Projects,
Post { page: String },
}
@@ -17,11 +18,14 @@ impl Router {
pub fn new(path: &str) -> Router {
let re = Regex::new(ACTION_REGEX).unwrap();
let caps = re.captures(path);
- let action = match caps {
+ let mut action = match caps {
Some(ref value) => &value["action"],
None => "index",
};
+ if action == "" {
+ action = "index"
+ }
// this 7 means the "/posts/" from the full path
let trimmed_path: String = path.chars().skip(7).collect();
@@ -33,6 +37,7 @@ impl Router {
"posts" => Router::Post {
page: caps.unwrap()["id"].to_string(),
},
+ "projects" => Router::Projects,
"index" => Router::Index,
_ => Router::NotFound,
}