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 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 \
 --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 {
// this should output "John"

async fn axum(#[Builder(name = "John")] pdo: Pdo) -> shuttle_axum::ShuttleAxum {
// add the pdo object to app-wide state variables
    let state = Arc::new(pdo);
    let router = Router::new().route("/", get(hello_world)).with_state(state);