-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simple router with handler clojures support
- Loading branch information
1 parent
cbccf53
commit 8de8b82
Showing
4 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
const std = @import("std"); | ||
const zap = @import("zap"); | ||
const Allocator = std.mem.Allocator; | ||
|
||
fn on_request_verbose(r: zap.SimpleRequest) void { | ||
if (r.path) |the_path| { | ||
std.debug.print("PATH: {s}\n", .{the_path}); | ||
} | ||
|
||
if (r.query) |the_query| { | ||
std.debug.print("QUERY: {s}\n", .{the_query}); | ||
} | ||
r.sendBody("<html><body><h1>Hello from ZAP!!!</h1></body></html>") catch return; | ||
} | ||
|
||
pub const SomePackage = struct { | ||
const Self = @This(); | ||
|
||
allocator: Allocator, | ||
a: i8, | ||
b: i8, | ||
|
||
pub fn init(allocator: Allocator, a: i8, b: i8) Self { | ||
return .{ | ||
.allocator = allocator, | ||
.a = a, | ||
.b = b, | ||
}; | ||
} | ||
|
||
pub fn getA(self: *Self, req: zap.SimpleRequest) void { | ||
std.log.warn("get_a_requested", .{}); | ||
|
||
const string = std.fmt.allocPrint( | ||
self.allocator, | ||
"A value is {d}\n", | ||
.{self.a}, | ||
) catch return; | ||
defer self.allocator.free(string); | ||
|
||
req.sendBody(string) catch return; | ||
} | ||
|
||
pub fn getB(self: *Self, req: zap.SimpleRequest) void { | ||
std.log.warn("get_b_requested", .{}); | ||
|
||
const string = std.fmt.allocPrint( | ||
self.allocator, | ||
"B value is {d}\n", | ||
.{self.b}, | ||
) catch return; | ||
defer self.allocator.free(string); | ||
|
||
req.sendBody(string) catch return; | ||
} | ||
|
||
pub fn incrementA(self: *Self, req: zap.SimpleRequest) void { | ||
std.log.warn("increment_a_requested", .{}); | ||
|
||
self.a += 1; | ||
|
||
req.sendBody("incremented A") catch return; | ||
} | ||
}; | ||
|
||
fn not_found(req: zap.SimpleRequest) void { | ||
std.debug.print("not found handler", .{}); | ||
|
||
req.sendBody("Not found") catch return; | ||
} | ||
|
||
pub fn main() !void { | ||
var gpa = std.heap.GeneralPurposeAllocator(.{ | ||
.thread_safe = true, | ||
}){}; | ||
var allocator = gpa.allocator(); | ||
|
||
var simpleRouter = zap.SimpleRouter.init(allocator, .{ | ||
.not_found = not_found, | ||
}); | ||
defer simpleRouter.deinit(); | ||
|
||
var somePackage = SomePackage.init(allocator, 1, 2); | ||
|
||
try simpleRouter.handle_func("/", on_request_verbose); | ||
|
||
try simpleRouter.handle_func("/geta", zap.SimpleRequestHandler(&somePackage, SomePackage.getA)); | ||
|
||
try simpleRouter.handle_func("/getb", zap.SimpleRequestHandler(&somePackage, SomePackage.getB)); | ||
|
||
try simpleRouter.handle_func("/inca", zap.SimpleRequestHandler(&somePackage, SomePackage.incrementA)); | ||
|
||
var listener = zap.SimpleHttpListener.init(.{ | ||
.port = 3000, | ||
.on_request = zap.SimpleRequestHandler(&simpleRouter, &zap.SimpleRouter.serve), | ||
.log = true, | ||
.max_clients = 100000, | ||
}); | ||
try listener.listen(); | ||
|
||
std.debug.print("Listening on 0.0.0.0:3000\n", .{}); | ||
|
||
// start worker threads | ||
zap.start(.{ | ||
.threads = 2, | ||
.workers = 2, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const std = @import("std"); | ||
const zap = @import("zap.zig"); | ||
|
||
const Allocator = std.mem.Allocator; | ||
const SimpleRouterError = error{ | ||
AlreadyExists, | ||
EmptyPath, | ||
}; | ||
|
||
// inline closure for SimpleRequestFn with self argument | ||
pub inline fn SimpleRequestHandler(self: anytype, func: *const fn (@TypeOf(self), zap.SimpleRequest) void) *const fn (zap.SimpleRequest) void { | ||
return (opaque { | ||
var hidden_self: @TypeOf(self) = undefined; | ||
var hidden_func: *const fn (@TypeOf(self), zap.SimpleRequest) void = undefined; | ||
pub fn init(h_self: @TypeOf(self), h_func: *const fn (@TypeOf(self), zap.SimpleRequest) void) *const @TypeOf(run) { | ||
hidden_self = h_self; | ||
hidden_func = h_func; | ||
return &run; | ||
} | ||
|
||
fn run(req: zap.SimpleRequest) void { | ||
hidden_func(hidden_self, req); | ||
} | ||
}).init(self, func); | ||
} | ||
|
||
pub const SimpleRouterOptions = struct { | ||
not_found: ?zap.SimpleHttpRequestFn = null, | ||
}; | ||
|
||
pub const SimpleRouter = struct { | ||
const Self = @This(); | ||
|
||
routes: std.StringHashMap(zap.SimpleHttpRequestFn), | ||
not_found: ?zap.SimpleHttpRequestFn, | ||
|
||
pub fn init(allocator: Allocator, options: SimpleRouterOptions) Self { | ||
return .{ | ||
.routes = std.StringHashMap(zap.SimpleHttpRequestFn).init(allocator), | ||
|
||
.not_found = options.not_found, | ||
}; | ||
} | ||
|
||
pub fn deinit(self: *Self) void { | ||
self.routes.deinit(); | ||
} | ||
|
||
pub fn handle_func(self: *Self, path: []const u8, h: zap.SimpleHttpRequestFn) !void { | ||
if (path.len == 0) { | ||
return SimpleRouterError.EmptyPath; | ||
} | ||
|
||
const route = self.routes.get(path); | ||
|
||
if (route != null) { | ||
return SimpleRouterError.AlreadyExists; | ||
} | ||
|
||
try self.routes.put(path, h); | ||
} | ||
|
||
pub fn serve(self: *Self, r: zap.SimpleRequest) void { | ||
var route = self.routes.get(r.path.?); | ||
|
||
if (route) |handler| { | ||
handler(r); | ||
} else if (self.not_found) |handler| { | ||
// not found handler | ||
handler(r); | ||
} else { | ||
// default 404 output | ||
r.setStatus(.not_found); | ||
r.sendBody("404 Not Found") catch return; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters