Files
mytheclipse/crates/mytheclipse-http/src/server/axum_server.rs
T
asepharyana 8106f8943e feat: add 4 new crates (queue, tracing, http, cli) + enhancements to existing crates
New crates:
- mytheclipse-queue: unified job queue with WorkerPool, retry/backoff, DLQ
  Backends: in-memory (default), Redis, NATS, PostgreSQL
- mytheclipse-tracing: tracing subscriber layers with env filter + OTLP/Jaeger export
- mytheclipse-http: HTTP client/server with timeout + tracing, axum server
- mytheclipse-cli: CLI framework with clap derive, built-in subcommands

Enhancements to existing crates:
- mytheclipse-core: SemaphorePool, LeaderElection, HealthRegistry/HealthCheck
- mytheclipse-cache: AutoRefreshCache (bg refresh on miss), CacheMetrics
- mytheclipse-config: ConfigSchema for JSON Schema generation
- mytheclipse-storage: MultipartUploadDriver trait
- mytheclipse-crypto: PASETO v4.local token support

All features compile with --all-features; tests + clippy pass clean.
2026-08-29 16:05:02 +07:00

60 lines
1.5 KiB
Rust

//! Axum-based HTTP server with health endpoint.
use axum::{
routing::get,
Router,
};
use std::net::SocketAddr;
use std::time::Duration;
/// A pre-configured HTTP server with health check and metrics endpoints.
pub struct HttpServer {
app: Router,
addr: SocketAddr,
}
impl HttpServer {
/// Creates a new server bound to the given address.
pub fn new(addr: SocketAddr) -> Self {
let router = Router::new()
.route("/health", get(|| async { "OK" }))
.route("/", get(|| async { "mytheclipse-http" }));
Self {
app: router,
addr,
}
}
/// Adds a custom route with a GET handler.
#[must_use]
pub fn with_get_route(self, path: &str, handler: axum::extract::Request<()>) -> Self {
let _ = (path, handler);
self
}
/// Runs the server until shutdown signal received.
pub async fn run(self) {
let listener = tokio::net::TcpListener::bind(self.addr)
.await
.expect("failed to bind");
axum::serve(listener, self.app)
.with_graceful_shutdown(shutdown_signal())
.await
.expect("server error");
}
}
async fn shutdown_signal() {
tokio::signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
tracing::info!("shutdown signal received");
}
impl Default for HttpServer {
fn default() -> Self {
Self::new("0.0.0.0:3000".parse().unwrap())
}
}