This example shows how you can make a custom resource.

The example resource we’ll be making will be a Plain Data Object (which we will refer to as pdo), and outputs the value that you pass into the “name” attribute for the resource.

We are using the Axum framework in main.rs so we can showcase the resource in action, but the implementation is entirely separate from what web framework you use so you can add your custom resource to any Shuttle-hosted resource.

You can clone the example below by running the following (you’ll need cargo-shuttle installed):

cargo shuttle init --from https://github.com/shuttle-hq/shuttle-examples \
  --subfolder custom-resource/pdo
use axum::{extract::State, routing::get, Router};
use pdo::{Builder, Pdo};
use std::sync::Arc;

async fn hello_world(State(pdo): State<Arc<Pdo>>) -> String {
    pdo.name.clone()
}

#[shuttle_runtime::main]
async fn axum(#[Builder(name = "John")] pdo: Pdo) -> shuttle_axum::ShuttleAxum {
    let state = Arc::new(pdo);
    let router = Router::new().route("/", get(hello_world)).with_state(state);

    Ok(router.into())
}