Recipe / Rust

Actix-web (Rust) primer

Actix-web is one of the fastest async HTTP frameworks for Rust. This primer walks through scaffolding a project, wiring two routes, and shipping a release binary you can drop behind the Meridian gateway in under five minutes.

1. Scaffold the crate

Create a new binary crate with cargo new meridian-svc --bin, then add actix-web = "4",serde, andserde_json to yourCargo.toml. Actix runs on its own Tokio-based runtime, so no extra executor is required.

2. Wire a health + echo route

The example below registers a GET /health that returns a JSON status, and a POST /echo that round-trips a typed body. Handlers are plain async functions returning anything that implements Responder.

use actix_web::{web, App, HttpServer, HttpResponse, Responder};
use serde::{Deserialize, Serialize};

#[derive(Serialize)]
struct Health { status: &'static str }

#[derive(Deserialize)]
struct Echo { message: String }

async fn health() -> impl Responder {
    HttpResponse::Ok().json(Health { status: "ok" })
}

async fn echo(body: web::Json<Echo>) -> impl Responder {
    HttpResponse::Ok().json(serde_json::json!({
        "echoed": body.message,
    }))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/health", web::get().to(health))
            .route("/echo",   web::post().to(echo))
    })
    .bind(("0.0.0.0", 8080))?
    .run()
    .await
}

3. Ship it

Build with cargo build --release; the static binary lands in target/release/meridian-svc. Put it behind the Meridian gateway with a single upstream pointing at port 8080 and you get structured logging, model-key auth, and per-customer billing for free.