dev: monolithic app state

reasons stated at the top of such mod.rs file
This commit is contained in:
2026-03-26 23:42:47 +01:00
parent 4785ab529c
commit bcec6af49b
14 changed files with 331 additions and 280 deletions

View File

@@ -2,55 +2,30 @@ use std::io;
use actix_web::{App, HttpServer, middleware, web};
pub mod caches;
pub mod services;
pub mod state;
pub mod static_app_data;
pub struct AppState {
pub args: crate::args::Args,
pub config: &'static crate::conf::Config,
pub cache: caches::AppCache<'static>,
pub db: crate::db::DB,
}
/// Type alias to be used just as `data: AppData,` extractor in [`actix_web`] request handlers.
pub type AppData = static_app_data::StaticAppDataExtractor<&'static AppState>;
/// Leaks memory for the sake of not atomic'ing all over.
#[expect(clippy::missing_errors_doc)]
pub async fn start_app(
args: crate::args::Args,
config: crate::conf::Config,
db: crate::db::DB,
) -> io::Result<()> {
pub async fn start_app(state: &'static state::AppState) -> io::Result<()> {
use crate::consts::web_scopes as ws;
let config = Box::leak(Box::new(config));
let cache = caches::AppCache::new(&config.unix.magic_paths, &config.unix.groups);
let app: &AppState = Box::leak(Box::new(AppState {
args,
config,
cache,
db,
}));
println!(
"\x1b[34mINF\x1b[0m: Trying to listen on \x1b[35m{:?}\x1b[0m",
app.config.server.listen
state.config.server.listen
);
HttpServer::new(move || {
App::new()
.app_data(app)
.app_data(state)
.wrap(middleware::Logger::new("%a (%{r}a) %r -> %s, %b B in %T s"))
.wrap(middleware::NormalizePath::trim())
.service(services::images::make_scope(ws::IMAGES))
.service(services::login::make_scope(ws::LOGIN))
.default_service(web::to(services::not_found::not_found))
})
.bind(&app.config.server.listen)?
.bind(&state.config.server.listen)?
.run()
.await
}