Http module

App

structSource

A web application: its routes, middleware, settings and shared state. A zeroed App is a working one. Add routes with get, post and the other verbs, then serve it with serve or start, or drive it in memory with test_client. Routes are added before serving and not changed while the app serves; the handlers run on the scheduler threads. The app owns the router, groups and setup errors it builds: release them with free_app after the servers have stopped. Strings you store in its fields (config.address, ...) are yours and are not copied.

Example
hello :: () -> string {
    return "hello";
}

main :: () {
    app: App;
    defer free_app(*app);

    get(*app, "/hello", hello);

    api := test_client(*app); // the app, driven in memory: no socket
    response, error := get(api, "/hello");
    defer close(*response);

    assert(error.kind == .NONE && response.status == 200);

    text, read_error := read_text(*response);
    defer free(text);

    assert(read_error.kind == .NONE && text == "hello");
}

Doesn't run in the browser (skipped on wasm).

Fields

state: *void
Shared state: app.state = *my_state; handlers take it as *State(My_State).
on_error: Error_Handler
Replaces the problem+json format of error responses.
json_options: Json_Options
Limits for the JSON bodies Json(T) and read_json read.
middleware: [..] Middleware
Middleware that wraps every request, including ones no route answers.
router: Router
setup_errors: [..] string
Problems found while routes were added (two routes that conflict), reported at startup.
groups: [..] *void
Groups made by group, released with the app.
shutdown_flag: s32
Set by shutdown; the servers of serve poll it.

See also