Update 2024 #1

Open
mtrx wants to merge 11 commits from update-2024 into main
6 changed files with 234 additions and 247 deletions
Showing only changes of commit 35cd018152 - Show all commits

249
site/Cargo.lock generated
View file

@ -87,7 +87,7 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb"
dependencies = [ dependencies = [
"quote 1.0.36", "quote",
"syn 2.0.63", "syn 2.0.63",
] ]
@ -200,7 +200,7 @@ checksum = "eb1f50ebbb30eca122b188319a4398b3f7bb4a8cdf50ecfb73bfc6a3c3ce54f5"
dependencies = [ dependencies = [
"actix-router", "actix-router",
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"syn 2.0.63", "syn 2.0.63",
] ]
@ -272,14 +272,52 @@ dependencies = [
] ]
[[package]] [[package]]
name = "atty" name = "anstream"
version = "0.2.14" version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b"
dependencies = [ dependencies = [
"hermit-abi", "anstyle",
"libc", "anstyle-parse",
"winapi", "anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b"
[[package]]
name = "anstyle-parse"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -367,12 +405,6 @@ version = "3.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "bytes" name = "bytes"
version = "1.6.0" version = "1.6.0"
@ -442,6 +474,12 @@ dependencies = [
"phf_codegen", "phf_codegen",
] ]
[[package]]
name = "colorchoice"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
[[package]] [[package]]
name = "convert_case" name = "convert_case"
version = "0.4.0" version = "0.4.0"
@ -481,8 +519,8 @@ dependencies = [
"actix-files", "actix-files",
"actix-web", "actix-web",
"chrono", "chrono",
"diesel 1.4.8", "diesel",
"diesel_codegen", "dotenvy",
"env_logger", "env_logger",
"once_cell", "once_cell",
"serde", "serde",
@ -553,7 +591,7 @@ checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
dependencies = [ dependencies = [
"convert_case", "convert_case",
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"rustc_version", "rustc_version",
"syn 1.0.109", "syn 1.0.109",
] ]
@ -566,47 +604,34 @@ checksum = "339544cc9e2c4dc3fc7149fd630c5f22263a4fdf18a98afd0075784968b5cf00"
[[package]] [[package]]
name = "diesel" name = "diesel"
version = "0.16.0" version = "2.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "304226fa7a3982b0405f6bb95dd9c10c3e2000709f194038a60ec2c277150951" checksum = "ff236accb9a5069572099f0b350a92e9560e8e63a9b8d546162f4a5e03026bb2"
dependencies = [ dependencies = [
"byteorder",
]
[[package]]
name = "diesel"
version = "1.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b28135ecf6b7d446b43e27e225622a038cc4e2930a1022f51cdb97ada19b8e4d"
dependencies = [
"byteorder",
"chrono",
"diesel_derives", "diesel_derives",
"libsqlite3-sys", "libsqlite3-sys",
] "time",
[[package]]
name = "diesel_codegen"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22006237aee4465b49fb3a0248310a0aaefa6f165fc63c5f6e67810c63521c31"
dependencies = [
"diesel 0.16.0",
"quote 0.3.15",
"serde",
"serde_json",
"syn 0.11.11",
] ]
[[package]] [[package]]
name = "diesel_derives" name = "diesel_derives"
version = "1.4.1" version = "2.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3" checksum = "14701062d6bed917b5c7103bdffaee1e4609279e240488ad24e7bd979ca6866c"
dependencies = [ dependencies = [
"diesel_table_macro_syntax",
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"syn 1.0.109", "syn 2.0.63",
]
[[package]]
name = "diesel_table_macro_syntax"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5"
dependencies = [
"syn 2.0.63",
] ]
[[package]] [[package]]
@ -619,6 +644,12 @@ dependencies = [
"crypto-common", "crypto-common",
] ]
[[package]]
name = "dotenvy"
version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
[[package]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.34" version = "0.8.34"
@ -629,16 +660,26 @@ dependencies = [
] ]
[[package]] [[package]]
name = "env_logger" name = "env_filter"
version = "0.8.4" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea"
dependencies = [ dependencies = [
"atty",
"humantime",
"log", "log",
"regex", "regex",
"termcolor", ]
[[package]]
name = "env_logger"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"humantime",
"log",
] ]
[[package]] [[package]]
@ -778,15 +819,6 @@ version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.12" version = "0.2.12"
@ -890,6 +922,12 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.11" version = "1.0.11"
@ -1128,7 +1166,7 @@ dependencies = [
"pest", "pest",
"pest_meta", "pest_meta",
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"syn 2.0.63", "syn 2.0.63",
] ]
@ -1220,12 +1258,6 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "quote"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.36" version = "1.0.36"
@ -1361,7 +1393,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5e405930b9796f1c00bee880d03fc7e0bb4b9a11afc776885ffe84320da2865" checksum = "c5e405930b9796f1c00bee880d03fc7e0bb4b9a11afc776885ffe84320da2865"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"syn 2.0.63", "syn 2.0.63",
] ]
@ -1466,17 +1498,6 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "syn"
version = "0.11.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
dependencies = [
"quote 0.3.15",
"synom",
"unicode-xid",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.109" version = "1.0.109"
@ -1484,7 +1505,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"unicode-ident", "unicode-ident",
] ]
@ -1495,19 +1516,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf5be731623ca1a1fb7d8be6f261a3be6d3e2337b8a1f97be944d020c8fcb704" checksum = "bf5be731623ca1a1fb7d8be6f261a3be6d3e2337b8a1f97be944d020c8fcb704"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "synom"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
dependencies = [
"unicode-xid",
]
[[package]] [[package]]
name = "tera" name = "tera"
version = "1.19.1" version = "1.19.1"
@ -1530,15 +1542,6 @@ dependencies = [
"unic-segment", "unic-segment",
] ]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.60" version = "1.0.60"
@ -1555,7 +1558,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524" checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"syn 2.0.63", "syn 2.0.63",
] ]
@ -1747,12 +1750,6 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-xid"
version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
[[package]] [[package]]
name = "url" name = "url"
version = "2.5.0" version = "2.5.0"
@ -1764,6 +1761,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.8.0" version = "1.8.0"
@ -1828,7 +1831,7 @@ dependencies = [
"log", "log",
"once_cell", "once_cell",
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"syn 2.0.63", "syn 2.0.63",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
@ -1839,7 +1842,7 @@ version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
dependencies = [ dependencies = [
"quote 1.0.36", "quote",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
] ]
@ -1850,7 +1853,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"syn 2.0.63", "syn 2.0.63",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
@ -1862,22 +1865,6 @@ version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]] [[package]]
name = "winapi-util" name = "winapi-util"
version = "0.1.8" version = "0.1.8"
@ -1887,12 +1874,6 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "windows-core" name = "windows-core"
version = "0.52.0" version = "0.52.0"
@ -2057,7 +2038,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b" checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.36", "quote",
"syn 2.0.63", "syn 2.0.63",
] ]

View file

@ -2,7 +2,7 @@
name = "crablog" name = "crablog"
version = "0.4.0" version = "0.4.0"
authors = ["mtrx <me@mtrx.tech>"] authors = ["mtrx <me@mtrx.tech>"]
edition = "2018" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -15,13 +15,13 @@ serde = { version = "1.0.201", features = ["derive"] }
serde_json = "*" serde_json = "*"
serde_derive = "*" serde_derive = "*"
diesel = { version = "1.4.6", default-features = false, features = ["sqlite", "chrono"] } diesel = { version = "2.1.0", default-features = false, features = ["sqlite"] }
diesel_codegen = { version = "0.16.1", default-features = false } dotenvy = "0.15"
uuid = { version = "1.8.0", features = ["serde", "v5"] } uuid = { version = "1.8.0", features = ["serde", "v5"] }
tera = "1.8.0" tera = "1.18.1"
once_cell = "1.7.2" once_cell = "1.7.2"
env_logger = "0.8.3" env_logger = "0.11.3"

View file

@ -1,8 +1,8 @@
use crate::config::CONFIG;
use crate::db::*; use crate::db::*;
use crate::routes::{id_valid, replace_newlines}; use crate::routes::{id_valid, replace_newlines};
use actix_web::{get, http::StatusCode, post, web, web::Form, HttpResponse, Responder}; use actix_web::{get, http::StatusCode, post, web, web::Form, HttpResponse, Responder};
use serde::Deserialize; use serde::Deserialize;
use super::CONFIG_MAP;
#[derive(Deserialize)] #[derive(Deserialize)]
struct NewPostForm { struct NewPostForm {
@ -18,7 +18,7 @@ struct BlogActionForm {
#[post("/api/blog/create")] #[post("/api/blog/create")]
async fn blog_create_post(form: Form<NewPostForm>) -> impl Responder { async fn blog_create_post(form: Form<NewPostForm>) -> impl Responder {
if *CONFIG_MAP.read().unwrap().get("SUBMIT_TOKEN").unwrap() == form.token { if CONFIG.submit_token == form.token {
create_post(&form.title.as_str(), replace_newlines(&form.body).as_str()); create_post(&form.title.as_str(), replace_newlines(&form.body).as_str());
println!("New blog post created."); println!("New blog post created.");
} else { } else {
@ -36,7 +36,7 @@ async fn blog_edit_post(
form: Form<NewPostForm>, form: Form<NewPostForm>,
) -> impl Responder { ) -> impl Responder {
let (valid, id) = id_valid(post_id.into_inner()); let (valid, id) = id_valid(post_id.into_inner());
if valid && *CONFIG_MAP.read().unwrap().get("SUBMIT_TOKEN").unwrap() == form.token { if valid && CONFIG.submit_token == form.token {
edit_post_by_id( edit_post_by_id(
id as i32, id as i32,
&form.title.as_str(), &form.title.as_str(),
@ -59,7 +59,7 @@ async fn blog_delete_post(
form: Form<BlogActionForm>, form: Form<BlogActionForm>,
) -> impl Responder { ) -> impl Responder {
let (valid, id) = id_valid(post_id.into_inner()); let (valid, id) = id_valid(post_id.into_inner());
if valid && *CONFIG_MAP.read().unwrap().get("SUBMIT_TOKEN").unwrap() == form.token { if valid && CONFIG.submit_token == form.token {
println!("Deleted post: {}", id); println!("Deleted post: {}", id);
delete_post_by_id(id as i32); delete_post_by_id(id as i32);
} else { } else {
@ -78,7 +78,7 @@ async fn blog_hide_post(
form: Form<BlogActionForm>, form: Form<BlogActionForm>,
) -> impl Responder { ) -> impl Responder {
let (valid, id) = id_valid(post_id.into_inner()); let (valid, id) = id_valid(post_id.into_inner());
if valid && *CONFIG_MAP.read().unwrap().get("SUBMIT_TOKEN").unwrap() == form.token { if valid && CONFIG.submit_token == form.token {
println!("Hid post: {}", id); println!("Hid post: {}", id);
hide_post_by_id(id as i32); hide_post_by_id(id as i32);
} else { } else {

72
site/src/config.rs Normal file
View file

@ -0,0 +1,72 @@
use std::sync::Mutex;
use dotenvy::dotenv;
use once_cell::sync::Lazy;
struct Config {
pub submit_token: String,
pub root_path: String,
pub username: String,
pub email: String,
pub bind_port: String,
pub accounts: Accounts
}
struct Accounts {
pub github: Option<String>,
pub twitter: Option<String>,
pub mastodon: Option<String>,
pub discord: Option<String>,
pub reddit: Option<String>,
}
fn load_config() -> Config {
dotenv().expect(".env file not found");
// return config value or panic if not set
let eval_required_conf = |variable_name| {
match std::env::var(variable_name) {
Ok(_) => {
let value = std::env::var(variable_name).unwrap();
println!("{}: {}", variable_name, value);
return value;
},
Err(_) => {
panic!("{} not set!", variable_name)
}
}
};
// return optional value
let eval_optional_conf = |variable_name| {
match std::env::var(variable_name) {
Ok(_) => {
let value = std::env::var(variable_name).unwrap();
println!("{}: {}", variable_name, value);
return Some(value);
},
Err(_) => {
return None
}
}
};
Config {
submit_token: eval_required_conf("SUBMIT_TOKEN"),
root_path: eval_required_conf("ROOT_PATH"),
username: eval_required_conf("USERNAME"),
email: eval_required_conf("EMAIL"),
bind_port: eval_required_conf("BIND_PORT"),
accounts: Accounts {
github: eval_optional_conf("GITHUB_ACCOUNT"),
discord: eval_optional_conf("DISCORD_ACCOUNT"),
twitter: eval_optional_conf("TWITTER_ACCOUNT"),
mastodon: eval_optional_conf("MASTODON_ACCOUNT"),
reddit: eval_optional_conf("REDDIT_ACCOUNT"),
}
}
}
pub static CONFIG: Lazy<Config> = Lazy::new(|| {
load_config()
});

View file

@ -1,6 +1,7 @@
mod api; mod api;
mod db; mod db;
mod routes; mod routes;
mod config;
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
@ -10,76 +11,18 @@ extern crate tera;
use actix_files as fs; use actix_files as fs;
use actix_web::{middleware::Logger, web::Data, App, HttpServer}; use actix_web::{middleware::Logger, web::Data, App, HttpServer};
use std::env;
use env_logger::Env; use env_logger::Env;
use once_cell::sync::Lazy;
use std::{collections::HashMap, env, sync::RwLock};
use tera::Tera; use tera::Tera;
use config::CONFIG;
pub static CONFIG_MAP: Lazy<RwLock<HashMap<String, String>>> = Lazy::new(|| {
let mut config: HashMap<String, String> = HashMap::new();
let required_env_vars = [
"SUBMIT_TOKEN",
"ROOT_PATH",
"USERNAME",
"EMAIL",
"BIND_PORT",
];
let optional_env_vars = [
"GITHUB_ACCOUNT",
"TWITTER_ACCOUNT",
"MASTODON_ACCOUNT",
"DISCORD_ACCOUNT",
"REDDIT_ACCOUNT",
];
// Test if variable is set. If not, panic.
let mut insert_required_env = |env: &str| {
let env_string = String::from(env);
config.insert(
env_string.clone(), // env var name
env::var(env_string).expect(format!("`{}` variable not set.", env).as_str()), // env var content
)
};
for var in required_env_vars.iter() {
insert_required_env(var);
}
// Test if variable is set. If it is insert into config.
let mut insert_optional_env = |env: &str| {
if let Ok(var_content) = env::var(String::from(env)) {
config.insert(String::from(env), var_content.clone());
}
};
for var in optional_env_vars.iter() {
insert_optional_env(var);
}
// Print some info about the current configuration
println!("Submit token = `{}`", config.get("SUBMIT_TOKEN").unwrap());
println!(
"Current working directory = `{}`",
env::current_dir().unwrap().to_str().unwrap()
);
println!("Root path = `{}`", config.get("ROOT_PATH").unwrap());
println!(
"Template path = `{}/templates/*`",
config.get("ROOT_PATH").unwrap()
);
println!("Launching on 0.0.0.0:{}", config.get("BIND_PORT").unwrap());
RwLock::new(config)
});
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
HttpServer::new(|| { HttpServer::new(move || {
let mut tera = Tera::new( let mut tera = Tera::new(
format!( format!(
"{}{}", "{}{}",
CONFIG_MAP.read().unwrap().get("ROOT_PATH").unwrap(), env::var("ROOT_PATH").unwrap(),
"/templates/*" "/templates/*"
) )
.as_str(), .as_str(),
@ -107,16 +50,13 @@ async fn main() -> std::io::Result<()> {
"/static", "/static",
format!( format!(
"{}{}", "{}{}",
CONFIG_MAP.read().unwrap().get("ROOT_PATH").unwrap(), CONFIG.root_path,
"/static" "/static"
), ),
)) ))
.wrap(Logger::new("%a %r %t")) .wrap(Logger::new("%a %r %t"))
}) })
.bind(format!( .bind(format!("0.0.0.0:{}", CONFIG.bind_port))?
"0.0.0.0:{}",
CONFIG_MAP.read().unwrap().get("BIND_PORT").unwrap()
))?
.run() .run()
.await .await
} }

View file

@ -1,6 +1,6 @@
use crate::db; use crate::db;
use super::CONFIG_MAP; use super::CONFIG;
use actix_web::{error, get, http::StatusCode, web, Error, HttpResponse}; use actix_web::{error, get, http::StatusCode, web, Error, HttpResponse};
use tera::Context; use tera::Context;
@ -38,24 +38,30 @@ async fn about(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
let mut context = Context::new(); let mut context = Context::new();
context.insert( context.insert(
"username", "username",
CONFIG_MAP.read().unwrap().get("USERNAME").unwrap(), &CONFIG.username
); );
context.insert("email", CONFIG_MAP.read().unwrap().get("EMAIL").unwrap()); context.insert("email", &CONFIG.email);
if let Some(acc) = CONFIG_MAP.read().unwrap().get("GITHUB_ACCOUNT") {
context.insert("github_account", acc); match &CONFIG.accounts.github {
} Some(acc) => context.insert("github_account", &acc),
if let Some(acc) = CONFIG_MAP.read().unwrap().get("TWITTER_ACCOUNT") { None => ()
context.insert("twitter_account", acc); };
} match &CONFIG.accounts.twitter {
if let Some(acc) = CONFIG_MAP.read().unwrap().get("MASTODON_ACCOUNT") { Some(acc) => context.insert("twitter_account", &acc),
context.insert("mastodon_account", acc); None => ()
} };
if let Some(acc) = CONFIG_MAP.read().unwrap().get("DISCORD_ACCOUNT") { match &CONFIG.accounts.mastodon {
context.insert("discord_account", acc); Some(acc) => context.insert("mastodon_account", &acc),
} None => ()
if let Some(acc) = CONFIG_MAP.read().unwrap().get("REDDIT_ACCOUNT") { };
context.insert("reddit_account", acc); match &CONFIG.accounts.reddit {
} Some(acc) => context.insert("reddit_account", &acc),
None => ()
};
match &CONFIG.accounts.discord {
Some(acc) => context.insert("discord_account", &acc),
None => ()
};
let result = tmpl let result = tmpl
.render("about.html", &context) .render("about.html", &context)
@ -70,10 +76,7 @@ async fn blog(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
let mut context = Context::new(); let mut context = Context::new();
context.insert("posts", &posts); context.insert("posts", &posts);
context.insert( context.insert("username", &CONFIG.username);
"username",
CONFIG_MAP.read().unwrap().get("USERNAME").unwrap(),
);
let result = tmpl let result = tmpl
.render("blog.html", &context) .render("blog.html", &context)
@ -88,10 +91,7 @@ async fn blog_all(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
let mut context = Context::new(); let mut context = Context::new();
context.insert("posts", &posts); context.insert("posts", &posts);
context.insert( context.insert("username", &CONFIG.username);
"username",
CONFIG_MAP.read().unwrap().get("USERNAME").unwrap(),
);
let result = tmpl let result = tmpl
.render("blog-all-posts.html", &context) .render("blog-all-posts.html", &context)
@ -103,7 +103,7 @@ async fn blog_all(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
#[get("/id/{post_id}")] #[get("/id/{post_id}")]
async fn blog_by_id( async fn blog_by_id(
tmpl: web::Data<tera::Tera>, tmpl: web::Data<tera::Tera>,
post_id: web::Path<(String)> post_id: web::Path<String>
// web::Path(post_id): web::Path<String>, // web::Path(post_id): web::Path<String>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let (valid, id) = id_valid(post_id.into_inner()); let (valid, id) = id_valid(post_id.into_inner());
@ -116,10 +116,7 @@ async fn blog_by_id(
let mut context = Context::new(); let mut context = Context::new();
context.insert("post", &post); context.insert("post", &post);
context.insert( context.insert("username", &CONFIG.username);
"username",
CONFIG_MAP.read().unwrap().get("USERNAME").unwrap(),
);
let result = tmpl let result = tmpl
.render("blog-by-id.html", &context) .render("blog-by-id.html", &context)
@ -148,10 +145,7 @@ async fn blog_submit(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error>
async fn blog_edit(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> { async fn blog_edit(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
let mut context = Context::new(); let mut context = Context::new();
context.insert("posts", &db::get_all_posts()); context.insert("posts", &db::get_all_posts());
context.insert( context.insert("username", &CONFIG.username);
"username",
CONFIG_MAP.read().unwrap().get("USERNAME").unwrap(),
);
let result = tmpl let result = tmpl
.render("edit.html", &context) .render("edit.html", &context)
@ -163,7 +157,7 @@ async fn blog_edit(tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
#[get("/edit/{post_id}")] #[get("/edit/{post_id}")]
async fn blog_edit_by_id( async fn blog_edit_by_id(
tmpl: web::Data<tera::Tera>, tmpl: web::Data<tera::Tera>,
post_id: web::Path<(String)> post_id: web::Path<String>
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let (valid, id) = id_valid(post_id.into_inner()); let (valid, id) = id_valid(post_id.into_inner());
if valid { if valid {