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.
}
Users only have access to a
*Response
andRequest
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: