This example provides a simple “Hello, world!” Rust application that you can deploy with Shuttle. It’s a great starting point for learning how to use Shuttle and getting familiar with the deployment process for Rust applications.
In order to get started, initialize your project with shuttle init and pick the framework you want to use for this example.
Once you are done, your project should be setup with all the required dependencies so go ahead and copy/paste the relevant code snippet from below into your main.rs file.
Copy
usestd::convert::Infallible;usestd::future::Future;usestd::pin::Pin;usestd::task::{Context,Poll};#[derive(Clone)]structHelloWorld;impltower::Service<hyper::Request<hyper::Body>>forHelloWorld{typeResponse=hyper::Response<hyper::Body>;typeError=Infallible;typeFuture=Pin<Box<dynFuture<Output=Result<Self::Response,Self::Error>>+Send+Sync>>;fnpoll_ready(&mutself, _cx:&mutContext<'_>)->Poll<Result<(),Self::Error>>{Poll::Ready(Ok(()))}fncall(&mutself, _req:hyper::Request<hyper::Body>)->Self::Future{let body =hyper::Body::from("Hello, world!");let resp =hyper::Response::builder().status(200).body(body).expect("Unable to create the `hyper::Response` object");let fut =async{Ok(resp)};Box::pin(fut)}}#[shuttle_runtime::main]asyncfntower()->shuttle_tower::ShuttleTower<HelloWorld>{let service =HelloWorld;Ok(service.into())}