use new middleware system

This commit is contained in:
jaina heartles 2022-11-25 17:43:30 -08:00
parent 43ddc7534b
commit 04a95a280b
3 changed files with 47 additions and 51 deletions

View file

@ -7,15 +7,11 @@ const util = @import("util");
const query_utils = @import("./query.zig"); const query_utils = @import("./query.zig");
const json_utils = @import("./json.zig"); const json_utils = @import("./json.zig");
const web_controllers = @import("./controllers/web.zig"); const web_endpoints = @import("./controllers/web.zig").routes;
const api_controllers = @import("./controllers/api.zig"); const api_endpoints = @import("./controllers/api.zig").routes;
const routes = api_controllers ++ web_controllers.routes;
const mdw = http.middleware; const mdw = http.middleware;
const router = mdw.Router(&.{});
const not_found = struct { const not_found = struct {
pub fn handler( pub fn handler(
_: @This(), _: @This(),
@ -35,7 +31,7 @@ const not_found = struct {
const base_handler = mdw.SplitUri(mdw.CatchErrors(not_found, mdw.DefaultErrorHandler)); const base_handler = mdw.SplitUri(mdw.CatchErrors(not_found, mdw.DefaultErrorHandler));
const inject_api_conn = struct { const inject_api_conn = struct {
fn getApiConn(alloc: std.mem.Allocator, api_source: anytype, req: anytype) !@TypeOf(api_source).Conn { fn getApiConn(alloc: std.mem.Allocator, api_source: anytype, req: anytype) !@TypeOf(api_source.*).Conn {
const host = req.headers.get("Host") orelse return error.NoHost; const host = req.headers.get("Host") orelse return error.NoHost;
const auth_header = req.headers.get("Authorization"); const auth_header = req.headers.get("Authorization");
const token = if (auth_header) |header| blk: { const token = if (auth_header) |header| blk: {
@ -64,20 +60,20 @@ const inject_api_conn = struct {
var api_conn = try getApiConn(ctx.allocator, ctx.api_source, req); var api_conn = try getApiConn(ctx.allocator, ctx.api_source, req);
defer api_conn.close(); defer api_conn.close();
return next.handle( return mdw.injectContext(.{ .api_conn = &api_conn }).handle(
req, req,
res, res,
mdw.injectContext(.{ .api_conn = &api_conn }), ctx,
{}, next,
); );
} }
}; }{};
pub fn EndpointRequest(comptime Endpoint: type) type { pub fn EndpointRequest(comptime Endpoint: type) type {
return struct { return struct {
pub const Args = if (@hasDecl(Endpoint, "Args")) Endpoint.Args else void; const Args = if (@hasDecl(Endpoint, "Args")) Endpoint.Args else void;
pub const Body = if (@hasDecl(Endpoint, "Body")) Endpoint.Body else void; const Body = if (@hasDecl(Endpoint, "Body")) Endpoint.Body else void;
pub const Query = if (@hasDecl(Endpoint, "Query")) Endpoint.Query else void; const Query = if (@hasDecl(Endpoint, "Query")) Endpoint.Query else void;
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
@ -92,7 +88,7 @@ pub fn EndpointRequest(comptime Endpoint: type) type {
const args_middleware = if (Args == void) const args_middleware = if (Args == void)
mdw.injectContext(.{ .args = {} }) mdw.injectContext(.{ .args = {} })
else else
mdw.ParsePathArgs(Args){}; mdw.ParsePathArgs(Endpoint.path, Args){};
const body_middleware = if (Body == void) const body_middleware = if (Body == void)
mdw.injectContext(.{ .body = {} }) mdw.injectContext(.{ .body = {} })
@ -100,7 +96,7 @@ pub fn EndpointRequest(comptime Endpoint: type) type {
mdw.ParseBody(Body){}; mdw.ParseBody(Body){};
const query_middleware = if (Query == void) const query_middleware = if (Query == void)
mdw.injectContext(.{ .query = {} }) mdw.injectContext(.{ .query_params = {} })
else else
mdw.ParseQueryParams(Query){}; mdw.ParseQueryParams(Query){};
}; };
@ -108,7 +104,7 @@ pub fn EndpointRequest(comptime Endpoint: type) type {
fn CallApiEndpoint(comptime Endpoint: type) type { fn CallApiEndpoint(comptime Endpoint: type) type {
return struct { return struct {
fn handle(_: @This(), req: anytype, res: anytype, ctx: anytype, _: void) !void { pub fn handle(_: @This(), req: anytype, res: anytype, ctx: anytype, _: void) !void {
const request = EndpointRequest(Endpoint){ const request = EndpointRequest(Endpoint){
.allocator = ctx.allocator, .allocator = ctx.allocator,
@ -118,12 +114,11 @@ fn CallApiEndpoint(comptime Endpoint: type) type {
.args = ctx.args, .args = ctx.args,
.body = ctx.body, .body = ctx.body,
.query = ctx.query, .query = ctx.query_params,
}; };
var response = Response{ .headers = http.Fields.init(ctx.allocator), .res = res }; var response = Response{ .headers = http.Fields.init(ctx.allocator), .res = res };
defer response.headers.deinit(); defer response.headers.deinit();
return Endpoint.handler(request, &response, ctx.api_conn); return Endpoint.handler(request, &response, ctx.api_conn);
} }
}; };
@ -133,7 +128,7 @@ pub fn apiEndpoint(
comptime Endpoint: type, comptime Endpoint: type,
) return_type: { ) return_type: {
const RequestType = EndpointRequest(Endpoint); const RequestType = EndpointRequest(Endpoint);
break :return_type mdw.Apply(std.meta.Tuple(.{ break :return_type mdw.Apply(std.meta.Tuple(&.{
mdw.Route, mdw.Route,
@TypeOf(RequestType.args_middleware), @TypeOf(RequestType.args_middleware),
@TypeOf(RequestType.query_middleware), @TypeOf(RequestType.query_middleware),
@ -155,17 +150,18 @@ pub fn apiEndpoint(
}); });
} }
pub fn routeRequest(api_source: anytype, req: *http.Request, res: *http.Response, alloc: std.mem.Allocator) void { const api_router = mdw.apply(.{
// TODO: hashmaps? mdw.mount("/api/v0/"),
_ = .{ api_source, req, res, alloc }; mdw.router(api_endpoints),
unreachable; });
//var response = Response{ .headers = http.Fields.init(alloc), .res = res };
//defer response.headers.deinit();
//const found = routeRequestInternal(api_source, req, &response, alloc); pub const router = mdw.apply(.{
mdw.split_uri,
//if (!found) response.status(.not_found) catch {}; mdw.catchErrors(mdw.default_error_handler),
} //mdw.router(.{api_router} ++ web_endpoints),
mdw.router(web_endpoints),
//api_router,
});
pub const AllocationStrategy = enum { pub const AllocationStrategy = enum {
arena, arena,

View file

@ -10,20 +10,20 @@ const streaming = @import("./api/streaming.zig");
const timelines = @import("./api/timelines.zig"); const timelines = @import("./api/timelines.zig");
pub const routes = .{ pub const routes = .{
controllers.apiEndpoint(auth.login), //controllers.apiEndpoint(auth.login),
controllers.apiEndpoint(auth.verify_login), //controllers.apiEndpoint(auth.verify_login),
controllers.apiEndpoint(communities.create), //controllers.apiEndpoint(communities.create),
controllers.apiEndpoint(communities.query), //controllers.apiEndpoint(communities.query),
controllers.apiEndpoint(invites.create), //controllers.apiEndpoint(invites.create),
controllers.apiEndpoint(users.create), //controllers.apiEndpoint(users.create),
controllers.apiEndpoint(notes.create), //controllers.apiEndpoint(notes.create),
controllers.apiEndpoint(notes.get), //controllers.apiEndpoint(notes.get),
controllers.apiEndpoint(streaming.streaming), //controllers.apiEndpoint(streaming.streaming),
controllers.apiEndpoint(timelines.global), //controllers.apiEndpoint(timelines.global),
controllers.apiEndpoint(timelines.local), //controllers.apiEndpoint(timelines.local),
controllers.apiEndpoint(timelines.home), //controllers.apiEndpoint(timelines.home),
controllers.apiEndpoint(follows.create), //controllers.apiEndpoint(follows.create),
controllers.apiEndpoint(follows.delete), //controllers.apiEndpoint(follows.delete),
controllers.apiEndpoint(follows.query_followers), //controllers.apiEndpoint(follows.query_followers),
controllers.apiEndpoint(follows.query_following), //controllers.apiEndpoint(follows.query_following),
}; };

View file

@ -2,11 +2,11 @@ const std = @import("std");
const controllers = @import("../controllers.zig"); const controllers = @import("../controllers.zig");
pub const routes = .{ pub const routes = .{
controllers.apiEndpoint(index), //controllers.apiEndpoint(index),
controllers.apiEndpoint(about), //controllers.apiEndpoint(about),
controllers.apiEndpoint(login), //controllers.apiEndpoint(login),
controllers.apiEndpoint(global_timeline), //controllers.apiEndpoint(global_timeline),
controllers.apiEndpoint(cluster.overview), //controllers.apiEndpoint(cluster.overview),
}; };
const index = struct { const index = struct {