crablog/site/src/routes.rs

93 lines
2.8 KiB
Rust
Raw Normal View History

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;
use actix_web::{get, http::StatusCode, post, web, web::Form, HttpResponse, Responder};
use serde::Deserialize;
2020-10-22 00:17:52 +02:00
use tera::{Context, Tera};
#[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,
true,
)
2020-10-22 01:38:43 +02:00
.unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e));
2020-10-22 00:17:52 +02:00
HttpResponse::Ok().body(result)
}
#[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);
HttpResponse::Ok().set_header("SameSite", "secure").body(
2020-10-22 23:51:20 +02:00
std::fs::read_to_string(root_path + "/html/submit.html")
.unwrap_or_else(|e| panic!("Error, couldn't load submit html file.\n{}", e)),
)
}
#[get("/blog/id/{post_id}")]
async fn blog_permalink(web::Path(post_id): web::Path<std::string::String>) -> impl Responder {
match post_id.parse::<u32>() {
Err(_) => HttpResponse::new(StatusCode::NOT_FOUND),
Ok(i) => {
2020-10-22 23:51:20 +02:00
let root_path = config::get_from_env("ROOT_PATH", true);
let post = get_post_by_id(i as i32);
let mut context = Context::new();
context.insert("posts", &[post]);
// 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")
.unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e))
.as_str()),
&context,
true,
)
.unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e));
HttpResponse::Ok().body(result)
}
}
}
#[derive(Deserialize)]
struct NewPostForm {
title: String,
body: String,
token: String,
}
#[post("/blog/posts/new")]
async fn blog_new_post(form: Form<NewPostForm>) -> impl Responder {
2020-10-22 23:51:20 +02:00
let token = config::get_from_env("SUBMIT_TOKEN", true);
if form.token == token {
add_post(&form.title.as_str(), &form.body.as_str());
println!("New blog post created.");
} else {
println!("Unauthorized new blog post");
}
HttpResponse::MovedPermanently()
.set_header("LOCATION", "/blog")
.finish()
2020-10-22 00:17:52 +02:00
}