crablog/site/src/main.rs

70 lines
2.5 KiB
Rust
Raw Normal View History

2020-10-25 23:23:32 +01:00
mod api;
2020-10-22 00:17:52 +02:00
mod db;
mod routes;
#[macro_use]
extern crate diesel;
extern crate chrono;
extern crate serde_derive;
extern crate tera;
use actix_files as fs;
2020-10-22 23:51:20 +02:00
use actix_web::{App, HttpServer};
use tera::Tera;
use std::{env, sync::RwLock, collections::HashMap};
use once_cell::sync::Lazy;
pub static CONFIG_MAP: Lazy<RwLock<HashMap<String, String>>> = Lazy::new(|| {
let mut config: HashMap<String, String> = HashMap::new();
config.insert(String::from("SUBMIT_TOKEN"), env::var("SUBMIT_TOKEN").expect("SUBMIT_TOKEN variable not set."));
config.insert(String::from("ROOT_PATH"), env::var("ROOT_PATH").expect("ROOT_PATH variable not set."));
config.insert(String::from("USERNAME"), env::var("USERNAME").expect("USERNAME variable not set."));
config.insert(String::from("EMAIL"), env::var("EMAIL").expect("EMAIL variable not set."));
config.insert(String::from("BIND_PORT"), env::var("BIND_PORT").expect("BIND_PORT variable not set."));
2020-11-21 22:35:36 +01:00
if let Ok(acc) = env::var("GITHUB_ACCOUNT") {
config.insert(String::from("GITHUB_ACCOUNT"), acc.clone());
}
if let Ok(acc) = env::var("TWITTER_ACCOUNT") {
config.insert(String::from("TWITTER_ACCOUNT"), acc.clone());
}
if let Ok(acc) = env::var("MASTODON_ACCOUNT") {
config.insert(String::from("MASTODON_ACCOUNT"), acc.clone());
}
if let Ok(acc) = env::var("DISCORD_ACCOUNT") {
config.insert(String::from("DISCORD_ACCOUNT"), acc.clone());
}
if let Ok(acc) = env::var("REDDIT_ACCOUNT") {
config.insert(String::from("REDDIT_ACCOUNT"), acc.clone());
}
RwLock::new(config)
});
2020-10-22 00:17:52 +02:00
#[actix_web::main]
async fn main() -> std::io::Result<()> {
2020-10-22 00:17:52 +02:00
HttpServer::new(|| {
let mut tera = Tera::new(format!("{}{}", CONFIG_MAP.read().unwrap().get("ROOT_PATH").unwrap(), "/templates/*").as_str()).unwrap();
tera.autoescape_on(vec![".sql"]);
2020-10-22 00:17:52 +02:00
App::new()
.data(tera)
2020-10-22 00:17:52 +02:00
.service(routes::root)
.service(routes::blog)
.service(routes::blog_all)
2020-10-25 23:23:32 +01:00
.service(routes::blog_by_id)
.service(routes::blog_submit)
2020-10-25 23:23:32 +01:00
.service(routes::blog_edit)
.service(routes::blog_edit_by_id)
.service(api::blog_get_posts_json)
.service(api::blog_create_post)
.service(api::blog_edit_post)
.service(api::blog_hide_post)
.service(api::blog_delete_post)
.service(fs::Files::new("/static", "../content/static"))
2020-10-22 00:17:52 +02:00
})
.bind(format!("0.0.0.0:{}", CONFIG_MAP.read().unwrap().get("BIND_PORT").unwrap()))?
2020-10-22 00:17:52 +02:00
.run()
.await
}