Minimal API Server

Serves JSON-like dictionaries from the built-in HTTP module.

Path
docs/examples/web/api_server.gb
Category
Web Applications

This is the smallest useful API shape: a request handler receives a request dictionary and returns a response dictionary.

Source

import http;
import io;

func app(dict<string, any> request): dict<string, any> {
    if (request["path"] == "/health") {
        return {
            "status": 200,
            "headers": {"Content-Type": "application/json"},
            "body": "{\"ok\":true}"
        };
    }

    return {
        "status": 404,
        "headers": {"Content-Type": "application/json"},
        "body": "{\"error\":\"not found\"}"
    };
}

io.println("listening on http://127.0.0.1:8080");
http.serve("127.0.0.1:8080", app);