mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
on_request fallback for SimpleHttpListener
This commit is contained in:
parent
2c93e1ef94
commit
5e349a9224
3 changed files with 16 additions and 3 deletions
|
@ -1,6 +1,6 @@
|
|||
.{
|
||||
.name = "zap",
|
||||
.version = "0.0.19",
|
||||
.version = "0.0.20",
|
||||
|
||||
.dependencies = .{
|
||||
.@"facil.io" = .{
|
||||
|
|
|
@ -2,6 +2,15 @@ const std = @import("std");
|
|||
const zap = @import("zap");
|
||||
const Endpoint = @import("endpoint.zig");
|
||||
|
||||
// this is just to demo that we can catch arbitrary slugs
|
||||
fn on_request(r: zap.SimpleRequest) void {
|
||||
if (r.path) |the_path| {
|
||||
std.debug.print("REQUESTED PATH: {s}\n", .{the_path});
|
||||
}
|
||||
|
||||
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{
|
||||
.thread_safe = true,
|
||||
|
@ -13,7 +22,7 @@ pub fn main() !void {
|
|||
allocator,
|
||||
.{
|
||||
.port = 3000,
|
||||
.on_request = null,
|
||||
.on_request = on_request,
|
||||
.log = true,
|
||||
.public_folder = "examples/endpoint/html",
|
||||
.max_clients = 100000,
|
||||
|
|
|
@ -178,13 +178,14 @@ pub const SimpleEndpointListener = struct {
|
|||
|
||||
/// static struct member endpoints
|
||||
var endpoints: std.ArrayList(*SimpleEndpoint) = undefined;
|
||||
var on_request: ?zap.SimpleHttpRequestFn = null;
|
||||
|
||||
pub fn init(a: std.mem.Allocator, l: ListenerSettings) Self {
|
||||
endpoints = std.ArrayList(*SimpleEndpoint).init(a);
|
||||
|
||||
var ls = l; // take copy of listener settings
|
||||
ls.on_request = onRequest;
|
||||
|
||||
on_request = l.on_request;
|
||||
return .{
|
||||
.listener = Listener.init(ls),
|
||||
.allocator = a,
|
||||
|
@ -227,5 +228,8 @@ pub const SimpleEndpointListener = struct {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (on_request) |foo| {
|
||||
foo(r);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue