2020-10-22 23:51:20 +02:00
|
|
|
use crate::config;
|
2020-10-22 00:17:52 +02:00
|
|
|
use crate::db::*;
|
|
|
|
|
|
|
|
use actix_files as fs;
|
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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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 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 {
|
2020-10-22 23:51:20 +02:00
|
|
|
let root_path = config::get_from_env("ROOT_PATH", true);
|
|
|
|
fs::NamedFile::open(root_path + "/html/index.html")
|
2020-10-22 00:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/blog")]
|
|
|
|
async fn blog() -> impl Responder {
|
2020-10-22 23:51:20 +02:00
|
|
|
let root_path = config::get_from_env("ROOT_PATH", true);
|
|
|
|
|
2020-10-22 00:17:52 +02:00
|
|
|
let posts = get_posts();
|
|
|
|
|
|
|
|
let mut context = Context::new();
|
|
|
|
context.insert("posts", &posts);
|
|
|
|
|
|
|
|
// one-off render blog template with context
|
|
|
|
let result = Tera::one_off(
|
2020-10-22 23:51:20 +02:00
|
|
|
&(std::fs::read_to_string(root_path + "/templates/blog.html")
|
2020-10-22 01:38:43 +02:00
|
|
|
.unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e))
|
2020-10-22 00:17:52 +02:00
|
|
|
.as_str()),
|
|
|
|
&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
|
|
|
}
|
|
|
|
|
2020-10-22 16:52:04 +02:00
|
|
|
#[get("/blog/submit")]
|
|
|
|
async fn blog_submit() -> impl Responder {
|
2020-10-22 23:51:20 +02:00
|
|
|
let root_path = config::get_from_env("ROOT_PATH", true);
|
2020-10-25 23:23:32 +01:00
|
|
|
|
|
|
|
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)
|
2020-10-22 16:52:04 +02:00
|
|
|
}
|
2020-10-22 00:25:30 +02:00
|
|
|
|
2020-10-22 16:52:04 +02:00
|
|
|
#[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 {
|
|
|
|
let root_path = config::get_from_env("ROOT_PATH", true);
|
|
|
|
|
|
|
|
let post = get_post_by_id(id as i32);
|
|
|
|
|
|
|
|
let mut context = Context::new();
|
|
|
|
context.insert("posts", &[post]);
|
|
|
|
|
|
|
|
// 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()),
|
|
|
|
&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);
|
2020-10-22 16:52:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 23:23:32 +01:00
|
|
|
#[get("/blog/edit")]
|
|
|
|
async fn blog_edit() -> impl Responder {
|
|
|
|
"edit"
|
2020-10-22 16:52:04 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
let root_path = config::get_from_env("ROOT_PATH", true);
|
|
|
|
|
|
|
|
let mut post = get_post_by_id(id as i32);
|
|
|
|
|
|
|
|
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(
|
|
|
|
&(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()),
|
|
|
|
&context,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|e| panic!("Error, couldn't render submit template.\n{}", e));
|
2020-10-23 22:50:55 +02:00
|
|
|
|
2020-10-25 23:23:32 +01:00
|
|
|
return HttpResponse::Ok().content_type("text/html").body(result);
|
2020-10-22 16:52:04 +02:00
|
|
|
} else {
|
2020-10-25 23:23:32 +01:00
|
|
|
return HttpResponse::new(StatusCode::UNAUTHORIZED);
|
2020-10-22 16:52:04 +02:00
|
|
|
}
|
2020-10-22 00:17:52 +02:00
|
|
|
}
|