diff --git a/README.md b/README.md index 762d93a..4e0afbe 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# My website +# crablog Pure rust. Built with actix, diesel, tera, serde and sqlite3. -Environment variables are documented in [variables](./doc/environment.md) +Environment variables are documented in [variables.md](./doc/environment.md) diff --git a/content/html/index.html b/content/html/index.html deleted file mode 100644 index ac38ca6..0000000 --- a/content/html/index.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - mtrx' site - - - - -

Hi, I'm mtrx.

-

- I have a blog.
- This site is 100% open source.
- If you have questions or input for me please send me an E-Mail to me[at]mtrx.tech -

- - diff --git a/content/templates/blog.html b/content/templates/blog.html deleted file mode 100644 index e37030f..0000000 --- a/content/templates/blog.html +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - mtrx' blog - - - - -

mtrx' blog

- - - diff --git a/content/templates/edit.html b/content/templates/edit.html deleted file mode 100644 index 3d71dd0..0000000 --- a/content/templates/edit.html +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - mtrx' blog - - - - -

mtrx' blog

-

Edit posts

- - - diff --git a/content/templates/post-edit.html b/content/templates/post-edit.html deleted file mode 100644 index 65030ba..0000000 --- a/content/templates/post-edit.html +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - Edit Post - - - - - - - -
- - - -
- - -
- -
- -
- - -
-
- - -
- - - diff --git a/content/templates/post-submit.html b/content/templates/post-submit.html deleted file mode 100644 index 7e8c14c..0000000 --- a/content/templates/post-submit.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - Submit Post - - - - - - - -
- - - -
- - -
- -
- - - diff --git a/doc/environment.md b/doc/environment.md index 72bf7a4..c22c1d2 100644 --- a/doc/environment.md +++ b/doc/environment.md @@ -1,5 +1,8 @@ # Environment Variables -- SUBMIT_TOKEN: Alphanumeric string to be used for creating blog posts - ROOT_PATH: path where html, static and database reside - BIND_PORT: port to bind to +- DATABASE_URL: path to the sqlite3 database +- USERNAME: who does this site belong to? +- EMAIL: email to contact the site owner +- SUBMIT_TOKEN: Alphanumeric string to be used for creating blog posts diff --git a/site/src/html.rs b/site/src/html.rs new file mode 100644 index 0000000..db54bb5 --- /dev/null +++ b/site/src/html.rs @@ -0,0 +1,162 @@ +pub const INDEX: &str = r#" + + + + + + + + + {{ username }}' site + + + + + +

Hi, I'm {{ username }}

+

+ I have a blog.
+ If you have questions or input for me please send me an E-Mail to {{ email }} +

+ + +"#; + +pub const BLOG: &str = r#" + + + + + + + + + {{ sitetitle }} + + + + + +

{{ username }}' blog

+ + + +"#; + +pub const SUBMIT: &str = r#" + + + + + + + + + Submit post + + + + + + + +
+ + + +
+ + +
+ +
+ + + +"#; + +pub const EDIT: &str = r#" + + + + + + + + + Edit posts... + + + + +

{{ username }}' blog

+

Edit posts

+ + + +"#; + +pub const POST_EDIT_FORM: &str = r#" + + + + + + + + + Edit '{{ title }}' + + + + + + + +
+ + + +
+ + +
+ +
+ +
+ + +
+
+ + +
+ + + +"#; + diff --git a/site/src/main.rs b/site/src/main.rs index 0caf822..ebaf7f5 100644 --- a/site/src/main.rs +++ b/site/src/main.rs @@ -2,6 +2,7 @@ mod api; mod config; mod db; mod routes; +mod html; #[macro_use] extern crate diesel; diff --git a/site/src/routes.rs b/site/src/routes.rs index 7babbfe..7366e67 100644 --- a/site/src/routes.rs +++ b/site/src/routes.rs @@ -1,7 +1,7 @@ use crate::config; use crate::db; +use crate::html; -use actix_files as fs; use actix_web::{get, http::StatusCode, web, HttpResponse, Responder}; use tera::{Context, Tera}; @@ -45,48 +45,38 @@ pub fn replace_br_tags(x: &str) -> String { #[get("/")] async fn root() -> impl Responder { - let root_path = config::get_from_env("ROOT_PATH", true); - fs::NamedFile::open(root_path + "/html/index.html") + let mut context = Context::new(); + + context.insert("username", &config::get_from_env("USERNAME", true)); + context.insert("email", &config::get_from_env("EMAIL", true)); + + let result = Tera::one_off( + html::INDEX, + &context, + false, + ) + .unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e)); + + HttpResponse::Ok().content_type("text/html").body(result) } #[get("/blog")] async fn blog() -> impl Responder { - let root_path = config::get_from_env("ROOT_PATH", true); - let posts = db::get_last_five_posts(); let mut context = Context::new(); context.insert("posts", &posts); + context.insert("username", &(config::get_from_env("USERNAME", true))); + context.insert("sitetitle", &(config::get_from_env("USERNAME", true) + "' blog")); // one-off render blog template with context let result = Tera::one_off( - &(std::fs::read_to_string(root_path + "/templates/blog.html") - .unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e)) - .as_str()), + html::BLOG, &context, false, ) .unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e)); - HttpResponse::Ok().content_type("text/html").body(result) -} -#[get("/blog/submit")] -async fn blog_submit() -> impl Responder { - let root_path = config::get_from_env("ROOT_PATH", true); - - let mut context = Context::new(); - context.insert("title", ""); - context.insert("body", ""); - - // one-off render blog template with context - let result = Tera::one_off( - &(std::fs::read_to_string(root_path + "/templates/post-submit.html") - .unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e)) - .as_str()), - &context, - false, - ) - .unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e)); HttpResponse::Ok().content_type("text/html").body(result) } @@ -94,40 +84,53 @@ async fn blog_submit() -> impl Responder { async fn blog_by_id(web::Path(post_id): web::Path) -> impl Responder { let (valid, id) = id_valid(post_id); if valid { - let root_path = config::get_from_env("ROOT_PATH", true); - let post = db::get_post_by_id(id as i32); let mut context = Context::new(); - context.insert("posts", &[post]); + context.insert("posts", &[&post]); + context.insert("username", &(config::get_from_env("USERNAME", true))); + context.insert("sitetitle", &post.title); // one-off render blog template with context let result = Tera::one_off( - &(std::fs::read_to_string(root_path + "/templates/blog.html") - .unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e)) - .as_str()), + html::BLOG, &context, false, ) .unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e)); + return HttpResponse::Ok().content_type("text/html").body(result); } else { return HttpResponse::new(StatusCode::NOT_FOUND); } } -#[get("/blog/edit")] -async fn blog_edit() -> impl Responder { - let root_path = config::get_from_env("ROOT_PATH", true); - +#[get("/blog/submit")] +async fn blog_submit() -> impl Responder { let mut context = Context::new(); - context.insert("posts", &db::get_all_posts()); + context.insert("title", ""); + context.insert("body", ""); // one-off render blog template with context let result = Tera::one_off( - &(std::fs::read_to_string(root_path + "/templates/edit.html") - .unwrap_or_else(|e| panic!("Error, couldn't load edit template.\n{}", e)) - .as_str()), + html::SUBMIT, + &context, + false, + ) + .unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e)); + + HttpResponse::Ok().content_type("text/html").body(result) +} + +#[get("/blog/edit")] +async fn blog_edit() -> impl Responder { + let mut context = Context::new(); + context.insert("posts", &db::get_all_posts()); + context.insert("username", &config::get_from_env("USERNAME", true)); + + // one-off render blog template with context + let result = Tera::one_off( + html::EDIT, &context, false, ) @@ -140,8 +143,6 @@ async fn blog_edit() -> impl Responder { async fn blog_edit_by_id(web::Path(post_id): web::Path) -> impl Responder { let (valid, id) = id_valid(post_id); if valid { - let root_path = config::get_from_env("ROOT_PATH", true); - let mut post = db::get_post_by_id(id as i32); post.title = replace_br_tags(&post.title); @@ -154,9 +155,7 @@ async fn blog_edit_by_id(web::Path(post_id): web::Path) -> // one-off render blog template with context let result = Tera::one_off( - &(std::fs::read_to_string(root_path + "/templates/post-edit.html") - .unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e)) - .as_str()), + html::POST_EDIT_FORM, &context, false, )