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

View file

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

View file

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