Move router.zig
This commit is contained in:
parent
4d18f031a8
commit
5bd4df8740
3 changed files with 13 additions and 9 deletions
|
@ -1,10 +1,12 @@
|
|||
const std = @import("std");
|
||||
const root = @import("root");
|
||||
|
||||
pub const Router = @import("./http/Router.zig");
|
||||
|
||||
const ciutf8 = root.ciutf8;
|
||||
const Reader = std.net.Stream.Reader;
|
||||
const Writer = std.net.Stream.Writer;
|
||||
const Route = root.router.Route;
|
||||
const Route = Router.Route;
|
||||
|
||||
const HeaderMap = std.HashMap([]const u8, []const u8, struct {
|
||||
pub fn eql(_: @This(), a: []const u8, b: []const u8) bool {
|
||||
|
@ -187,7 +189,7 @@ fn handleHttpRequest(reader: Reader, writer: Writer) anyerror!void {
|
|||
.allocator = allocator,
|
||||
};
|
||||
|
||||
try root.router.routeRequest(&context);
|
||||
try (Router{ .routes = &root.routes }).routeRequest(&context);
|
||||
}
|
||||
|
||||
pub const Context = struct {
|
||||
|
|
|
@ -5,6 +5,10 @@ const Context = root.http.Context;
|
|||
const Method = root.http.Method;
|
||||
const ciutf8 = root.ciutf8;
|
||||
|
||||
const Self = @This();
|
||||
|
||||
routes: []const Route,
|
||||
|
||||
pub const Route = struct {
|
||||
const Segment = union(enum) {
|
||||
param: []const u8,
|
||||
|
@ -12,7 +16,6 @@ pub const Route = struct {
|
|||
};
|
||||
|
||||
pub const Handler = fn (*Context, *const Route) callconv(.Async) anyerror!void;
|
||||
|
||||
fn normalize(comptime path: []const u8) []const u8 {
|
||||
var arr: [path.len]u8 = undefined;
|
||||
|
||||
|
@ -151,8 +154,8 @@ fn handleNotFound(ctx: *Context) !void {
|
|||
try ctx.response.writer.writeAll("HTTP/1.1 404 Not Found\r\n\r\n");
|
||||
}
|
||||
|
||||
pub fn routeRequest(ctx: *Context) !void {
|
||||
for (root.routes) |*route| {
|
||||
pub fn routeRequest(self: Self, ctx: *Context) !void {
|
||||
for (self.routes) |*route| {
|
||||
if (route.method == ctx.request.method and route.matches(ctx.request.path)) {
|
||||
std.log.info("{s} {s}", .{ @tagName(ctx.request.method), ctx.request.path });
|
||||
ctx.request.route = route;
|
|
@ -3,14 +3,13 @@ const std = @import("std");
|
|||
pub const db = @import("./db.zig");
|
||||
pub const util = @import("./util.zig");
|
||||
pub const http = @import("./http.zig");
|
||||
pub const router = @import("./router.zig");
|
||||
|
||||
pub const Uuid = util.Uuid;
|
||||
pub const ciutf8 = util.ciutf8;
|
||||
|
||||
pub const io_mode = .evented;
|
||||
|
||||
const Route = router.Route;
|
||||
const Route = http.Router.Route;
|
||||
|
||||
pub const routes = [_]Route{
|
||||
Route.from(.GET, "/", staticString("Index Page")),
|
||||
|
@ -21,7 +20,7 @@ pub const routes = [_]Route{
|
|||
const this_scheme = "http";
|
||||
const this_host = "localhost:8080";
|
||||
|
||||
fn getUser(ctx: *http.Context, route: *const router.Route) anyerror!void {
|
||||
fn getUser(ctx: *http.Context, route: *const Route) anyerror!void {
|
||||
const id_str = route.arg("id", ctx.request.path);
|
||||
|
||||
const host = ctx.request.headers.get("host") orelse {
|
||||
|
@ -52,7 +51,7 @@ fn getUser(ctx: *http.Context, route: *const router.Route) anyerror!void {
|
|||
|
||||
fn staticString(comptime str: []const u8) Route.Handler {
|
||||
return (struct {
|
||||
fn func(ctx: *http.Context, _: *const router.Route) anyerror!void {
|
||||
fn func(ctx: *http.Context, _: *const Route) anyerror!void {
|
||||
try ctx.response.headers.put("Content-Type", "text/plain");
|
||||
try ctx.response.write(200, str);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue