Added envvar configuration

This commit is contained in:
Leonard Lorenz 2020-10-22 23:51:20 +02:00
parent b56dcdf330
commit 8a70db9b42
4 changed files with 31 additions and 11 deletions

12
src/config.rs Normal file
View file

@ -0,0 +1,12 @@
use std::string::String;
pub fn get_from_env(variable: &str, mandatory: bool) -> String {
std::env::var(variable).unwrap_or_else(|_| {
if mandatory {
println!("Error, couldn't read environment variable: {}", variable);
std::process::exit(1);
} else {
panic!("Error, couldn't read environment variable: {}", variable);
}
})
}

View file

@ -1,12 +1,14 @@
mod models; mod models;
mod schema; mod schema;
use crate::config;
use diesel::prelude::*; use diesel::prelude::*;
use diesel::sqlite::SqliteConnection; use diesel::sqlite::SqliteConnection;
use models::*; use models::*;
fn establish_connection() -> SqliteConnection { fn establish_connection() -> SqliteConnection {
let db_path = "db.sqlite3"; let root_path = config::get_from_env("ROOT_PATH", true);
let db_path = root_path + "/db.sqlite3";
SqliteConnection::establish(&db_path) SqliteConnection::establish(&db_path)
.unwrap_or_else(|_| panic!("Error, connection to {} failed.", &db_path)) .unwrap_or_else(|_| panic!("Error, connection to {} failed.", &db_path))
} }

View file

@ -1,3 +1,4 @@
mod config;
mod db; mod db;
mod routes; mod routes;
@ -8,12 +9,12 @@ extern crate serde_derive;
extern crate tera; extern crate tera;
use actix_files as fs; use actix_files as fs;
use actix_web::{middleware, App, HttpServer}; use actix_web::{App, HttpServer};
use std::string::String;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
HttpServer::new(|| { HttpServer::new(|| {
let root_path = config::get_from_env("ROOT_PATH", true);
App::new() App::new()
//.wrap(middleware::NormalizePath::default()) //.wrap(middleware::NormalizePath::default())
.service(routes::root) .service(routes::root)
@ -21,7 +22,7 @@ async fn main() -> std::io::Result<()> {
.service(routes::blog_permalink) .service(routes::blog_permalink)
.service(routes::blog_submit) .service(routes::blog_submit)
.service(routes::blog_new_post) .service(routes::blog_new_post)
.service(fs::Files::new("/static", "./static/")) .service(fs::Files::new("/static", root_path + "/static"))
}) })
.bind("localhost:8000")? .bind("localhost:8000")?
.run() .run()

View file

@ -1,3 +1,4 @@
use crate::config;
use crate::db::*; use crate::db::*;
use actix_files as fs; use actix_files as fs;
@ -7,11 +8,14 @@ use tera::{Context, Tera};
#[get("/")] #[get("/")]
async fn root() -> impl Responder { async fn root() -> impl Responder {
fs::NamedFile::open("html/index.html") let root_path = config::get_from_env("ROOT_PATH", true);
fs::NamedFile::open(root_path + "/html/index.html")
} }
#[get("/blog")] #[get("/blog")]
async fn blog() -> impl Responder { async fn blog() -> impl Responder {
let root_path = config::get_from_env("ROOT_PATH", true);
let posts = get_posts(); let posts = get_posts();
let mut context = Context::new(); let mut context = Context::new();
@ -19,7 +23,7 @@ async fn blog() -> impl Responder {
// one-off render blog template with context // one-off render blog template with context
let result = Tera::one_off( let result = Tera::one_off(
&(std::fs::read_to_string("templates/blog.html") &(std::fs::read_to_string(root_path + "/templates/blog.html")
.unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e)) .unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e))
.as_str()), .as_str()),
&context, &context,
@ -31,8 +35,9 @@ async fn blog() -> impl Responder {
#[get("/blog/submit")] #[get("/blog/submit")]
async fn blog_submit() -> impl Responder { async fn blog_submit() -> impl Responder {
let root_path = config::get_from_env("ROOT_PATH", true);
HttpResponse::Ok().set_header("SameSite", "secure").body( HttpResponse::Ok().set_header("SameSite", "secure").body(
std::fs::read_to_string("html/submit.html") std::fs::read_to_string(root_path + "/html/submit.html")
.unwrap_or_else(|e| panic!("Error, couldn't load submit html file.\n{}", e)), .unwrap_or_else(|e| panic!("Error, couldn't load submit html file.\n{}", e)),
) )
} }
@ -42,6 +47,8 @@ async fn blog_permalink(web::Path(post_id): web::Path<std::string::String>) -> i
match post_id.parse::<u32>() { match post_id.parse::<u32>() {
Err(_) => HttpResponse::new(StatusCode::NOT_FOUND), Err(_) => HttpResponse::new(StatusCode::NOT_FOUND),
Ok(i) => { Ok(i) => {
let root_path = config::get_from_env("ROOT_PATH", true);
let post = get_post_by_id(i as i32); let post = get_post_by_id(i as i32);
let mut context = Context::new(); let mut context = Context::new();
@ -49,7 +56,7 @@ async fn blog_permalink(web::Path(post_id): web::Path<std::string::String>) -> i
// one-off render blog template with context // one-off render blog template with context
let result = Tera::one_off( let result = Tera::one_off(
&(std::fs::read_to_string("templates/blog.html") &(std::fs::read_to_string(root_path + "/templates/blog.html")
.unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e)) .unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e))
.as_str()), .as_str()),
&context, &context,
@ -70,9 +77,7 @@ struct NewPostForm {
#[post("/blog/posts/new")] #[post("/blog/posts/new")]
async fn blog_new_post(form: Form<NewPostForm>) -> impl Responder { async fn blog_new_post(form: Form<NewPostForm>) -> impl Responder {
let token: String = std::env::var("SUBMIT_TOKEN").unwrap_or_else(|_| { let token = config::get_from_env("SUBMIT_TOKEN", true);
panic!("Error, can't authenticate submission if no submit token was set.");
});
if form.token == token { if form.token == token {
add_post(&form.title.as_str(), &form.body.as_str()); add_post(&form.title.as_str(), &form.body.as_str());