add a slightly hacky "stop" endpoint in debug mode

This commit is contained in:
Emil Lerch 2026-01-12 12:47:22 -08:00
parent f1c85205f9
commit c1e864be61
Signed by: lobo
GPG key ID: A7B62D657EF764F8
3 changed files with 14 additions and 1 deletions

View file

@ -12,7 +12,7 @@ allocator: std.mem.Allocator,
httpz_server: httpz.Server(*Context), httpz_server: httpz.Server(*Context),
context: Context, context: Context,
const Context = struct { pub const Context = struct {
options: handler.HandleWeatherOptions, options: handler.HandleWeatherOptions,
rate_limiter: *RateLimiter, rate_limiter: *RateLimiter,
@ -45,6 +45,7 @@ pub fn init(
rate_limiter: *RateLimiter, rate_limiter: *RateLimiter,
) !Server { ) !Server {
const ctx = try allocator.create(Context); const ctx = try allocator.create(Context);
errdefer allocator.destroy(ctx);
ctx.* = .{ ctx.* = .{
.options = options, .options = options,
.rate_limiter = rate_limiter, .rate_limiter = rate_limiter,

View file

@ -21,6 +21,9 @@ pub const HandleWeatherOptions = struct {
geoip: *@import("../location/GeoIp.zig"), geoip: *@import("../location/GeoIp.zig"),
}; };
/// Only used for shutdown route (/stop) in debug mode
pub var server_instance: ?*httpz.Server(*@import("Server.zig").Context) = null;
pub fn handleWeather( pub fn handleWeather(
opts: *HandleWeatherOptions, opts: *HandleWeatherOptions,
req: *httpz.Request, req: *httpz.Request,
@ -42,6 +45,11 @@ pub fn handleWeather(
break :blk loc; break :blk loc;
} else break :blk client_ip; // no location, just use client ip instead } else break :blk client_ip; // no location, just use client ip instead
}; };
if (server_instance) |s|
if (std.mem.eql(u8, location, "stop")) {
s.stop();
return;
};
if (std.mem.eql(u8, "favicon.ico", location)) { if (std.mem.eql(u8, "favicon.ico", location)) {
res.header("Content-Type", "image/x-icon"); res.header("Content-Type", "image/x-icon");

View file

@ -80,7 +80,11 @@ pub fn main() !u8 {
.geoip = &geoip, .geoip = &geoip,
}, &rate_limiter); }, &rate_limiter);
// Only set up the server instance in debug mode
if (@import("builtin").mode == .Debug) @import("http/handler.zig").server_instance = &server.httpz_server;
try server.listen(); try server.listen();
std.debug.print("shutting down\n", .{});
return 0; return 0;
} }