Repository navigation
Expand file tree
/
Copy pathexample.zig
More file actions
68 lines (55 loc) · 1.71 KB
/
Copy pathexample.zig
File metadata and controls
68 lines (55 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! A compact Zig syntax gallery for the TensorFoundry theme.
const std = @import("std");
const default_route_order: u16 = 100;
const Method = enum {
get,
post,
};
const RouteError = error{
EmptyRoute,
DuplicateRoute,
};
const RouteInfo = struct {
handler: *const fn ([]const u8) anyerror![]const u8,
description: []const u8,
method: Method,
order: u16 = default_route_order,
is_proxy: bool = true,
};
const RouteRegistry = struct {
allocator: std.mem.Allocator,
routes: std.StringHashMap(RouteInfo),
pub fn init(allocator: std.mem.Allocator) RouteRegistry {
return .{
.allocator = allocator,
.routes = std.StringHashMap(RouteInfo).init(allocator),
};
}
pub fn registerProxyRoute(
self: *RouteRegistry,
route: []const u8,
handler: *const fn ([]const u8) anyerror![]const u8,
description: []const u8,
method: Method,
) RouteError!void {
if (route.len == 0) return RouteError.EmptyRoute;
if (self.routes.contains(route)) return RouteError.DuplicateRoute;
try self.routes.put(route, .{
.handler = handler,
.description = std.mem.trim(u8, description, " \\t\\n"),
.method = method,
});
}
pub fn deinit(self: *RouteRegistry) void {
self.routes.deinit();
}
};
fn identityHandler(route: []const u8) ![]const u8 {
return route;
}
test "registers a proxy route" {
var registry = RouteRegistry.init(std.testing.allocator);
defer registry.deinit();
try registry.registerProxyRoute("/health", identityHandler, "Health check", .get);
try std.testing.expectEqual(@as(usize, 1), registry.routes.count());
}