// Copyright (C) 2022 Aravinth Manivannan // SPDX-FileCopyrightText: 2023 Aravinth Manivannan // // SPDX-License-Identifier: AGPL-3.0-or-later use std::env; use std::sync::Arc; use actix_web::{ error::InternalError, http::StatusCode, middleware as actix_middleware, web::Data as WebData, web::JsonConfig, App, HttpServer, }; //use clap::{Parser, Subcommand}; //use static_assets::FileMap; use tracing::info; use tracing_actix_web::TracingLogger; // //pub use crate::api::v1::ROUTES as V1_API_ROUTES; use ctx::Ctx; pub use settings::Settings; pub const CACHE_AGE: u32 = 604800; pub const GIT_COMMIT_HASH: &str = env!("GIT_HASH"); pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const PKG_NAME: &str = env!("CARGO_PKG_NAME"); pub const PKG_DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION"); pub const PKG_HOMEPAGE: &str = env!("CARGO_PKG_HOMEPAGE"); pub type AppCtx = WebData; mod api; mod complaince; mod ctx; mod db; mod docker; mod docker_compose; mod errors; mod git; mod runner; mod settings; mod utils; //lazy_static::lazy_static! { // pub static ref FILES: FileMap = FileMap::new(); //} // //#[derive(Parser)] //#[clap(author, version, about, long_about = None)] //struct Cli { // #[clap(subcommand)] // command: Commands, //} #[actix_web::main] #[cfg(not(tarpaulin_include))] async fn main() -> std::io::Result<()> { if env::var("RUST_LOG").is_err() { env::set_var("RUST_LOG", "info"); } pretty_env_logger::init(); // let cli = Cli::parse(); info!( "{}: {}.\nFor more information, see: {}\nBuild info:\nVersion: {} commit: {}", PKG_NAME, PKG_DESCRIPTION, PKG_HOMEPAGE, VERSION, GIT_COMMIT_HASH ); let settings = Settings::new().unwrap(); settings.init(); let ctx = AppCtx::new(Arc::new(Ctx::new(settings.clone()).await)); ctx.db.migrate().await.unwrap(); serve(settings, ctx).await?; Ok(()) } async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> { let ip = settings.server.get_ip(); let workers = settings.server.workers.unwrap_or_else(num_cpus::get); let scheduler = runner::Scheduler::spawn(ctx.clone()).await; info!("Starting server on: http://{}", ip); HttpServer::new(move || { App::new() .wrap(TracingLogger::default()) .wrap(actix_middleware::Compress::default()) .app_data(ctx.clone()) .app_data(get_json_err()) .wrap( actix_middleware::DefaultHeaders::new() .add(("Permissions-Policy", "interest-cohort=()")), ) .wrap(actix_middleware::NormalizePath::new( actix_middleware::TrailingSlash::Trim, )) .configure(services) }) .workers(workers) .bind(ip) .unwrap() .run() .await?; scheduler.stop().await; Ok(()) } #[cfg(not(tarpaulin_include))] pub fn get_json_err() -> JsonConfig { JsonConfig::default().error_handler(|err, _| { //debug!("JSON deserialization error: {:?}", &err); InternalError::new(err, StatusCode::BAD_REQUEST).into() }) } pub fn services(cfg: &mut actix_web::web::ServiceConfig) { crate::api::v1::services(cfg); }