mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
123 lines
3.1 KiB
Zig
123 lines
3.1 KiB
Zig
//!
|
|
//! Part of the Zap examples.
|
|
//!
|
|
//! Build me with `zig build simple_router`.
|
|
//! Run me with `zig build run-simple_router`.
|
|
//!
|
|
const std = @import("std");
|
|
const zap = @import("zap");
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
fn on_request_verbose(r: zap.Request) !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 {
|
|
allocator: Allocator,
|
|
a: i8,
|
|
b: i8,
|
|
|
|
pub fn init(allocator: Allocator, a: i8, b: i8) SomePackage {
|
|
return .{
|
|
.allocator = allocator,
|
|
.a = a,
|
|
.b = b,
|
|
};
|
|
}
|
|
|
|
pub fn getA(self: *SomePackage, req: zap.Request) !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: *SomePackage, req: zap.Request) !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: *SomePackage, req: zap.Request) !void {
|
|
std.log.warn("increment_a_requested", .{});
|
|
|
|
self.a += 1;
|
|
|
|
req.sendBody("incremented A") catch return;
|
|
}
|
|
};
|
|
|
|
fn not_found(req: zap.Request) !void {
|
|
std.debug.print("not found handler", .{});
|
|
|
|
try req.sendBody("Not found");
|
|
}
|
|
|
|
pub fn main() !void {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{
|
|
.thread_safe = true,
|
|
}){};
|
|
const allocator = gpa.allocator();
|
|
|
|
var simpleRouter = zap.Router.init(allocator, .{
|
|
.not_found = not_found,
|
|
});
|
|
defer simpleRouter.deinit();
|
|
|
|
var somePackage = SomePackage.init(allocator, 1, 2);
|
|
|
|
try simpleRouter.handle_func_unbound("/", on_request_verbose);
|
|
|
|
try simpleRouter.handle_func("/geta", &somePackage, &SomePackage.getA);
|
|
|
|
try simpleRouter.handle_func("/getb", &somePackage, &SomePackage.getB);
|
|
|
|
try simpleRouter.handle_func("/inca", &somePackage, &SomePackage.incrementA);
|
|
|
|
var listener = zap.HttpListener.init(.{
|
|
.port = 3000,
|
|
.on_request = simpleRouter.on_request_handler(),
|
|
.log = true,
|
|
.max_clients = 100000,
|
|
});
|
|
try listener.listen();
|
|
|
|
std.debug.print(
|
|
\\ Listening on 0.0.0.0:3000
|
|
\\
|
|
\\ Test me with:
|
|
\\ curl http://localhost:3000/
|
|
\\ curl http://localhost:3000/geta
|
|
\\ curl http://localhost:3000/getb
|
|
\\ curl http://localhost:3000/inca
|
|
\\
|
|
\\
|
|
, .{});
|
|
// start worker threads
|
|
zap.start(.{
|
|
.threads = 2,
|
|
|
|
// Must be 1 if state is shared
|
|
.workers = 1,
|
|
});
|
|
}
|