62 lines
1.8 KiB
Rust
62 lines
1.8 KiB
Rust
use std::str::FromStr;
|
|
use actix_cors::Cors;
|
|
|
|
use actix_web::middleware::TrailingSlash;
|
|
use actix_web::{middleware, web, App, HttpServer};
|
|
use emgauwa_lib::handlers;
|
|
use log::{trace, LevelFilter};
|
|
use simple_logger::SimpleLogger;
|
|
|
|
mod settings;
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
let settings = settings::init();
|
|
|
|
let log_level: LevelFilter = LevelFilter::from_str(&settings.logging.level)
|
|
.expect("Error parsing log level.");
|
|
trace!("Log level set to {:?}", log_level);
|
|
|
|
SimpleLogger::new()
|
|
.with_level(log_level)
|
|
.init()
|
|
.expect("Error initializing logger.");
|
|
|
|
let pool = emgauwa_lib::db::init(&settings.database).await;
|
|
|
|
log::info!("Starting server on {}:{}", settings.host, settings.port);
|
|
HttpServer::new(move || {
|
|
|
|
let cors = Cors::default()
|
|
.allow_any_method()
|
|
.allow_any_header()
|
|
.max_age(3600);
|
|
|
|
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())
|
|
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
|
|
.app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler))
|
|
.app_data(web::Data::new(pool.clone()))
|
|
.service(handlers::v1::relays::index)
|
|
.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::ws::controllers::index)
|
|
})
|
|
.bind(format!("{}:{}", settings.host, settings.port))?
|
|
.run()
|
|
.await
|
|
}
|