mirror of
https://codeberg.org/Mo8it/git-webhook-client
synced 2024-11-21 11:06:32 +00:00
Migrate main.rs
This commit is contained in:
parent
1bb2313fd5
commit
de576cab37
1 changed files with 55 additions and 17 deletions
72
src/main.rs
72
src/main.rs
|
@ -8,32 +8,70 @@ mod schema;
|
|||
mod states;
|
||||
|
||||
use anyhow::Result;
|
||||
use log::info;
|
||||
use rocket::{Build, Rocket};
|
||||
use rocket_dyn_templates::Template;
|
||||
use axum::extract::Extension;
|
||||
use axum::routing::{get, post};
|
||||
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::sync::Arc;
|
||||
use tower_http::trace::{DefaultOnResponse, TraceLayer};
|
||||
use tracing::Level;
|
||||
use tracing_appender::non_blocking::WorkerGuard;
|
||||
|
||||
fn init() -> Result<Rocket<Build>> {
|
||||
let config = config::Config::new()?;
|
||||
|
||||
logging::init_logger(&config)?;
|
||||
|
||||
info!("Starting client");
|
||||
|
||||
let rocket = rocket::build()
|
||||
.mount("/", rocket::routes![routes::index])
|
||||
.mount("/api", rocket::routes![routes::trigger])
|
||||
.manage(states::DB::new()?)
|
||||
.manage(states::Config::new(config))
|
||||
.attach(Template::fairing());
|
||||
|
||||
Ok(rocket)
|
||||
}
|
||||
|
||||
#[rocket::launch]
|
||||
fn rocket() -> _ {
|
||||
init().unwrap_or_else(|e| {
|
||||
async fn init() -> Result<WorkerGuard> {
|
||||
let mut config = config::Config::new()?;
|
||||
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:?}");
|
||||
process::exit(1);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue