opti: Replace Arc with &'static

This commit is contained in:
Pascal Engélibert 2022-10-29 11:03:37 +02:00
commit 0d7356ec1f
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
6 changed files with 61 additions and 81 deletions

View file

@ -3,65 +3,40 @@ use crate::{config::*, db::*, helpers, queries::*, templates::*};
use argon2::{Argon2, PasswordHash, PasswordVerifier};
use crossbeam_channel::Sender;
use log::{error, warn};
use std::sync::Arc;
use tera::Context;
pub async fn run_server(config: Arc<Config>, dbs: Dbs, templates: Arc<Templates>) {
pub async fn run_server(config: &'static Config, dbs: Dbs, templates: &'static Templates) {
tide::log::start();
let (notify_send, notify_recv) = crossbeam_channel::bounded(10);
tokio::spawn(crate::notify::run_notifier(config.clone(), notify_recv));
tokio::spawn(crate::notify::run_notifier(config, notify_recv));
let mut app = tide::new();
app.at(&format!("{}t/:topic", config.root_url)).get({
let config = config.clone();
let templates = templates.clone();
let dbs = dbs.clone();
move |req: tide::Request<()>| {
serve_comments(
req,
config.clone(),
templates.clone(),
dbs.clone(),
Context::new(),
200,
)
serve_comments(req, config, templates, dbs.clone(), Context::new(), 200)
}
});
app.at(&format!("{}t/:topic", config.root_url)).post({
let config = config.clone();
let templates = templates.clone();
let dbs = dbs.clone();
move |req: tide::Request<()>| {
handle_post_comments(
req,
config.clone(),
templates.clone(),
dbs.clone(),
notify_send.clone(),
)
handle_post_comments(req, config, templates, dbs.clone(), notify_send.clone())
}
});
app.at(&format!("{}admin", config.root_url)).get({
let config = config.clone();
let templates = templates.clone();
move |req: tide::Request<()>| serve_admin_login(req, config.clone(), templates.clone())
});
app.at(&format!("{}admin", config.root_url))
.get(move |req: tide::Request<()>| serve_admin_login(req, config, templates));
app.at(&format!("{}admin", config.root_url)).post({
let config = config.clone();
let templates = templates.clone();
let dbs = dbs.clone();
move |req: tide::Request<()>| {
handle_post_admin(req, config.clone(), templates.clone(), dbs.clone())
}
move |req: tide::Request<()>| handle_post_admin(req, config, templates, dbs.clone())
});
app.listen(config.listen).await.unwrap();
}
async fn serve_comments<'a>(
req: tide::Request<()>,
config: Arc<Config>,
templates: Arc<Templates>,
config: &Config,
templates: &Templates,
dbs: Dbs,
mut context: Context,
status_code: u16,
@ -71,7 +46,7 @@ async fn serve_comments<'a>(
};
let admin = req.cookie("admin").map_or(false, |psw| {
check_admin_password_hash(&config, &String::from(psw.value()))
check_admin_password_hash(config, &String::from(psw.value()))
});
let topic_hash = TopicHash::from_topic(topic);
@ -142,8 +117,8 @@ async fn serve_comments<'a>(
async fn serve_admin<'a>(
_req: tide::Request<()>,
config: Arc<Config>,
templates: Arc<Templates>,
config: &Config,
templates: &Templates,
dbs: Dbs,
) -> tide::Result<tide::Response> {
let mut context = Context::new();
@ -187,8 +162,8 @@ async fn serve_admin<'a>(
async fn serve_admin_login(
_req: tide::Request<()>,
config: Arc<Config>,
templates: Arc<Templates>,
config: &Config,
templates: &Templates,
) -> tide::Result<tide::Response> {
let mut context = Context::new();
context.insert("config", &config);
@ -201,17 +176,17 @@ async fn serve_admin_login(
async fn handle_post_comments(
mut req: tide::Request<()>,
config: Arc<Config>,
templates: Arc<Templates>,
config: &Config,
templates: &Templates,
dbs: Dbs,
notify_send: Sender<()>,
) -> tide::Result<tide::Response> {
let admin = req.cookie("admin").map_or(false, |psw| {
check_admin_password_hash(&config, &String::from(psw.value()))
check_admin_password_hash(config, &String::from(psw.value()))
});
let client_addr = if !admin && config.antispam_enable {
match helpers::get_client_addr(&config, &req) {
match helpers::get_client_addr(config, &req) {
Some(Ok(addr)) => {
if config.antispam_whitelist.contains(&addr) {
None
@ -241,11 +216,11 @@ async fn handle_post_comments(
return Err(tide::Error::from_str(404, "No topic"))
};
helpers::check_comment(&config, &query.comment, &mut errors);
helpers::check_comment(config, &query.comment, &mut errors);
if let Some(client_addr) = &client_addr {
if let Some(antispam_timeout) =
helpers::antispam_check_client_mutation(client_addr, &dbs, &config).unwrap()
helpers::antispam_check_client_mutation(client_addr, &dbs, config).unwrap()
{
errors.push(format!(
"The edition quota from your IP is reached. You will be unblocked in {}s.",
@ -294,7 +269,7 @@ async fn handle_post_comments(
return Err(tide::Error::from_str(403, "Forbidden"));
}
helpers::check_comment(&config, &query.comment, &mut errors);
helpers::check_comment(config, &query.comment, &mut errors);
let comment_id = if let Ok(comment_id) = CommentId::from_base64(&query.id) {
comment_id
@ -310,7 +285,7 @@ async fn handle_post_comments(
if let Some(client_addr) = &client_addr {
if let Some(antispam_timeout) =
helpers::antispam_check_client_mutation(client_addr, &dbs, &config).unwrap()
helpers::antispam_check_client_mutation(client_addr, &dbs, config).unwrap()
{
errors.push(format!(
"The edition quota from your IP is reached. You will be unblocked in {}s.",
@ -361,12 +336,12 @@ async fn handle_post_comments(
async fn handle_post_admin(
mut req: tide::Request<()>,
config: Arc<Config>,
templates: Arc<Templates>,
config: &Config,
templates: &Templates,
dbs: Dbs,
) -> tide::Result<tide::Response> {
if let Some(psw) = req.cookie("admin") {
if check_admin_password(&config, &String::from(psw.value())).is_some() {
if check_admin_password(config, &String::from(psw.value())).is_some() {
#[allow(clippy::match_single_binding)]
match req.body_form::<AdminQuery>().await? {
_ => serve_admin(req, config, templates, dbs).await,
@ -375,22 +350,20 @@ async fn handle_post_admin(
serve_admin_login(req, config, templates).await
}
} else if let AdminQuery::Login(query) = req.body_form::<AdminQuery>().await? {
if let Some(password_hash) = check_admin_password(&config, &query.psw) {
serve_admin(req, config.clone(), templates, dbs)
.await
.map(|mut r| {
let mut cookie = tide::http::Cookie::new("admin", password_hash);
cookie.set_http_only(Some(true));
cookie.set_path(config.root_url.clone());
if let Some(domain) = &config.cookies_domain {
cookie.set_domain(domain.clone());
}
if config.cookies_https_only {
cookie.set_secure(Some(true));
}
r.insert_cookie(cookie);
r
})
if let Some(password_hash) = check_admin_password(config, &query.psw) {
serve_admin(req, config, templates, dbs).await.map(|mut r| {
let mut cookie = tide::http::Cookie::new("admin", password_hash);
cookie.set_http_only(Some(true));
cookie.set_path(config.root_url.clone());
if let Some(domain) = &config.cookies_domain {
cookie.set_domain(domain.clone());
}
if config.cookies_https_only {
cookie.set_secure(Some(true));
}
r.insert_cookie(cookie);
r
})
} else {
serve_admin_login(req, config, templates).await
}