shuttle_rocket/
lib.rs

1#![doc = include_str!("../README.md")]
2use std::net::SocketAddr;
3
4pub use rocket;
5
6/// A wrapper type for [rocket::Rocket<rocket::Build>] so we can implement [shuttle_runtime::Service] for it.
7pub struct RocketService(pub rocket::Rocket<rocket::Build>);
8
9#[shuttle_runtime::async_trait]
10impl shuttle_runtime::Service for RocketService {
11    /// Takes the router that is returned by the user in their [shuttle_runtime::main] function
12    /// and binds to an address passed in by shuttle.
13    async fn bind(mut self, addr: SocketAddr) -> Result<(), shuttle_runtime::Error> {
14        let shutdown = rocket::config::Shutdown {
15            ctrlc: false,
16            ..rocket::config::Shutdown::default()
17        };
18
19        let config = self
20            .0
21            .figment()
22            .clone()
23            .merge((rocket::Config::ADDRESS, addr.ip()))
24            .merge((rocket::Config::PORT, addr.port()))
25            .merge((rocket::Config::LOG_LEVEL, rocket::config::LogLevel::Off))
26            .merge((rocket::Config::SHUTDOWN, shutdown));
27
28        let _rocket = self
29            .0
30            .configure(config)
31            .launch()
32            .await
33            .map_err(shuttle_runtime::CustomError::new)?;
34
35        Ok(())
36    }
37}
38
39impl From<rocket::Rocket<rocket::Build>> for RocketService {
40    fn from(router: rocket::Rocket<rocket::Build>) -> Self {
41        Self(router)
42    }
43}
44
45#[doc = include_str!("../README.md")]
46pub type ShuttleRocket = Result<RocketService, shuttle_runtime::Error>;