mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
some refactoring
This commit is contained in:
parent
f13160eda6
commit
5e76e17baf
5 changed files with 56 additions and 55 deletions
|
@ -34,7 +34,7 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
.{ .name = "hello", .src = "examples/hello/hello.zig" },
|
.{ .name = "hello", .src = "examples/hello/hello.zig" },
|
||||||
.{ .name = "https", .src = "examples/https/https.zig" },
|
.{ .name = "https", .src = "examples/https/https.zig" },
|
||||||
.{ .name = "hello2", .src = "examples/hello2/hello2.zig" },
|
.{ .name = "hello2", .src = "examples/hello2/hello2.zig" },
|
||||||
.{ .name = "hello3", .src = "examples/hello3/hello3.zig" },
|
.{ .name = "simple_router", .src = "examples/simple_router/simple_router.zig" },
|
||||||
.{ .name = "routes", .src = "examples/routes/routes.zig" },
|
.{ .name = "routes", .src = "examples/routes/routes.zig" },
|
||||||
.{ .name = "serve", .src = "examples/serve/serve.zig" },
|
.{ .name = "serve", .src = "examples/serve/serve.zig" },
|
||||||
.{ .name = "hello_json", .src = "examples/hello_json/hello_json.zig" },
|
.{ .name = "hello_json", .src = "examples/hello_json/hello_json.zig" },
|
||||||
|
|
|
@ -7,71 +7,54 @@ const RouterError = error{
|
||||||
EmptyPath,
|
EmptyPath,
|
||||||
};
|
};
|
||||||
|
|
||||||
// inline closure for RequestFn with self argument
|
const Self = @This();
|
||||||
pub inline fn RequestHandler(self: anytype, func: *const fn (@TypeOf(self), zap.Request) void) *const fn (zap.Request) void {
|
|
||||||
return (opaque {
|
|
||||||
var hidden_self: @TypeOf(self) = undefined;
|
|
||||||
var hidden_func: *const fn (@TypeOf(self), zap.Request) void = undefined;
|
|
||||||
pub fn init(h_self: @TypeOf(self), h_func: *const fn (@TypeOf(self), zap.Request) void) *const @TypeOf(run) {
|
|
||||||
hidden_self = h_self;
|
|
||||||
hidden_func = h_func;
|
|
||||||
return &run;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run(req: zap.Request) void {
|
pub const Options = struct {
|
||||||
hidden_func(hidden_self, req);
|
|
||||||
}
|
|
||||||
}).init(self, func);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const RouterOptions = struct {
|
|
||||||
not_found: ?zap.HttpRequestFn = null,
|
not_found: ?zap.HttpRequestFn = null,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Router = struct {
|
routes: std.StringHashMap(zap.HttpRequestFn),
|
||||||
const Self = @This();
|
not_found: ?zap.HttpRequestFn,
|
||||||
|
|
||||||
routes: std.StringHashMap(zap.HttpRequestFn),
|
pub fn init(allocator: Allocator, options: Options) Self {
|
||||||
not_found: ?zap.HttpRequestFn,
|
return .{
|
||||||
|
.routes = std.StringHashMap(zap.HttpRequestFn).init(allocator),
|
||||||
|
|
||||||
pub fn init(allocator: Allocator, options: RouterOptions) Self {
|
.not_found = options.not_found,
|
||||||
return .{
|
};
|
||||||
.routes = std.StringHashMap(zap.HttpRequestFn).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.HttpRequestFn) !void {
|
||||||
|
if (path.len == 0) {
|
||||||
|
return RouterError.EmptyPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Self) void {
|
const route = self.routes.get(path);
|
||||||
self.routes.deinit();
|
|
||||||
|
if (route != null) {
|
||||||
|
return RouterError.AlreadyExists;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_func(self: *Self, path: []const u8, h: zap.HttpRequestFn) !void {
|
try self.routes.put(path, h);
|
||||||
if (path.len == 0) {
|
}
|
||||||
return RouterError.EmptyPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
const route = self.routes.get(path);
|
pub fn serve(self: *Self, r: zap.Request) void {
|
||||||
|
const path = if (r.path) |p| p else "/";
|
||||||
|
|
||||||
if (route != null) {
|
var route = self.routes.get(path);
|
||||||
return RouterError.AlreadyExists;
|
|
||||||
}
|
|
||||||
|
|
||||||
try self.routes.put(path, h);
|
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;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
pub fn serve(self: *Self, r: zap.Request) 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
18
src/util.zig
18
src/util.zig
|
@ -1,5 +1,23 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const fio = @import("fio.zig");
|
const fio = @import("fio.zig");
|
||||||
|
const zap = @import("zap.zig");
|
||||||
|
|
||||||
|
/// capture self for RequestFn signature support
|
||||||
|
pub inline fn RequestHandler(self: anytype, func: *const fn (@TypeOf(self), zap.Request) void) *const fn (zap.Request) void {
|
||||||
|
return (opaque {
|
||||||
|
var hidden_self: @TypeOf(self) = undefined;
|
||||||
|
var hidden_func: *const fn (@TypeOf(self), zap.Request) void = undefined;
|
||||||
|
pub fn init(h_self: @TypeOf(self), h_func: *const fn (@TypeOf(self), zap.Request) void) *const @TypeOf(run) {
|
||||||
|
hidden_self = h_self;
|
||||||
|
hidden_func = h_func;
|
||||||
|
return &run;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(req: zap.Request) void {
|
||||||
|
hidden_func(hidden_self, req);
|
||||||
|
}
|
||||||
|
}).init(self, func);
|
||||||
|
}
|
||||||
|
|
||||||
/// Used internally: convert a FIO object into its string representation.
|
/// Used internally: convert a FIO object into its string representation.
|
||||||
/// note: since this is called from within request functions, we don't make
|
/// note: since this is called from within request functions, we don't make
|
||||||
|
|
|
@ -52,7 +52,7 @@ pub const Tls = @import("tls.zig");
|
||||||
/// ```
|
/// ```
|
||||||
pub const Endpoint = @import("endpoint.zig");
|
pub const Endpoint = @import("endpoint.zig");
|
||||||
|
|
||||||
pub usingnamespace @import("router.zig");
|
pub const Router = @import("router.zig");
|
||||||
|
|
||||||
pub usingnamespace @import("util.zig");
|
pub usingnamespace @import("util.zig");
|
||||||
pub usingnamespace @import("http.zig");
|
pub usingnamespace @import("http.zig");
|
||||||
|
|
Loading…
Add table
Reference in a new issue