logo
macro_rules! declare_service {
    ($service_type:ty, $constructor:path) => { ... };
    ($service_type:ty, $constructor:path, $state_builder:path) => { ... };
}
Expand description

Helper macro that generates the entrypoint required of any service.

Can be used in one of two ways:

Without a state

If your service does not require a state (like a database connection pool), just pass a type and a constructor function:

#[macro_use]
extern crate shuttle_service;

use rocket::{Rocket, Build};

fn rocket() -> Rocket<Build> {
    rocket::build()
}

declare_service!(Rocket<Build>, rocket);

The constructor function must return an instance of the type passed as first argument. Furthermore, the type must implement IntoService.

With a state

If your service requires a state, pass a type, a constructor and a state builder:

use rocket::{Rocket, Build};
use sqlx::PgPool;

#[macro_use]
extern crate shuttle_service;
use shuttle_service::{Factory, Error};

struct MyState(PgPool);

async fn state(factory: &mut dyn Factory) -> Result<MyState, shuttle_service::Error> {
   let pool = sqlx::postgres::PgPoolOptions::new()
       .connect(&factory.get_sql_connection_string().await?)
       .await?;
   Ok(MyState(pool))
}

fn rocket() -> Rocket<Build> {
    rocket::build()
}

declare_service!(Rocket<Build>, rocket, state);

The state builder will be called when the deployer calls Service::build.