use std::net::TcpListener; use actix::{Actor, Arbiter}; use actix_cors::Cors; use actix_web::middleware::TrailingSlash; use actix_web::{middleware, web, App, HttpServer}; use emgauwa_common::db::DbController; use emgauwa_common::errors::EmgauwaError; use emgauwa_common::utils::{drop_privileges, init_logging}; use serde_json::Value; use utoipa_swagger_ui::SwaggerUi; use crate::app_state::AppState; mod app_state; mod handlers; mod settings; #[actix_web::main] async fn main() -> Result<(), std::io::Error> { let settings = settings::init()?; let listener = TcpListener::bind(format!("{}:{}", settings.server.host, settings.server.port))?; drop_privileges(&settings.permissions)?; init_logging(&settings.logging.level)?; let pool_size = 10; let pool = emgauwa_common::db::init(&settings.database, pool_size).await?; let mut conn = pool.acquire().await.map_err(EmgauwaError::from)?; DbController::all_inactive(&mut conn) .await .map_err(EmgauwaError::from)?; conn.close().await.map_err(EmgauwaError::from)?; let app_state_arbiter = Arbiter::with_tokio_rt(|| { tokio::runtime::Builder::new_multi_thread() .worker_threads(2) .enable_all() .build() .unwrap() }); let app_state_pool = pool.clone(); let app_state = Actor::start_in_arbiter(&app_state_arbiter.handle(), move |_| { AppState::new(app_state_pool) }); let api_v1_json: Value = serde_json::from_str(include_str!("../api.v1.json")).map_err(EmgauwaError::from)?; log::info!( "Starting server on {}:{}", settings.server.host, settings.server.port ); HttpServer::new(move || { let cors = Cors::default().allow_any_method().allow_any_header(); let origins = settings.origins.clone(); let cors = match settings.origins.is_empty() { true => cors.allow_any_origin(), false => cors.allowed_origin_fn(move |origin, _req_head| { origins.contains(&origin.to_str().unwrap_or_default().to_string()) }), }; App::new() .wrap(cors) .wrap(middleware::Logger::default()) .app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler)) .app_data(web::Data::new(pool.clone())) .app_data(web::Data::new(app_state.clone())) .service( SwaggerUi::new("/api/docs/{_:.*}") .external_urls_from_iter_unchecked([("/api/v1.json", api_v1_json.clone())]), ) .service(web::redirect("/api/docs", "/api/docs/")) .service( web::scope("/api/v1") .wrap(middleware::NormalizePath::new(TrailingSlash::Trim)) .service(handlers::v1::controllers::index) .service(handlers::v1::controllers::show) .service(handlers::v1::controllers::update) .service(handlers::v1::controllers::delete) .service(handlers::v1::relays::index) .service(handlers::v1::relays::tagged) .service(handlers::v1::relays::index_for_controller) .service(handlers::v1::relays::show_for_controller) .service(handlers::v1::relays::update_for_controller) .service(handlers::v1::relays::pulse) .service(handlers::v1::schedules::index) .service(handlers::v1::schedules::tagged) .service(handlers::v1::schedules::show) .service(handlers::v1::schedules::add) .service(handlers::v1::schedules::add_list) .service(handlers::v1::schedules::update) .service(handlers::v1::schedules::delete) .service(handlers::v1::tags::index) .service(handlers::v1::tags::show) .service(handlers::v1::tags::delete) .service(handlers::v1::tags::add) .service(handlers::v1::macros::index) .service(handlers::v1::macros::show) .service(handlers::v1::macros::add) .service(handlers::v1::macros::update) .service(handlers::v1::macros::delete) .service(handlers::v1::macros::execute) .service(handlers::v1::ws::ws_controllers) .service(handlers::v1::ws::ws_relays), ) }) .workers((pool_size / 2) as usize) .listen(listener)? .run() .await }