lemon_pie icon indicating copy to clipboard operation
lemon_pie copied to clipboard

Allow passing context variables

Open Luukdegram opened this issue 2 years ago • 0 comments

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.
}

Luukdegram avatar Jun 29 '21 10:06 Luukdegram