89 lines
2.3 KiB
Rust
Raw Normal View History

2024-07-14 16:38:07 -04:00
#![forbid(unsafe_code)]
2024-07-14 19:01:10 -04:00
use std::time::Duration;
2024-07-14 15:19:41 -04:00
use axum::http::StatusCode;
2024-07-15 21:02:30 -04:00
use axum::middleware;
2024-07-14 15:14:52 -04:00
use axum::routing::get;
2024-07-14 16:13:06 -04:00
use axum::routing::post;
2024-07-14 15:19:41 -04:00
use axum::Json;
2024-07-14 15:14:52 -04:00
use axum::Router;
2024-07-14 23:08:10 -04:00
use kube::Client;
2024-07-14 15:19:41 -04:00
use serde::Serialize;
2024-07-14 19:01:10 -04:00
use tokio::signal;
use tower_http::timeout::TimeoutLayer;
2024-07-14 15:50:13 -04:00
use tower_http::trace::TraceLayer;
2024-07-14 15:34:14 -04:00
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
2024-07-14 15:14:52 -04:00
2024-07-14 16:13:06 -04:00
use self::webhook::hook;
2024-07-15 21:02:30 -04:00
use self::webhook::verify_signature;
2024-07-14 16:13:06 -04:00
2024-07-14 18:33:24 -04:00
mod hook_push;
2024-07-14 16:13:06 -04:00
mod webhook;
2024-07-14 15:14:52 -04:00
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
2024-07-14 15:34:14 -04:00
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
"webhook_bridge=info,tower_http=debug,axum::rejection=trace".into()
}),
)
.with(tracing_subscriber::fmt::layer())
.init();
2024-07-14 23:08:10 -04:00
let kubernetes_client: Client = Client::try_default()
.await
.expect("Set KUBECONFIG to a valid kubernetes config.");
2024-07-14 15:50:13 -04:00
let app = Router::new()
2024-07-14 16:13:06 -04:00
.route("/hook", post(hook))
2024-07-15 21:02:30 -04:00
.layer(middleware::from_fn(verify_signature))
.route("/health", get(health))
2024-07-14 19:01:10 -04:00
.layer((
TraceLayer::new_for_http(),
// Add a timeout layer so graceful shutdown can't wait forever.
TimeoutLayer::new(Duration::from_secs(600)),
));
2024-07-14 15:14:52 -04:00
2024-07-15 21:02:30 -04:00
let listener = tokio::net::TcpListener::bind("0.0.0.0:9988").await?;
2024-07-14 15:34:14 -04:00
tracing::info!("listening on {}", listener.local_addr().unwrap());
2024-07-14 19:01:10 -04:00
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await?;
2024-07-14 15:14:52 -04:00
Ok(())
}
2024-07-14 19:01:10 -04:00
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
}
2024-07-14 15:19:41 -04:00
async fn health() -> (StatusCode, Json<HealthResponse>) {
(StatusCode::OK, Json(HealthResponse { ok: true }))
}
#[derive(Serialize)]
struct HealthResponse {
ok: bool,
2024-07-14 14:52:45 -04:00
}