Replies: 4 comments 4 replies
-
please see the example https://github.com/tokio-rs/axum/blob/dea36db400f27c025b646e5720b9a6784ea4db6e/examples/key-value-store/src/main.rs this should help |
Beta Was this translation helpful? Give feedback.
-
there's also a real world example in my see https://github.com/ttys3/static-server/blob/main/src/main.rs#L162 struct StaticServerConfig {
pub(crate) root_dir: String,
}
let app = Router::new()
.route("/favicon.ico", get(favicon))
.route("/healthz", get(health_check))
.fallback(get(index_or_content))
.layer(AddExtensionLayer::new(Arc::new(StaticServerConfig { root_dir }))); and in the handler: async fn index_or_content(Extension(cfg): Extension<Arc<StaticServerConfig>>, req: Request<Body>) -> impl IntoResponse {
// ...
} |
Beta Was this translation helpful? Give feedback.
-
To mutate shared state you’ll need an Arc<Mutex>, or equivalent. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to pass mutable shared state to my route handler function using an My code looks like this:
This is basically the same as in the example linked by @ttys3, but I'm getting the following compiler error:
Does anyone have any suggestions? |
Beta Was this translation helpful? Give feedback.
-
is there some way to mutate share state in route handlers, i had a golang project use package variables to store some share state and mutated it in route handlers, when i try to rewrite this golang project with axum, i have no idea about how to mutate share state, any suggestions?
Beta Was this translation helpful? Give feedback.
All reactions