#[config]Expand description
Attribute macro that makes a struct usable as State<T> in handlers by
generating a FromRef<GotchaContext<T, C>> impl. See GotchaContext.
Marks a struct as a Gotcha application state so it can be extracted directly
with axum’s State<T> in handlers.
It generates impl<C: GotchaConfig> FromRef<GotchaContext<T, C>> for T, which
pulls the state out of the GotchaContext that the framework injects as the
axum state. Without this, handlers would have to extract the whole
State<GotchaContext<T, C>> and reach into .state.
The struct must be Clone and non-generic. (For a generic state, write the
FromRef impl by hand.)
use gotcha::prelude::*;
#[state]
#[derive(Clone, Default)]
struct AppState {
started_at: std::time::SystemTime,
}
async fn handler(State(state): State<AppState>) -> impl Responder {
format!("{:?}", state.started_at)
}Marks a struct as the application configuration so it can be extracted directly with
axum’s State<T> in handlers.
It generates impl FromRef<GotchaContext<S, Self>> for Self, pulling the application’s own
settings out of the loaded configuration. Without this, handlers would have to extract
State<ConfigWrapper<Config>> and reach through the wrapper.
The struct must be Clone and non-generic, and must satisfy the config bounds
(Serialize + Deserialize + Default).
use gotcha::prelude::*;
use serde::{Deserialize, Serialize};
#[config]
#[derive(Clone, Default, Serialize, Deserialize)]
struct Config {
name: String,
}
async fn handler(State(config): State<Config>) -> impl Responder {
config.name.clone()
}