mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
Endpoint example: /stop endpoint & leak detection
This commit is contained in:
parent
b4a23e1000
commit
28b90c7b00
2 changed files with 74 additions and 34 deletions
|
@ -1,6 +1,7 @@
|
|||
const std = @import("std");
|
||||
const zap = @import("zap");
|
||||
const Endpoint = @import("endpoint.zig");
|
||||
const StopEndpoint = @import("stopendpoint.zig");
|
||||
|
||||
// this is just to demo that we can catch arbitrary slugs
|
||||
fn on_request(r: zap.SimpleRequest) void {
|
||||
|
@ -17,6 +18,8 @@ pub fn main() !void {
|
|||
}){};
|
||||
var allocator = gpa.allocator();
|
||||
|
||||
// we scope everything that can allocate within this block for leak detection
|
||||
{
|
||||
// setup listener
|
||||
var listener = zap.SimpleEndpointListener.init(
|
||||
allocator,
|
||||
|
@ -34,8 +37,11 @@ pub fn main() !void {
|
|||
var endpoint = Endpoint.init(allocator, "/users");
|
||||
defer endpoint.deinit();
|
||||
|
||||
var stopEp = StopEndpoint.init("/stop");
|
||||
|
||||
// add endpoint
|
||||
try listener.addEndpoint(endpoint.getUserEndpoint());
|
||||
try listener.addEndpoint(stopEp.getEndpoint());
|
||||
|
||||
// fake some users
|
||||
var uid: usize = undefined;
|
||||
|
@ -57,4 +63,9 @@ pub fn main() !void {
|
|||
// Try it with `zig build run-endpoint -Drelease-fast`
|
||||
.workers = 1,
|
||||
});
|
||||
}
|
||||
|
||||
// show potential memory leaks when ZAP is shut down
|
||||
const has_leaked = gpa.detectLeaks();
|
||||
std.log.debug("Has leaked: {}\n", .{has_leaked});
|
||||
}
|
||||
|
|
29
examples/endpoint/stopendpoint.zig
Normal file
29
examples/endpoint/stopendpoint.zig
Normal file
|
@ -0,0 +1,29 @@
|
|||
const std = @import("std");
|
||||
const zap = @import("zap");
|
||||
|
||||
/// A simple endpoint listening on the /stop route that shuts down zap
|
||||
/// the main thread usually continues at the instructions after the call to zap.start().
|
||||
pub const Self = @This();
|
||||
|
||||
endpoint: zap.SimpleEndpoint = undefined,
|
||||
|
||||
pub fn init(
|
||||
path: []const u8,
|
||||
) Self {
|
||||
return .{
|
||||
.endpoint = zap.SimpleEndpoint.init(.{
|
||||
.path = path,
|
||||
.get = get,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getEndpoint(self: *Self) *zap.SimpleEndpoint {
|
||||
return &self.endpoint;
|
||||
}
|
||||
|
||||
fn get(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||
_ = e;
|
||||
_ = r;
|
||||
zap.stop();
|
||||
}
|
Loading…
Add table
Reference in a new issue