add a slightly hacky "stop" endpoint in debug mode
This commit is contained in:
parent
f1c85205f9
commit
c1e864be61
3 changed files with 14 additions and 1 deletions
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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");
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue