You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/oab/src/main.rs

79 lines
2.3 KiB
Rust

//
// main.rs
// Copyright (C) 2022 veypi <i@veypi.com>
// 2022-07-07 23:51
// Distributed under terms of the Apache license.
//
3 years ago
use actix_files as fs;
3 years ago
use actix_web::{
3 years ago
dev,
http::StatusCode,
middleware::{self, ErrorHandlerResponse, ErrorHandlers},
3 years ago
web::{self, Data},
App, HttpServer,
};
3 years ago
3 years ago
use oab::{api, init_log, libs, models, Clis, Result, CLI, CONFIG};
use tracing::{error, info, warn};
3 years ago
#[tokio::main]
async fn main() -> Result<()> {
init_log();
if let Some(c) = &CLI.command {
match c {
Clis::Init => {
models::init().await;
return Ok(());
}
_ => {}
};
};
web().await?;
Ok(())
}
async fn web() -> Result<()> {
std::env::set_var("RUST_LOG", "info");
std::env::set_var("RUST_BACKTRACE", "1");
let serv = HttpServer::new(move || {
let logger = middleware::Logger::default();
3 years ago
let json_config = web::JsonConfig::default()
.limit(4096)
3 years ago
.error_handler(|err, _req| {
3 years ago
// create custom error response
// oab::Error::InternalServerError
warn!("{:#?}", err);
actix_web::error::InternalError::from_response(
err,
actix_web::HttpResponse::Conflict().finish(),
)
.into()
});
let app = App::new();
app.wrap(logger)
.wrap(middleware::Compress::default())
3 years ago
.service(fs::Files::new("/media", CONFIG.media_path.clone()).show_files_listing())
3 years ago
.service(
web::scope("api")
3 years ago
.wrap(
ErrorHandlers::new()
.handler(StatusCode::INTERNAL_SERVER_ERROR, add_error_header),
)
.wrap(libs::auth::Auth)
3 years ago
.app_data(json_config)
3 years ago
.app_data(Data::new(CONFIG.db()))
3 years ago
.configure(api::routes),
)
});
info!("listen to {}", CONFIG.server_url);
serv.bind(CONFIG.server_url.clone())?.run().await?;
Ok(())
3 years ago
}
3 years ago
fn add_error_header<B>(
res: dev::ServiceResponse<B>,
) -> std::result::Result<ErrorHandlerResponse<B>, actix_web::Error> {
error!("{}", res.response().error().unwrap());
Ok(ErrorHandlerResponse::Response(res.map_into_left_body()))
}