webcomment/src/server.rs

55 lines
1.5 KiB
Rust
Raw Normal View History

2022-12-04 09:12:34 +00:00
#![allow(clippy::too_many_arguments)]
2023-01-08 00:26:33 +00:00
pub mod api;
pub mod page;
use crate::{config::*, db::*, locales::*};
2022-10-15 13:32:57 +00:00
2022-10-15 15:32:35 +00:00
use argon2::{Argon2, PasswordHash, PasswordVerifier};
2022-10-15 13:32:57 +00:00
2022-12-04 09:12:34 +00:00
pub async fn run_server(
config: &'static Config,
dbs: Dbs,
2023-01-08 00:26:33 +00:00
templates: &'static page::templates::Templates,
2022-12-04 09:12:34 +00:00
locales: &'static Locales,
) {
2022-10-15 13:32:57 +00:00
tide::log::start();
2022-10-15 21:48:06 +00:00
let (notify_send, notify_recv) = crossbeam_channel::bounded(10);
2022-10-29 09:03:37 +00:00
tokio::spawn(crate::notify::run_notifier(config, notify_recv));
2022-10-15 21:48:06 +00:00
2022-10-15 13:32:57 +00:00
let mut app = tide::new();
2022-10-22 15:56:24 +00:00
2023-01-08 00:26:33 +00:00
// CORS sucks
app.at(&format!("{}*", config.root_url))
.options(|_req: tide::Request<()>| async {
Ok(tide::Response::builder(200)
.header("Access-Control-Allow-Origin", &config.cors_allow_origin)
.header("Access-Control-Allow-Headers", "*")
.build())
});
2022-10-22 15:56:24 +00:00
2023-01-11 21:56:32 +00:00
api::init_routes(&mut app, config, dbs.clone(), notify_send.clone()).await;
2023-01-08 00:26:33 +00:00
page::init_routes(&mut app, config, dbs, templates, locales, notify_send).await;
2022-12-07 18:06:08 +00:00
2023-01-08 00:26:33 +00:00
app.listen(config.listen).await.unwrap();
2022-10-15 13:32:57 +00:00
}
2022-10-15 15:32:35 +00:00
fn check_admin_password(config: &Config, password: &str) -> Option<String> {
let argon2 = Argon2::default();
config
.admin_passwords
.iter()
.filter_map(|admin_password| PasswordHash::new(admin_password).ok())
.find(|admin_password| {
argon2
.verify_password(password.as_bytes(), admin_password)
.is_ok()
})
.map(|password_hash| password_hash.to_string())
}
fn check_admin_password_hash(config: &Config, password_hash: &str) -> bool {
config.admin_passwords.iter().any(|h| h == password_hash)
}