Hello World

This example provides a simple “Hello, world!” Rust application that you can deploy with Cyndra. It’s a great starting point for learning how to use Cyndra and getting familiar with the deployment process for Rust applications.

In order to get started, initialize your project with cyndra 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.

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(),
    })
}

#[cyndra_runtime::main]
async fn main() -> Result<impl cyndra_rama::CyndraService, cyndra_rama::CyndraError> {
    let router = Router::new().get("/", hello_world);

    let app =
        // Cyndra 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.cyndra.dev/docs/deployment-environment#https-traffic>
        GetForwardedHeaderLayer::x_forwarded_for().into_layer(router);

    Ok(cyndra_rama::RamaService::application(app))
}

Run the example locally with:

cyndra run

In order to deploy the example, simply run:

cyndra deploy