2023-11-28 19:20:12 +00:00
|
|
|
use std::collections::HashMap;
|
2023-11-27 16:36:44 +00:00
|
|
|
use std::net::TcpListener;
|
2023-11-28 19:20:12 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2023-11-21 13:58:01 +00:00
|
|
|
|
2023-11-29 13:27:46 +00:00
|
|
|
use actix_cors::Cors;
|
2023-11-19 17:54:27 +00:00
|
|
|
use actix_web::middleware::TrailingSlash;
|
2022-07-17 18:19:36 +00:00
|
|
|
use actix_web::{middleware, web, App, HttpServer};
|
2023-11-27 23:19:15 +00:00
|
|
|
use emgauwa_lib::db::DbController;
|
2023-11-28 19:20:12 +00:00
|
|
|
use emgauwa_lib::types::ConnectedControllersType;
|
2023-11-29 00:03:04 +00:00
|
|
|
use emgauwa_lib::utils::init_logging;
|
2021-11-05 15:32:30 +00:00
|
|
|
|
2023-11-29 13:27:46 +00:00
|
|
|
use crate::utils::drop_privileges;
|
|
|
|
|
|
|
|
mod handlers;
|
2023-11-19 17:54:27 +00:00
|
|
|
mod settings;
|
2023-11-27 18:36:53 +00:00
|
|
|
mod utils;
|
2021-11-04 22:37:16 +00:00
|
|
|
|
|
|
|
#[actix_web::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2023-11-22 19:06:20 +00:00
|
|
|
let settings = settings::init();
|
2023-11-29 00:03:04 +00:00
|
|
|
init_logging(&settings.logging.level);
|
2022-04-15 00:11:34 +00:00
|
|
|
|
2023-11-27 16:36:44 +00:00
|
|
|
let listener = TcpListener::bind(format!("{}:{}", settings.host, settings.port))
|
|
|
|
.expect("Error creating listener");
|
|
|
|
|
2023-11-27 18:36:53 +00:00
|
|
|
drop_privileges(&settings).expect("Error dropping privileges");
|
2023-11-27 16:36:44 +00:00
|
|
|
|
2023-11-22 19:06:20 +00:00
|
|
|
let pool = emgauwa_lib::db::init(&settings.database).await;
|
2022-04-15 00:11:34 +00:00
|
|
|
|
2023-11-27 23:19:15 +00:00
|
|
|
// This block is to ensure that the connection is dropped after use.
|
|
|
|
{
|
|
|
|
let mut conn = pool.acquire().await.unwrap();
|
|
|
|
DbController::all_inactive(&mut conn)
|
|
|
|
.await
|
|
|
|
.expect("Error setting all controllers inactive");
|
|
|
|
}
|
|
|
|
|
2023-11-28 19:20:12 +00:00
|
|
|
let connected_controllers: ConnectedControllersType = Arc::new(Mutex::new(HashMap::new()));
|
|
|
|
|
2023-11-23 02:36:14 +00:00
|
|
|
log::info!("Starting server on {}:{}", settings.host, settings.port);
|
2023-11-20 23:44:45 +00:00
|
|
|
HttpServer::new(move || {
|
2023-11-27 18:36:53 +00:00
|
|
|
let cors = Cors::default().allow_any_method().allow_any_header();
|
2023-11-24 00:10:33 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2022-07-19 22:38:09 +00:00
|
|
|
App::new()
|
2023-11-24 00:10:33 +00:00
|
|
|
.wrap(cors)
|
2022-07-19 22:38:09 +00:00
|
|
|
.wrap(middleware::Logger::default())
|
|
|
|
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
|
|
|
|
.app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler))
|
2023-11-20 23:44:45 +00:00
|
|
|
.app_data(web::Data::new(pool.clone()))
|
2023-11-28 19:20:12 +00:00
|
|
|
.app_data(web::Data::new(connected_controllers.clone()))
|
|
|
|
.service(handlers::v1::controllers::index)
|
2023-11-30 00:43:56 +00:00
|
|
|
.service(handlers::v1::controllers::show)
|
|
|
|
.service(handlers::v1::controllers::update)
|
|
|
|
.service(handlers::v1::controllers::delete)
|
2023-11-25 23:54:03 +00:00
|
|
|
.service(handlers::v1::relays::index)
|
2023-11-30 02:24:13 +00:00
|
|
|
.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)
|
2022-07-19 22:38:09 +00:00
|
|
|
.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)
|
2023-11-30 02:24:13 +00:00
|
|
|
.service(handlers::v1::tags::index)
|
2023-11-28 19:20:12 +00:00
|
|
|
.service(handlers::v1::ws::ws_controllers)
|
2022-07-19 22:38:09 +00:00
|
|
|
})
|
2023-11-27 16:36:44 +00:00
|
|
|
.listen(listener)?
|
2022-07-19 22:38:09 +00:00
|
|
|
.run()
|
|
|
|
.await
|
2021-11-04 22:37:16 +00:00
|
|
|
}
|