crablog/site/src/routes.rs

169 lines
4.9 KiB
Rust
Raw Normal View History

2020-10-22 23:51:20 +02:00
use crate::config;
2020-10-26 12:17:52 +01:00
use crate::db;
use crate::html;
2020-10-22 00:17:52 +02:00
2020-10-25 23:23:32 +01:00
use actix_web::{get, http::StatusCode, web, HttpResponse, Responder};
2020-10-22 00:17:52 +02:00
use tera::{Context, Tera};
2020-10-26 12:17:52 +01:00
/// authorizes a request by comparing it to the SUBMIT_TOKEN environment variable
2020-10-25 23:23:32 +01:00
pub fn authorized(form_token: &str) -> bool {
let token = config::get_from_env("SUBMIT_TOKEN", true);
if token == form_token {
return true;
}
false
}
2020-10-26 12:17:52 +01:00
/// tests if the post id is a valid i32 integer bigger than zero
/// assert(!(id_valid("2147483648").0))
/// assert(!(id_valid("-1").0))
/// assert(id_valid("1").0))
2020-10-25 23:23:32 +01:00
pub fn id_valid(post_id: String) -> (bool, i32) {
match post_id.parse::<i32>() {
Err(_) => (false, 0),
Ok(id) => {
if id < 1 {
(false, id)
} else {
(true, id)
}
}
}
}
2020-10-26 12:17:52 +01:00
/// replaces the \n character with a <br> html tag
/// assert(replace_newlines("test\ntest") == "test<br>test")
2020-10-26 10:56:05 +01:00
pub fn replace_newlines(x: &str) -> String {
2020-10-25 23:23:32 +01:00
x.replace("\n", "<br>")
}
2020-10-26 12:17:52 +01:00
/// replaces the \n character with a <br> html tag
/// assert(replace_newlines("test<br>test") == "test\ntest")
2020-10-26 10:56:05 +01:00
pub fn replace_br_tags(x: &str) -> String {
2020-10-25 23:23:32 +01:00
x.replace("<br>", "\n")
}
2020-10-22 00:17:52 +02:00
#[get("/")]
async fn root() -> impl Responder {
let mut context = Context::new();
context.insert("username", &config::get_from_env("USERNAME", true));
context.insert("email", &config::get_from_env("EMAIL", true));
2020-10-22 00:17:52 +02:00
let result = Tera::one_off(
html::INDEX,
2020-10-22 00:17:52 +02:00
&context,
2020-10-23 22:41:28 +02:00
false,
2020-10-22 00:17:52 +02:00
)
2020-10-22 01:38:43 +02:00
.unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e));
2020-10-23 11:34:54 +02:00
HttpResponse::Ok().content_type("text/html").body(result)
2020-10-22 00:17:52 +02:00
}
#[get("/blog")]
async fn blog() -> impl Responder {
let posts = db::get_last_five_posts();
2020-10-25 23:23:32 +01:00
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"));
2020-10-25 23:23:32 +01:00
// one-off render blog template with context
let result = Tera::one_off(
html::BLOG,
2020-10-25 23:23:32 +01:00
&context,
false,
)
.unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e));
2020-10-25 23:23:32 +01:00
HttpResponse::Ok().content_type("text/html").body(result)
}
#[get("/blog/id/{post_id}")]
2020-10-25 23:23:32 +01:00
async fn blog_by_id(web::Path(post_id): web::Path<std::string::String>) -> impl Responder {
let (valid, id) = id_valid(post_id);
if valid {
2020-10-26 12:17:52 +01:00
let post = db::get_post_by_id(id as i32);
2020-10-25 23:23:32 +01:00
let mut context = Context::new();
context.insert("posts", &[&post]);
context.insert("username", &(config::get_from_env("USERNAME", true)));
context.insert("sitetitle", &post.title);
2020-10-25 23:23:32 +01:00
// one-off render blog template with context
let result = Tera::one_off(
html::BLOG,
2020-10-25 23:23:32 +01:00
&context,
false,
)
.unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e));
2020-10-25 23:23:32 +01:00
return HttpResponse::Ok().content_type("text/html").body(result);
} else {
return HttpResponse::new(StatusCode::NOT_FOUND);
}
}
#[get("/blog/submit")]
async fn blog_submit() -> impl Responder {
let mut context = Context::new();
context.insert("title", "");
context.insert("body", "");
// one-off render blog template with context
let result = Tera::one_off(
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)
}
2020-10-25 23:23:32 +01:00
#[get("/blog/edit")]
async fn blog_edit() -> impl Responder {
2020-10-26 12:17:52 +01:00
let mut context = Context::new();
context.insert("posts", &db::get_all_posts());
context.insert("username", &config::get_from_env("USERNAME", true));
2020-10-26 12:17:52 +01:00
// one-off render blog template with context
let result = Tera::one_off(
html::EDIT,
2020-10-26 12:17:52 +01:00
&context,
false,
)
.unwrap_or_else(|e| panic!("Error, couldn't render submit template.\n{}", e));
return HttpResponse::Ok().content_type("text/html").body(result);
}
2020-10-25 23:23:32 +01:00
#[get("/blog/edit/{post_id}")]
async fn blog_edit_by_id(web::Path(post_id): web::Path<std::string::String>) -> impl Responder {
let (valid, id) = id_valid(post_id);
if valid {
2020-10-26 12:17:52 +01:00
let mut post = db::get_post_by_id(id as i32);
2020-10-25 23:23:32 +01:00
post.title = replace_br_tags(&post.title);
post.body = replace_br_tags(&post.body);
let mut context = Context::new();
context.insert("title", &post.title);
context.insert("body", &post.body);
context.insert("id", &id);
// one-off render blog template with context
let result = Tera::one_off(
html::POST_EDIT_FORM,
2020-10-25 23:23:32 +01:00
&context,
false,
)
.unwrap_or_else(|e| panic!("Error, couldn't render submit template.\n{}", e));
2020-10-25 23:23:32 +01:00
return HttpResponse::Ok().content_type("text/html").body(result);
} else {
2020-10-25 23:23:32 +01:00
return HttpResponse::new(StatusCode::UNAUTHORIZED);
}
2020-10-22 00:17:52 +02:00
}