HTTP server, routing, and JSON responses — powered by Axum.
The foundation of every AID application. Create servers, define routes with handlers, and return JSON or text responses — all in a few lines.
Every function in the std.http module.
Creates a new HTTP server bound to the specified port. Does not start listening until server.start() is called.
server := http.new(port: 8080)
Registers a GET route handler. The handler receives a request object and must return a Response.
server.get("/users") => fn(req) -> Response {
Response.json({ users: [] })
}
Registers a POST route handler. Request body is available via req.body.
server.post("/users") => fn(req) -> Response {
Response.json({ created: req.body.name })
}
Registers a PUT route handler for updates.
Registers a DELETE route handler for resource removal.
Returns a JSON response with Content-Type: application/json. Accepts any AID object/map literal.
Returns a plain text response with Content-Type: text/plain.
Starts the HTTP server and begins listening for connections. Blocks the main thread.
A full HTTP API with multiple routes and response types.
module main
use std.http
fn main() {
server := http.new(port: 8080)
server.get("/") => fn(req) -> Response {
Response.json({
service: "My API",
version: "1.0.0"
})
}
server.get("/health") => fn(req) -> Response {
Response.json({ status: "ok" })
}
server.post("/echo") => fn(req) -> Response {
Response.text(req.body)
}
server.start()
}
What the AID compiler produces — an Axum application.
Transpiled Output (simplified)use axum::{Router, routing::{get, post}, Json, response::IntoResponse};
use serde_json::json;
// server.get("/") => handler
async fn handle_root() -> impl IntoResponse {
Json(json!({
"service": "My API",
"version": "1.0.0"
}))
}
// http.new(port: 8080) + server.start()
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(handle_root))
.route("/health", get(handle_health))
.route("/echo", post(handle_echo));
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080")
.await.unwrap();
axum::serve(listener, app).await.unwrap();
}