calog/libs/calogHttpd.h

33 lines
1.8 KiB
C

// calogHttpd.h -- calog polyglot HTTP server: serve routes whose handlers are script functions.
//
// A script registers route handlers (function values, in ANY engine); the server accepts
// connections on its own thread and, per request, invokes the matching handler ON ITS OWNING
// CONTEXT THREAD (via the callable machinery), then writes the returned response. Because a handler
// is an ordinary calog function value, re-registering a route replaces it live -- hot reload -- and
// different routes can be written in different languages and still share helpers via calogExport.
//
// httpdListen(port [, optsMap]) -> serverHandle opts: { host, backlog, maxBody }
// httpdRoute(serverHandle, method, path, handler) method "*" = any; exact-path match; re-register replaces
// httpdUnroute(serverHandle, method, path)
// httpdStop(serverHandle) stop accepting, close, release handlers
//
// The handler receives a request map { method, path, query, headers (map, lowercased names), body }
// and returns a response: a map { status (default 200), headers (map), body (string) }, a bare
// string (=> 200 with that body), or nil (=> 204 No Content). Bodies are binary-safe.
//
// v1 serves HTTP/1.1 with Connection: close and one request in flight at a time (a handler runs to
// completion before the next connection is accepted). TLS (https) and WebSocket are not yet included.
#ifndef CALOG_HTTPD_H
#define CALOG_HTTPD_H
#include "calog.h"
// Register the httpd natives on a runtime. Idempotent across runtimes.
int32_t calogHttpdRegister(CalogT *calog);
// Stop every server and release route handlers (once the last registered runtime unregisters).
// Call while the handler-owning contexts are still alive, before calogDestroy.
void calogHttpdShutdown(void);
#endif