controller/src/main.rs

58 lines
1.6 KiB
Rust

#[macro_use]
extern crate diesel;
extern crate diesel_migrations;
extern crate dotenv;
use actix_web::middleware::TrailingSlash;
use actix_web::{middleware, web, App, HttpServer};
use log::{trace, LevelFilter};
use simple_logger::SimpleLogger;
use std::fmt::format;
use std::str::FromStr;
mod db;
mod handlers;
mod return_models;
mod settings;
mod types;
mod utils;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
settings::init();
let settings = settings::get();
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);
SimpleLogger::new()
.with_level(log_level)
.init()
.unwrap_or_else(|_| panic!("Error initializing logger."));
db::init(&settings.database);
HttpServer::new(|| {
App::new()
.wrap(
middleware::DefaultHeaders::new()
.add(("Access-Control-Allow-Origin", "*"))
.add(("Access-Control-Allow-Headers", "*"))
.add(("Access-Control-Allow-Methods", "*")),
)
.wrap(middleware::Logger::default())
.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
.app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler))
.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)
})
.bind(format!("{}:{}", settings.host, settings.port))?
.run()
.await
}