Skip to main content

Crate shuttle_rama

Crate shuttle_rama 

Source
Expand description

§Shuttle service integration for the Rama framework

Learn more about rama at https://ramaproxy.org/ and see more [Rama v0.2] examples at https://github.com/plabayo/rama/tree/rama-0.2.0/examples.

§Examples

§Application Service
use rama::{
    Context, Layer,
    error::ErrorContext,
    http::{
        StatusCode,
        layer::forwarded::GetForwardedHeaderLayer,
        service::web::{Router, response::Result},
    },
    net::forwarded::Forwarded,
};

async fn hello_world(ctx: Context<()>) -> Result<String> {
    Ok(match ctx.get::<Forwarded>() {
        Some(forwarded) => format!(
            "Hello cloud user @ {}!",
            forwarded
                .client_ip()
                .context("missing IP information from user")
                .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?
        ),
        None => "Hello local user! Are you developing?".to_owned(),
    })
}

#[shuttle_runtime::main]
async fn main() -> Result<impl shuttle_rama::ShuttleService, shuttle_rama::ShuttleError> {
    let router = Router::new().get("/", hello_world);

    let app =
        // Shuttle sits behind a load-balancer,
        // so in case you want the real IP of the user,
        // you need to ensure this headers is handled.
        //
        // Learn more at <https://docs.shuttle.dev/docs/deployment-environment#https-traffic>
        GetForwardedHeaderLayer::x_forwarded_for().into_layer(router);

    Ok(shuttle_rama::RamaService::application(app))
}
§Transport Service
use rama::{net, service::service_fn};
use std::convert::Infallible;
use tokio::io::AsyncWriteExt;

async fn hello_world<S>(mut stream: S) -> Result<(), Infallible>
where
    S: net::stream::Stream + Unpin,
{
    const TEXT: &str = "Hello, Shuttle!";

    let resp = [
        "HTTP/1.1 200 OK",
        "Content-Type: text/plain",
        format!("Content-Length: {}", TEXT.len()).as_str(),
        "",
        TEXT,
        "",
    ]
    .join("\r\n");

    stream
        .write_all(resp.as_bytes())
        .await
        .expect("write to stream");

    Ok::<_, std::convert::Infallible>(())
}

#[shuttle_runtime::main]
async fn main() -> Result<impl shuttle_rama::ShuttleService, shuttle_rama::ShuttleError> {
    Ok(shuttle_rama::RamaService::transport(service_fn(
        hello_world,
    )))
}

Structs§

Application
Private type wrapper to indicate RamaService is used by the user from the Application layer (http(s)).
RamaService
A wrapper type for Service so we can implement shuttle_runtime::Service for it.
Transport
Private type wrapper to indicate RamaService is used by the user from the Transport layer (tcp).

Enums§

ShuttleError
An error that can occur in the process of building and deploying a service.

Traits§

ShuttleService
The core trait of the Shuttle platform. Every service deployed to Shuttle needs to implement this trait.

Type Aliases§

ShuttleRamaApplication
Shuttle service integration for the Rama framework
ShuttleRamaTransport
Shuttle service integration for the Rama framework