Luukdegram / lemon_pie

Library to build fast, reliable, Gemini servers with ease
MIT License
8 stars 1 forks source link

Allow passing context variables #4

Open Luukdegram opened 3 years ago

Luukdegram commented 3 years ago

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