diff --git a/axum/with-state/Cargo.toml b/axum/with-state/Cargo.toml new file mode 100644 index 00000000..2baff2fc --- /dev/null +++ b/axum/with-state/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "with-state" +version = "0.1.0" +edition = "2021" + +[dependencies] +axum = "0.6.10" +shuttle-axum = { version = "0.18.0" } +shuttle-runtime = { version = "0.18.0" } +tokio = { version = "1.26.0" } diff --git a/axum/with-state/src/main.rs b/axum/with-state/src/main.rs new file mode 100644 index 00000000..58b284ff --- /dev/null +++ b/axum/with-state/src/main.rs @@ -0,0 +1,23 @@ +use std::sync::Arc; + +use axum::{extract::State, response::IntoResponse, routing::get, Router}; + +async fn hello_world(State(state): State>) -> impl IntoResponse { + state.msg +} + +#[derive(Clone)] +struct AppState { + msg: &'static str, +} + +#[shuttle_runtime::main] +async fn axum() -> shuttle_axum::ShuttleAxum { + let state = Arc::new(AppState { + msg: "Hello, world!", + }); + + let router = Router::new().route("/", get(hello_world)).with_state(state); + + Ok(router.into()) +}