Update README.md
This commit is contained in:
66
README.md
66
README.md
@@ -1,6 +1,6 @@
|
|||||||
# axum-app-wrapper
|
# 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:
|
Plugins can:
|
||||||
|
|
||||||
@@ -27,12 +27,14 @@ use std::{net::SocketAddr, sync::Arc};
|
|||||||
use axum::{
|
use axum::{
|
||||||
extract::State,
|
extract::State,
|
||||||
routing::get,
|
routing::get,
|
||||||
|
Extension,
|
||||||
};
|
};
|
||||||
use axum_app_wrapper::{AdHocPlugin, App, AppState};
|
use axum_app_wrapper::{AdHocPlugin, App, AppState};
|
||||||
|
|
||||||
#[derive(Clone, AppState)]
|
#[derive(Clone, AppState)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
config: Arc<Config>,
|
config: Arc<Config>,
|
||||||
|
metrics: Arc<Metrics>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -40,30 +42,64 @@ struct Config {
|
|||||||
service_name: String,
|
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<AppState>) -> String {
|
async fn health(State(state): State<AppState>) -> String {
|
||||||
format!("{}:ok", state.config.service_name)
|
format!("{}:ok", state.config.service_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn metrics_handler(Extension(metrics): Extension<Arc<Metrics>>) -> &'static str {
|
||||||
|
let _metrics = metrics;
|
||||||
|
"metrics:ok"
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
let config = Arc::new(Config {
|
let config = Arc::new(Config {
|
||||||
service_name: "api".to_owned(),
|
service_name: "api".to_owned(),
|
||||||
});
|
});
|
||||||
|
let metrics_registry = Arc::new(Metrics::new());
|
||||||
|
|
||||||
let app = App::<AppState>::new().register(
|
let config_plugin = AdHocPlugin::<AppState>::new()
|
||||||
AdHocPlugin::<AppState>::new()
|
.on_init(async move |mut state| {
|
||||||
.on_init(async move |mut state| {
|
state.insert(config);
|
||||||
state.insert(config);
|
Ok(state)
|
||||||
Ok(state)
|
})
|
||||||
})
|
.on_setup(|router, state| {
|
||||||
.on_setup(|router, _state| Ok(router.route("/health", get(health))))
|
tracing::info!(service = %state.config.service_name, "configuring routes");
|
||||||
.on_shutdown(|state| {
|
Ok(router.route("/health", get(health)))
|
||||||
let service_name = state.config.service_name.clone();
|
});
|
||||||
async move {
|
|
||||||
tracing::info!(%service_name, "shutting down");
|
let metrics_plugin = AdHocPlugin::<AppState>::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::<AppState>::new()
|
||||||
|
.register(config_plugin)
|
||||||
|
.register(metrics_plugin);
|
||||||
|
|
||||||
let (router, state, on_shutdown) = app.init().await?;
|
let (router, state, on_shutdown) = app.init().await?;
|
||||||
let router = router.with_state(state);
|
let router = router.with_state(state);
|
||||||
|
|||||||
Reference in New Issue
Block a user