1
0
Fork 0
mirror of https://codeberg.org/Mo8it/git-webhook-client synced 2024-11-24 11:21:36 +00:00

Migrate main.rs

This commit is contained in:
Mo 2022-11-12 13:04:18 +01:00
parent 1bb2313fd5
commit de576cab37

View file

@ -8,32 +8,70 @@ mod schema;
mod states; mod states;
use anyhow::Result; use anyhow::Result;
use log::info; use axum::extract::Extension;
use rocket::{Build, Rocket}; use axum::routing::{get, post};
use rocket_dyn_templates::Template; use axum::{error_handling::HandleErrorLayer, http::StatusCode, BoxError};
use axum::{Router, Server};
use axum_extra::routing::SpaRouter;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::process; use std::process;
use std::sync::Arc;
use tower_http::trace::{DefaultOnResponse, TraceLayer};
use tracing::Level;
use tracing_appender::non_blocking::WorkerGuard;
fn init() -> Result<Rocket<Build>> { fn init() -> Result<Rocket<Build>> {
let config = config::Config::new()?;
logging::init_logger(&config)?;
info!("Starting client");
let rocket = rocket::build() let rocket = rocket::build()
.mount("/", rocket::routes![routes::index]) .mount("/", rocket::routes![routes::index])
.mount("/api", rocket::routes![routes::trigger]) .mount("/api", rocket::routes![routes::trigger])
.manage(states::DB::new()?) .manage(states::DB::new()?)
.manage(states::Config::new(config))
.attach(Template::fairing());
Ok(rocket)
} }
#[rocket::launch] async fn init() -> Result<WorkerGuard> {
fn rocket() -> _ { let mut config = config::Config::new()?;
init().unwrap_or_else(|e| { let path_prefix = config.path_prefix.clone();
let mailer = Arc::new(mailer::Mailer::new(&mut config)?);
let address = config.socket_address.address;
let socket_address = SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(
address[0], address[1], address[2], address[3],
)),
config.socket_address.port,
);
let tracing_worker_gurad = logging::init_logger(&config.logging);
let config = Arc::new(config);
let spa = SpaRouter::new(&format!("{}/static", &path_prefix), "static");
let api_routes = Router::new()
.route("/submit", post(routes::trigger));
let routes = Router::new()
.route("/", get(routes::index))
.nest("/api", api_routes);
let app = Router::new()
.merge(spa)
.merge(routes)
.layer(TraceLayer::new_for_http().on_response(DefaultOnResponse::new().level(Level::INFO)))
.layer(Extension(config))
.layer(Extension(mailer));
Server::bind(&socket_address)
.serve(app.into_make_service())
.await
.unwrap();
Ok(tracing_worker_gurad)
}
#[tokio::main]
async fn main() {
let _tracing_worker_gurad = init().await.unwrap_or_else(|e| {
eprintln!("{e:?}"); eprintln!("{e:?}");
process::exit(1); process::exit(1);
}) });
} }