2023-11-27 16:36:44 +00:00
|
|
|
use std::net::TcpListener;
|
2023-11-21 13:58:01 +00:00
|
|
|
|
2024-03-28 00:23:49 +00:00
|
|
|
use actix::{Actor, Arbiter};
|
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};
|
2024-04-30 06:44:33 +00:00
|
|
|
use emgauwa_common::db::DbController;
|
|
|
|
use emgauwa_common::errors::EmgauwaError;
|
|
|
|
use emgauwa_common::utils::{drop_privileges, init_logging};
|
2024-05-23 18:18:11 +00:00
|
|
|
use serde_json::Value;
|
|
|
|
use utoipa_swagger_ui::SwaggerUi;
|
2021-11-05 15:32:30 +00:00
|
|
|
|
2023-12-05 20:57:00 +00:00
|
|
|
use crate::app_state::AppState;
|
2023-11-29 13:27:46 +00:00
|
|
|
|
2023-12-01 17:27:04 +00:00
|
|
|
mod app_state;
|
2023-11-29 13:27:46 +00:00
|
|
|
mod handlers;
|
2023-11-19 17:54:27 +00:00
|
|
|
mod settings;
|
2021-11-04 22:37:16 +00:00
|
|
|
|
|
|
|
#[actix_web::main]
|
2023-12-05 00:42:19 +00:00
|
|
|
async fn main() -> Result<(), std::io::Error> {
|
|
|
|
let settings = settings::init()?;
|
|
|
|
|
2024-02-18 18:50:22 +00:00
|
|
|
let listener = TcpListener::bind(format!("{}:{}", settings.server.host, settings.server.port))?;
|
|
|
|
drop_privileges(&settings.permissions)?;
|
2023-12-05 00:42:19 +00:00
|
|
|
|
2024-02-18 18:50:22 +00:00
|
|
|
init_logging(&settings.logging.level)?;
|
2023-12-05 00:42:19 +00:00
|
|
|
|
2024-05-10 16:03:02 +00:00
|
|
|
let pool_size = 10;
|
|
|
|
let pool = emgauwa_common::db::init(&settings.database, pool_size).await?;
|
2023-12-05 00:42:19 +00:00
|
|
|
|
2024-05-02 17:33:25 +00:00
|
|
|
let mut conn = pool.acquire().await.map_err(EmgauwaError::from)?;
|
|
|
|
DbController::all_inactive(&mut conn)
|
2023-12-05 00:42:19 +00:00
|
|
|
.await
|
|
|
|
.map_err(EmgauwaError::from)?;
|
2024-05-02 17:33:25 +00:00
|
|
|
conn.close().await.map_err(EmgauwaError::from)?;
|
2023-11-27 23:19:15 +00:00
|
|
|
|
2024-03-28 00:23:49 +00:00
|
|
|
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 |_| {
|
2024-04-24 23:41:53 +00:00
|
|
|
AppState::new(app_state_pool)
|
2024-03-28 00:23:49 +00:00
|
|
|
});
|
2023-11-28 19:20:12 +00:00
|
|
|
|
2024-05-06 14:27:42 +00:00
|
|
|
let api_v1_json: Value =
|
|
|
|
serde_json::from_str(include_str!("../api.v1.json")).map_err(EmgauwaError::from)?;
|
|
|
|
|
2024-02-18 18:50:22 +00:00
|
|
|
log::info!(
|
|
|
|
"Starting server on {}:{}",
|
|
|
|
settings.server.host,
|
|
|
|
settings.server.port
|
|
|
|
);
|
2023-12-01 17:27:04 +00:00
|
|
|
|
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())
|
|
|
|
.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-12-07 03:30:33 +00:00
|
|
|
.app_data(web::Data::new(app_state.clone()))
|
2024-04-22 01:41:41 +00:00
|
|
|
.service(
|
|
|
|
SwaggerUi::new("/api/docs/{_:.*}")
|
2024-05-06 14:27:42 +00:00
|
|
|
.external_urls_from_iter_unchecked([("/api/v1.json", api_v1_json.clone())]),
|
2024-04-22 01:41:41 +00:00
|
|
|
)
|
2024-05-01 22:21:21 +00:00
|
|
|
.service(web::redirect("/api/docs", "/api/docs/"))
|
2024-04-22 01:41:41 +00:00
|
|
|
.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)
|
2024-04-26 14:15:24 +00:00
|
|
|
.service(handlers::v1::relays::pulse)
|
2024-04-22 01:41:41 +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)
|
|
|
|
.service(handlers::v1::tags::index)
|
|
|
|
.service(handlers::v1::tags::show)
|
|
|
|
.service(handlers::v1::tags::delete)
|
|
|
|
.service(handlers::v1::tags::add)
|
2024-04-28 23:11:52 +00:00
|
|
|
.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)
|
2024-04-25 17:45:22 +00:00
|
|
|
.service(handlers::v1::ws::ws_controllers)
|
|
|
|
.service(handlers::v1::ws::ws_relays),
|
2024-04-22 01:41:41 +00:00
|
|
|
)
|
2022-07-19 22:38:09 +00:00
|
|
|
})
|
2024-05-10 16:03:02 +00:00
|
|
|
.workers((pool_size / 2) as usize)
|
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
|
|
|
}
|