#[macro_use]
extern crate actix_web;
use chimes_auth::ChimesAuthorization;
use chimes_utils::{get_rbatis, AppConfig, WebServerConfig};
use actix_cors::Cors;
use actix_web::http::{header, StatusCode};
use actix_web::{middleware, App, HttpRequest, HttpResponse, HttpServer, Result};
mod entity;
mod handler;
mod query;
mod utils;
use crate::utils::{AppEntryCollect, ChimesUserAuthService};
#[get("/")]
async fn index_handler(_req: HttpRequest) -> Result<HttpResponse> {
Ok(HttpResponse::build(StatusCode::OK)
.content_type("text/html; charset=utf-8")
.body("App is running."))
}
#[tokio::main(flavor = "multi_thread", worker_threads = 10)]
async fn main() -> std::io::Result<()> {
let conf_path = std::env::current_dir()
.unwrap()
.as_os_str()
.to_str()
.unwrap()
.to_owned()
+ "/conf/app.yml";
log::info!("Current Path: {}", conf_path);
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
match fast_log::init(fast_log::config::Config::new().console()) {
Ok(_) => {}
Err(err) => {
log::warn!("An error occurred on the Logger initializing. {}", err);
}
};
let mut appconf = AppConfig::get().lock().unwrap();
appconf.load_yaml(&conf_path.clone());
let conf = appconf.clone();
drop(appconf);
let _rb = get_rbatis();
start_web_server(&conf.webserver_conf).await
}
async fn start_web_server(webconf: &WebServerConfig) -> std::io::Result<()> {
let temp_path = webconf.upload_temp_path.clone();
let ip = format!("{}:{}", "0.0.0.0", webconf.port.clone());
log::info!("App is listening on {}.", ip.clone());
let cuas = ChimesUserAuthService { system_user: None };
HttpServer::new(move || {
App::new()
.wrap(middleware::Logger::default())
.wrap(
ChimesAuthorization::new(cuas.clone())
.header_key(&"Authorization".to_string())
.allow(&"/api/v1/auth/login".to_string())
.allow(&"/api/v1/auth/code".to_string())
.allow(&"/api/v1/auth/logout".to_string())
.allow(&"/api/v1/healthcheck".to_string()),
)
.wrap(
Cors::default()
.allowed_origin("http://localhost:8013")
.allowed_methods(vec!["GET", "POST", "PUT", "DELETE"])
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.allowed_header(header::CONTENT_TYPE)
.supports_credentials()
.max_age(3600),
)
.app_data(
awmp::PartsConfig::default()
.with_file_limit(1000000000)
.with_temp_dir(temp_path.as_str()),
)
.service(index_handler)
.register_handlers()
})
.bind(ip)?
.run()
.await
}