Skip to content

Allow passing context variables #4

Description

@Luukdegram

Users only have access to a *Response and Request instance.
This means that if they require access to some allocator, database connection, or w/e, the variable must be global. This is very annoying to work with, and also prone to footguns.

The idea is to allow providing a context variable to the server, which is then accessible within the handler.
Using comptime we can ensure the types match.

This would look as follow:

try server.run(
...,
some_variable,
myHandle);

fn myHandle(ctx: @TypeOf(some_variable), resp: *Response, req: Request) !void {
    // access to `some_variable` is possible here.
    // Allows both constants and mutable pointers, depending on what was provided in the `run` function.
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions