Skip to content

HTTP, QUERIES, MIDDLEWARE

Web Development

Serve JSON APIs and static sites from one codebase. std.http gives you routing, middleware, and hardened file serving, with typed handlers the compiler checks before the process starts.

A web server in ZZ is a server value, a set of routes, and a listen call. Handlers are ordinary functions, so the type checker validates request and response shapes before the process ever starts. Middleware composes the same way routes do, and the same codebase reaches SQLite, Postgres, or MySQL through one query API.

  • Routing with typed params — unknown methods and misspelled param names fail at compile time.
  • Middleware that composes: CORS, secure headers, rate limiting, request IDs, and access logs.
  • Static file serving that is traversal-proof, with ETag/304 and single-range support.
  • One query API across SQLite, Postgres, and MySQL — rows decode straight into typed structs.
  • TLS termination with `http.listen_tls`, and WebSocket upgrades that hand you a raw TCP stream.

Modules

std.httpstd.sqlzstd.jsonstd.fs
api.zzzz
Run
import std.http
import std.json
import std.sqlz

func main() {
    db := sqlz.open("app.db")

    s := http.server()
    s = http.route(s, "GET", "/users", |req| {
        rows := sqlz.query(db, "SELECT id, name FROM users LIMIT 50")
        http.ok(json.stringify(rows) ?? "[]")
    })
    s = http.route(s, "GET", "/users/:id", |req| {
        match http.param(req, "id") {
            .ok(id) => {
                q := "SELECT id, name FROM users WHERE id = {id}"
                rows := sqlz.query(db, q)
                http.ok(json.stringify(rows) ?? "[]")
            }
            .err(_) => http.not_found(),
        }
    })

    http.listen_cfg(s, 8080, {"max_body_bytes": 1000000})
}

Runs in the playground — no install required. Open it

All four domainsBack to use casesCase studiesDocs