2023-11-21 13:58:01 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
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-22 19:06:20 +00:00
|
|
|
use emgauwa_lib::handlers;
|
2023-11-19 17:54:27 +00:00
|
|
|
use log::{trace, LevelFilter};
|
|
|
|
use simple_logger::SimpleLogger;
|
2021-11-05 15:32:30 +00:00
|
|
|
|
2023-11-19 17:54:27 +00:00
|
|
|
mod settings;
|
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();
|
2021-11-04 22:37:16 +00:00
|
|
|
|
2023-11-19 17:54:27 +00:00
|
|
|
let log_level: LevelFilter = log::LevelFilter::from_str(&settings.logging.level)
|
|
|
|
.unwrap_or_else(|_| panic!("Error parsing log level."));
|
|
|
|
trace!("Log level set to {:?}", log_level);
|
2021-11-06 13:54:45 +00:00
|
|
|
|
2023-11-19 17:54:27 +00:00
|
|
|
SimpleLogger::new()
|
|
|
|
.with_level(log_level)
|
|
|
|
.init()
|
|
|
|
.unwrap_or_else(|_| panic!("Error initializing logger."));
|
2022-04-15 00:11:34 +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-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 || {
|
2022-07-19 22:38:09 +00:00
|
|
|
App::new()
|
|
|
|
.wrap(
|
|
|
|
middleware::DefaultHeaders::new()
|
2023-11-19 17:54:27 +00:00
|
|
|
.add(("Access-Control-Allow-Origin", "*"))
|
|
|
|
.add(("Access-Control-Allow-Headers", "*"))
|
|
|
|
.add(("Access-Control-Allow-Methods", "*")),
|
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()))
|
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-22 15:28:46 +00:00
|
|
|
.service(handlers::v1::ws::controllers::index)
|
2022-07-19 22:38:09 +00:00
|
|
|
})
|
2023-11-19 17:54:27 +00:00
|
|
|
.bind(format!("{}:{}", settings.host, settings.port))?
|
2022-07-19 22:38:09 +00:00
|
|
|
.run()
|
|
|
|
.await
|
2021-11-04 22:37:16 +00:00
|
|
|
}
|