51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
use std::io;
|
|
|
|
use actix_web::{App, HttpServer, web};
|
|
|
|
pub mod caches;
|
|
pub mod services;
|
|
pub mod static_app_data;
|
|
|
|
pub struct AppState {
|
|
pub args: crate::args::Args,
|
|
pub config: &'static crate::conf::Config,
|
|
pub cache: caches::AppCache<'static>,
|
|
}
|
|
|
|
/// 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) -> 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,
|
|
}));
|
|
|
|
println!(
|
|
"\x1b[34mINF\x1b[0m: Trying to listen on \x1b[35m{:?}\x1b[0m",
|
|
app.config.server.listen
|
|
);
|
|
|
|
HttpServer::new(move || {
|
|
App::new()
|
|
.app_data(app)
|
|
.wrap(actix_web::middleware::Logger::new(
|
|
"%a (%{r}a) %r -> %s, %b B in %T s",
|
|
))
|
|
.service(services::images::make_scope(ws::IMAGES))
|
|
.default_service(web::to(services::not_found::not_found))
|
|
})
|
|
.bind(&app.config.server.listen)?
|
|
.run()
|
|
.await
|
|
}
|