From 513cbc19a3c892b4301345b8ebb345ddb5b0650a Mon Sep 17 00:00:00 2001 From: fa-sharp Date: Tue, 26 May 2026 02:44:08 -0400 Subject: [PATCH] Update README.md --- README.md | 66 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b94c46a..2b476f0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # axum-app-wrapper -A small plugin layer for `axum` applications, inspired by [fastify](https://fastify.dev/) from the Node/JS ecossytem. +A small plugin layer for `axum` applications, inspired by [fastify](https://fastify.dev/) from the Node/JS ecosystem. Plugins can: @@ -27,12 +27,14 @@ use std::{net::SocketAddr, sync::Arc}; use axum::{ extract::State, routing::get, + Extension, }; use axum_app_wrapper::{AdHocPlugin, App, AppState}; #[derive(Clone, AppState)] struct AppState { config: Arc, + metrics: Arc, } #[derive(Clone)] @@ -40,30 +42,64 @@ struct Config { service_name: String, } +struct Metrics; + +impl Metrics { + fn new() -> Self { + Self + } + + async fn flush(&self) { + tracing::info!("flushed metrics"); + } +} + async fn health(State(state): State) -> String { format!("{}:ok", state.config.service_name) } +async fn metrics_handler(Extension(metrics): Extension>) -> &'static str { + let _metrics = metrics; + "metrics:ok" +} + #[tokio::main] async fn main() -> anyhow::Result<()> { let config = Arc::new(Config { service_name: "api".to_owned(), }); + let metrics_registry = Arc::new(Metrics::new()); - let app = App::::new().register( - AdHocPlugin::::new() - .on_init(async move |mut state| { - state.insert(config); - Ok(state) - }) - .on_setup(|router, _state| Ok(router.route("/health", get(health)))) - .on_shutdown(|state| { - let service_name = state.config.service_name.clone(); - async move { - tracing::info!(%service_name, "shutting down"); - } - }), - ); + let config_plugin = AdHocPlugin::::new() + .on_init(async move |mut state| { + state.insert(config); + Ok(state) + }) + .on_setup(|router, state| { + tracing::info!(service = %state.config.service_name, "configuring routes"); + Ok(router.route("/health", get(health))) + }); + + let metrics_plugin = AdHocPlugin::::new() + .on_init(async move |mut state| { + state.insert(metrics_registry); + Ok(state) + }) + .on_setup(|router, state| { + Ok(router + .route("/metrics", get(metrics_handler)) + .layer(Extension(Arc::clone(&state.metrics)))) + }) + .on_shutdown(|state| { + let metrics = Arc::clone(&state.metrics); + async move { + metrics.flush().await; + } + }); + + let app = App::::new() + .register(config_plugin) + .register(metrics_plugin); let (router, state, on_shutdown) = app.init().await?; let router = router.with_state(state);