Added envvar configuration
This commit is contained in:
parent
b56dcdf330
commit
8a70db9b42
4 changed files with 31 additions and 11 deletions
12
src/config.rs
Normal file
12
src/config.rs
Normal 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);
|
||||
}
|
||||
})
|
||||
}
|
|
@ -1,12 +1,14 @@
|
|||
mod models;
|
||||
mod schema;
|
||||
|
||||
use crate::config;
|
||||
use diesel::prelude::*;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use models::*;
|
||||
|
||||
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)
|
||||
.unwrap_or_else(|_| panic!("Error, connection to {} failed.", &db_path))
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
mod config;
|
||||
mod db;
|
||||
mod routes;
|
||||
|
||||
|
@ -8,12 +9,12 @@ extern crate serde_derive;
|
|||
extern crate tera;
|
||||
|
||||
use actix_files as fs;
|
||||
use actix_web::{middleware, App, HttpServer};
|
||||
use std::string::String;
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
let root_path = config::get_from_env("ROOT_PATH", true);
|
||||
App::new()
|
||||
//.wrap(middleware::NormalizePath::default())
|
||||
.service(routes::root)
|
||||
|
@ -21,7 +22,7 @@ async fn main() -> std::io::Result<()> {
|
|||
.service(routes::blog_permalink)
|
||||
.service(routes::blog_submit)
|
||||
.service(routes::blog_new_post)
|
||||
.service(fs::Files::new("/static", "./static/"))
|
||||
.service(fs::Files::new("/static", root_path + "/static"))
|
||||
})
|
||||
.bind("localhost:8000")?
|
||||
.run()
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::config;
|
||||
use crate::db::*;
|
||||
|
||||
use actix_files as fs;
|
||||
|
@ -7,11 +8,14 @@ use tera::{Context, Tera};
|
|||
|
||||
#[get("/")]
|
||||
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")]
|
||||
async fn blog() -> impl Responder {
|
||||
let root_path = config::get_from_env("ROOT_PATH", true);
|
||||
|
||||
let posts = get_posts();
|
||||
|
||||
let mut context = Context::new();
|
||||
|
@ -19,7 +23,7 @@ async fn blog() -> impl Responder {
|
|||
|
||||
// one-off render blog template with context
|
||||
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))
|
||||
.as_str()),
|
||||
&context,
|
||||
|
@ -31,8 +35,9 @@ async fn blog() -> impl Responder {
|
|||
|
||||
#[get("/blog/submit")]
|
||||
async fn blog_submit() -> impl Responder {
|
||||
let root_path = config::get_from_env("ROOT_PATH", true);
|
||||
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)),
|
||||
)
|
||||
}
|
||||
|
@ -42,6 +47,8 @@ async fn blog_permalink(web::Path(post_id): web::Path<std::string::String>) -> i
|
|||
match post_id.parse::<u32>() {
|
||||
Err(_) => HttpResponse::new(StatusCode::NOT_FOUND),
|
||||
Ok(i) => {
|
||||
let root_path = config::get_from_env("ROOT_PATH", true);
|
||||
|
||||
let post = get_post_by_id(i as i32);
|
||||
|
||||
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
|
||||
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))
|
||||
.as_str()),
|
||||
&context,
|
||||
|
@ -70,9 +77,7 @@ struct NewPostForm {
|
|||
|
||||
#[post("/blog/posts/new")]
|
||||
async fn blog_new_post(form: Form<NewPostForm>) -> impl Responder {
|
||||
let token: String = std::env::var("SUBMIT_TOKEN").unwrap_or_else(|_| {
|
||||
panic!("Error, can't authenticate submission if no submit token was set.");
|
||||
});
|
||||
let token = config::get_from_env("SUBMIT_TOKEN", true);
|
||||
|
||||
if form.token == token {
|
||||
add_post(&form.title.as_str(), &form.body.as_str());
|
||||
|
|
Loading…
Reference in a new issue