Skip to content

HTTP server

A minimal serve handler on :PORT daemon. Run with c4 serve examples/04_serve.c4 and it boots a ThreadingHTTPServer that JSON-encodes return values.

Source: examples/04_serve.c4

# An inline HTTP handler registered via the `serve ... on :port` syntax.
# In M1, `serve` registers the handler in the runtime; the daemon mode
# (M4) actually binds and listens. This example demonstrates the syntax
# and shows how to inspect the registry.

use cobra4.runtime.core as rt

fn api(req) {
    name = req?.params?.name ?? "world"
    return {"hello": name}
}

serve api on :8080

# In M1, no actual server starts — verify registration:
entries = rt.serve_registry()
log("serve registered", count=len(entries), port=entries[0].port)

Run it

c4 run examples/04_serve.c4