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

89 lines
2.7 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},
web::{self},
3 years ago
App, HttpServer,
};
3 years ago
use oab::{api, init_log, libs, models, AppState, Clis, Result, CLI};
3 years ago
use tracing::{error, info, warn};
3 years ago
#[tokio::main]
async fn main() -> Result<()> {
std::env::set_var("RUST_LOG", "debug");
std::env::set_var("RUST_BACKTRACE", "1");
init_log();
let mut data = AppState::new();
data.connect().await?;
data.connect_sqlx()?;
if let Some(c) = &CLI.command {
match c {
Clis::Init => {
models::init(data).await;
return Ok(());
}
_ => {}
};
};
web(data).await?;
Ok(())
}
async fn web(data: AppState) -> Result<()> {
let url = data.server_url.clone();
2 years ago
let dav = libs::fs::core();
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())
2 years ago
.app_data(web::Data::new(data.clone()))
.service(fs::Files::new("/media", data.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)
.configure(api::routes),
)
2 years ago
.service(
web::scope("file")
.app_data(web::Data::new(dav.clone()))
.service(web::resource("/{tail:.*}").to(libs::fs::dav_handler)),
)
});
info!("listen to {}", url);
serv.bind(url)?.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()))
}