typestar

A router and a handler in Rust

An axum service is a Router of routes, each pointing at an async handler.

use axum::{routing::get, Router};

async fn health() -> &'static str {
    "ok"
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/healthz", get(health))
        .route("/", get(|| async { "typestar" }));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:8080")
        .await
        .unwrap();
    axum::serve(listener, app).await.unwrap();
}

How it works

  1. A handler is an async fn returning something that is a response.
  2. route pairs a path with a method and its handler.
  3. serve takes a listener and drives the router.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
337
Tokens
114
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 65 seconds.

Type this snippet

Step 1 of 3 in Routes & handlers, step 1 of 9 in Web services with axum.

Next →