logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/getsynth/shuttle/main/resources/logo-square-transparent.png",
    html_favicon_url = "https://raw.githubusercontent.com/getsynth/shuttle/main/resources/favicon.ico"
)]
//! # Shuttle - Deploy Rust apps with a single Cargo subcommand
//! <div style="display: flex; margin-top: 30px; margin-bottom: 30px;">
//! <img src="https://raw.githubusercontent.com/getsynth/shuttle/main/resources/logo-rectangle-transparent.png" width="400px" style="margin-left: auto; margin-right: auto;"/>
//! </div>
//!
//! Hello, and welcome to the <span style="font-family: Sans-Serif;"><a href="https://shuttle.rs">shuttle</a></span> API documentation!
//!
//! Shuttle is an open-source app platform that uses traits and annotations to configure your backend deployments.
//!
//! ## Usage
//!
//! Depend on `shuttle-service` in `Cargo.toml`:
//!
//! ```toml
//! shuttle-service = "0.2"
//! ```
//!
//! and make sure your crate has a `cdylib` output target:
//!
//! ```toml
//! [lib]
//! crate-type = ["cdylib"]
//! ```
//!
//! See the [shuttle_service::main][main] macro for more information on how to implement a service. Here's a simple example using [rocket][rocket] to get you started:
//!
//! ```rust,no_run
//! #[macro_use]
//! extern crate rocket;
//!
//! use rocket::{Build, Rocket};
//!
//! #[get("/hello")]
//! fn hello() -> &'static str {
//!     "Hello, world!"
//! }
//!
//! #[shuttle_service::main]
//! async fn init() -> Result<Rocket<Build>, shuttle_service::Error> {
//!     let rocket = rocket::build().mount("/", routes![hello]);
//!
//!     Ok(rocket)
//! }
//! ```
//!
//! Complete examples can be found [in the repository](https://github.com/getsynth/shuttle/tree/main/examples/rocket).
//!
//! ## Deploying
//!
//! You can deploy your service with the [`cargo shuttle`](https://docs.rs/crate/cargo-shuttle/latest) subcommand. To install run:
//!
//! ```bash
//! $ cargo install cargo-shuttle
//! ```
//!
//! in a terminal. Once installed, run:
//!
//! ```bash
//! $ cargo shuttle login
//! ```
//!
//! this will open a browser window and prompt you to connect using your GitHub account.
//!
//! Then, deploy the service with:
//!
//! ```bash
//! $ cargo shuttle deploy
//! ```
//!
//! Your service will immediately be available at `{crate_name}.shuttleapp.rs`. For example:
//!
//! ```bash
//! $ curl https://hello-world-rocket-app.shuttleapp.rs
//! Hello, world!
//! ```
//!
//! ## Using `sqlx`
//!
//! Here is a quick example to deploy a service which uses a postgres database and [sqlx](http://docs.rs/sqlx):
//!
//! ```rust,no_run
//! #[macro_use]
//! extern crate rocket;
//!
//! use rocket::{Build, Rocket};
//! use sqlx::PgPool;
//!
//! struct MyState(PgPool);
//!
//! #[get("/hello")]
//! fn hello(state: &State<MyState>) -> &'static str {
//!     // Do things with `state.0`...
//!     "Hello, Postgres!"
//! }
//!
//! #[shuttle_service::main]
//! async fn rocket(pool: PgPool) -> Result<Rocket<Build>, shuttle_service::Error> {
//!     let state = MyState(pool);
//!     let rocket = rocket::build().manage(state).mount("/", routes![hello]);
//!
//!     Ok(rocket)
//! }
//! ```
//!
//! To learn more about shuttle managed services, see [shuttle_service::main][main#getting-shuttle-managed-services].
//!
//! ## Configuration
//!
//! The `cargo shuttle` command can be customised by creating a `Shuttle.toml` in the same location as your `Cargo.toml`.
//!
//! ## Getting API keys
//!
//! After you've installed the [cargo-shuttle](https://docs.rs/crate/cargo-shuttle/latest) command, run:
//!
//! ```bash
//! $ cargo shuttle login
//! ```
//!
//! this will open a browser window and prompt you to connect using your GitHub account.
//!
//! ##### Change the name of your service
//!
//! To have your service deployed with a different name, add a `name` entry in the `Shuttle.toml`:
//!
//! ```toml
//! name = "hello-world"
//! ```
//!
//! If the `name` key is not specified, the service's name will be the same as the crate's name.
//!
//! ## We're in alpha 🤗
//!
//! Thanks for using shuttle! We're very happy to have you with us!
//!
//! During our alpha period, API keys are completely free and you can deploy as many services as you want.
//!
//! Just keep in mind that there may be some kinks that require us to take all deployments down once in a while. In certain circumstances we may also have to delete all the data associated with those deployments.
//!
//! To stay updated with the release status of shuttle, [join our Discord](https://discord.gg/H33rRDTm3p)!
//!
//! ## Join Discord
//!
//! If you have any questions, [join our Discord server](https://discord.gg/H33rRDTm3p). There's always someone on there that can help!
//!
//! You can also [open an issue or a discussion on GitHub](https://github.com/getsynth/shuttle).
//!

use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;

use async_trait::async_trait;
use tokio::runtime::Runtime;

pub mod error;
pub use error::Error;

#[cfg(feature = "codegen")]
extern crate shuttle_codegen;
#[cfg(feature = "codegen")]
/// Helper macro that generates the entrypoint required by any service - likely the only macro you need in this crate.
///
/// # Without shuttle managed services
/// The simplest usage is when your service does not require any shuttle managed resources, so you only need to return a shuttle supported service:
///
/// ```rust,no_run
/// use rocket::{Build, Rocket};
///
/// #[shuttle_service::main]
/// async fn rocket() -> Result<Rocket<Build>, shuttle_service::Error> {
///     let rocket = rocket::build();
///
///     Ok(rocket)
/// }
/// ```
///
/// ## shuttle supported services
/// The following type can take the place of the `Ok` type and enjoy first class service support in shuttle.
///
/// | Ok type           | Service  |
/// | ----------------- | -------- |
/// | [`Rocket<Build>`] | [rocket] |
///
/// # Getting shuttle managed services
/// The shuttle is able to manage service dependencies for you. These services are passed in as inputs to your main function:
/// ```rust,no_run
/// use rocket::{Build, Rocket};
/// use sqlx::PgPool;
///
/// struct MyState(PgPool);
///
/// #[shuttle_service::main]
/// async fn rocket(pool: PgPool) -> Result<Rocket<Build>, shuttle_service::Error> {
///     let state = MyState(pool);
///     let rocket = rocket::build().manage(state);
///
///     Ok(rocket)
/// }
/// ```
///
/// ## shuttle managed dependencies
/// The following dependencies can be managed by shuttle:
///
/// | Argument type      | Dependency                                                         |
/// | ------------------ | ------------------------------------------------------------------ |
/// | [`PgPool`](https://docs.rs/sqlx/latest/sqlx/type.PgPool.html) | A PostgresSql instance accessed using [sqlx](https://docs.rs/sqlx) |
pub use shuttle_codegen::main;

#[cfg(feature = "loader")]
pub mod loader;

/// Factories can be used to request the provisioning of additional resources (like databases).
///
/// An instance of factory is passed by the deployer as an argument to [Service::build][Service::build] in the initial phase of deployment.
///
/// Also see the [declare_service!][declare_service] macro.
#[async_trait]
pub trait Factory: Send + Sync {
    /// Declare that the [Service][Service] requires a postgres database.
    ///
    /// Returns the connection string to the provisioned database.
    async fn get_sql_connection_string(&mut self) -> Result<String, crate::Error>;
}

/// Used to get resources of type `T` from factories.
///
/// This is mainly meant for consumption by our code generator and should generally not be implemented by users.
#[async_trait]
pub trait GetResource<T> {
    async fn get_resource(self) -> Result<T, crate::Error>;
}

/// Get an `sqlx::PgPool` from any factory
#[cfg(feature = "sqlx-postgres")]
#[async_trait]
impl GetResource<sqlx::PgPool> for &mut dyn Factory {
    async fn get_resource(self) -> Result<sqlx::PgPool, crate::Error> {
        use error::CustomError;

        let connection_string = self.get_sql_connection_string().await?;

        let pool = sqlx::postgres::PgPoolOptions::new()
            .min_connections(1)
            .max_connections(5)
            .connect(&connection_string)
            .await
            .map_err(CustomError::new)?;

        Ok(pool)
    }
}

/// The core trait of the shuttle platform. Every crate deployed to shuttle needs to implement this trait.
///
/// Use the [declare_service!][crate::declare_service] macro to expose your implementation to the deployment backend.
pub trait Service: Send + Sync {
    /// This function is run exactly once on each instance of a deployment, prior to calling [bind][Service::bind].
    ///
    /// The passed [Factory][Factory] can be used to configure additional resources (like databases).
    ///
    /// The default is a noop that returns `Ok(())`.
    fn build(&mut self, _: &mut dyn Factory) -> Result<(), Error> {
        Ok(())
    }

    /// This function is run exactly once on each instance of a deployment.
    ///
    /// The deployer expects this instance of [Service][Service] to bind to the passed [SocketAddr][SocketAddr].
    fn bind(&mut self, addr: SocketAddr) -> Result<(), error::Error>;
}

/// A convenience trait for handling out of the box conversions into [Service][Service] instances.
pub trait IntoService {
    /// The [Service][Service] instance this converts to.
    type Service: Service;

    /// Convert into a [Service][Service] instance.
    fn into_service(self) -> Self::Service;
}

pub type StateBuilder<T> =
    fn(&mut dyn Factory) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + '_>>;

#[cfg(feature = "web-rocket")]
/// A convenience struct for building a [Service][Service] from a [Rocket<Build>][Rocket] instance.
///
/// To construct, use [into_service][IntoService::into_service].
///
/// If you have a state of type `T` you wish to load rocket with, use [into_service][IntoService::into_service] on a pair
/// `(Rocket<Build>, async fn (&mut dyn Factory) -> Result<T, Error>)`.
///
/// Also see the [declare_service!][declare_service] macro.
pub struct RocketService<T: Sized> {
    rocket: Option<rocket::Rocket<rocket::Build>>,
    state_builder: Option<StateBuilder<T>>,
    runtime: Runtime,
}

#[cfg(feature = "web-rocket")]
impl IntoService for rocket::Rocket<rocket::Build> {
    type Service = RocketService<()>;
    fn into_service(self) -> Self::Service {
        RocketService {
            rocket: Some(self),
            state_builder: None,
            runtime: Runtime::new().unwrap(),
        }
    }
}

#[cfg(feature = "web-rocket")]
impl<T: Send + Sync + 'static> IntoService
    for (
        rocket::Rocket<rocket::Build>,
        fn(&mut dyn Factory) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + '_>>,
    )
{
    type Service = RocketService<T>;

    fn into_service(self) -> Self::Service {
        RocketService {
            rocket: Some(self.0),
            state_builder: Some(self.1),
            runtime: Runtime::new().unwrap(),
        }
    }
}

#[cfg(feature = "web-rocket")]
impl<T> Service for RocketService<T>
where
    T: Send + Sync + 'static,
{
    fn build(&mut self, factory: &mut dyn Factory) -> Result<(), Error> {
        if let Some(state_builder) = self.state_builder.take() {
            // We want to build any sqlx pools on the same runtime the client code will run on. Without this expect to get errors of no tokio reactor being present.
            let state = self.runtime.block_on(state_builder(factory))?;

            if let Some(rocket) = self.rocket.take() {
                self.rocket.replace(rocket.manage(state));
            }
        }

        Ok(())
    }

    fn bind(&mut self, addr: SocketAddr) -> Result<(), error::Error> {
        let rocket = self.rocket.take().expect("service has already been bound");

        let config = rocket::Config {
            address: addr.ip(),
            port: addr.port(),
            log_level: rocket::config::LogLevel::Normal,
            ..Default::default()
        };
        let launched = rocket.configure(config).launch();
        self.runtime
            .block_on(launched)
            .map_err(error::CustomError::new)?;
        Ok(())
    }
}

/// A wrapper that takes a user's future, gives the future a factory, and takes the returned service from the future
/// The returned service will be deployed by shuttle
pub struct SimpleService<T> {
    service: Option<T>,
    builder: Option<StateBuilder<T>>,
    runtime: Runtime,
}

impl<T> IntoService
    for fn(&mut dyn Factory) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + '_>>
where
    SimpleService<T>: Service,
{
    type Service = SimpleService<T>;

    fn into_service(self) -> Self::Service {
        SimpleService {
            service: None,
            builder: Some(self),
            runtime: Runtime::new().unwrap(),
        }
    }
}

#[cfg(feature = "web-rocket")]
impl Service for SimpleService<rocket::Rocket<rocket::Build>> {
    fn build(&mut self, factory: &mut dyn Factory) -> Result<(), Error> {
        if let Some(builder) = self.builder.take() {
            // We want to build any sqlx pools on the same runtime the client code will run on. Without this expect to get errors of no tokio reactor being present.
            let rocket = self.runtime.block_on(builder(factory))?;

            self.service = Some(rocket);
        }

        Ok(())
    }

    fn bind(&mut self, addr: SocketAddr) -> Result<(), error::Error> {
        let rocket = self.service.take().expect("service has already been bound");

        let config = rocket::Config {
            address: addr.ip(),
            port: addr.port(),
            log_level: rocket::config::LogLevel::Normal,
            ..Default::default()
        };
        let launched = rocket.configure(config).launch();
        self.runtime
            .block_on(launched)
            .map_err(error::CustomError::new)?;
        Ok(())
    }
}

#[cfg(feature = "web-axum")]
impl Service for SimpleService<sync_wrapper::SyncWrapper<axum::Router>> {
    fn build(&mut self, factory: &mut dyn Factory) -> Result<(), Error> {
        if let Some(builder) = self.builder.take() {
            // We want to build any sqlx pools on the same runtime the client code will run on. Without this expect to get errors of no tokio reactor being present.
            let axum = self.runtime.block_on(builder(factory))?;

            self.service = Some(axum);
        }

        Ok(())
    }

    fn bind(&mut self, addr: SocketAddr) -> Result<(), error::Error> {
        let axum = self
            .service
            .take()
            .expect("service has already been bound")
            .into_inner();

        self.runtime
            .block_on(async {
                axum::Server::bind(&addr)
                    .serve(axum.into_make_service())
                    .await
            })
            .map_err(error::CustomError::new)?;

        Ok(())
    }
}

/// 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:
///
/// ```rust,no_run
/// #[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][IntoService].
///
/// ## With a state
///
/// If your service requires a state, pass a type, a constructor and a state builder:
///
/// ```rust,no_run
/// 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][Service::build].
///
#[macro_export]
macro_rules! declare_service {
    ($service_type:ty, $constructor:path) => {
        #[no_mangle]
        pub extern "C" fn _create_service() -> *mut dyn $crate::Service {
            // Ensure constructor returns concrete type.
            let constructor: fn() -> $service_type = $constructor;

            let obj = $crate::IntoService::into_service(constructor());
            let boxed: Box<dyn $crate::Service> = Box::new(obj);
            Box::into_raw(boxed)
        }
    };
    ($service_type:ty, $constructor:path, $state_builder:path) => {
        #[no_mangle]
        pub extern "C" fn _create_service() -> *mut dyn $crate::Service {
            // Ensure constructor returns concrete type.
            let constructor: fn() -> $service_type = $constructor;

            // Ensure state builder is a function
            let state_builder: fn(
                &mut dyn $crate::Factory,
            ) -> std::pin::Pin<
                Box<dyn std::future::Future<Output = Result<_, $crate::Error>> + Send + '_>,
            > = |factory| Box::pin($state_builder(factory));

            let obj = $crate::IntoService::into_service((constructor(), state_builder));
            let boxed: Box<dyn $crate::Service> = Box::new(obj);
            Box::into_raw(boxed)
        }
    };
}