Finish using middleware impl

This commit is contained in:
jaina heartles 2022-11-26 17:52:30 -08:00
parent 9c5e46ec5a
commit 856619b286
11 changed files with 31 additions and 81 deletions

View file

@ -5,48 +5,6 @@ const http = @import("./lib.zig");
const util = @import("util");
const query_utils = @import("./query.zig");
const json_utils = @import("./json.zig");
//const json_utils = util;
//const query_utils = util;
fn printFields(comptime fields: []const std.builtin.Type.StructField) void {
comptime {
inline for (fields) |f| @compileLog(f.name.ptr, f.field_type);
}
}
fn AddFields(comptime lhs: type, comptime rhs: type) type {
const fields = std.meta.fields(lhs) ++ std.meta.fields(rhs);
comptime if (false) {
@compileLog("New Type");
@compileLog("lhs");
printFields(std.meta.fields(lhs));
@compileLog("rhs");
printFields(std.meta.fields(rhs));
@compileLog("all");
printFields(fields);
};
const Ctx = @Type(.{ .Struct = .{
.layout = .Auto,
.fields = fields,
.decls = &.{},
.is_tuple = false,
} });
return Ctx;
// return struct {
// path: []const u8,
// fragment: []const u8,
// query_string: []const u8,
// api_source: *anyopaque,
// };
}
fn addFields(lhs: anytype, rhs: anytype) AddFields(@TypeOf(lhs), @TypeOf(rhs)) {
var result: AddFields(@TypeOf(lhs), @TypeOf(rhs)) = undefined;
inline for (comptime std.meta.fieldNames(@TypeOf(lhs))) |f| @field(result, f) = @field(lhs, f);
inline for (comptime std.meta.fieldNames(@TypeOf(rhs))) |f| @field(result, f) = @field(rhs, f);
return result;
}
fn AddUniqueField(comptime Lhs: type, comptime N: usize, comptime name: [N]u8, comptime Val: type) type {
const Ctx = @Type(.{ .Struct = .{
.layout = .Auto,
@ -114,15 +72,6 @@ pub fn Apply(comptime Middlewares: type) type {
return ApplyInternal(std.meta.fields(Middlewares));
}
pub fn InjectContext(comptime Values: type) type {
return struct {
values: Values,
pub fn handle(self: @This(), req: anytype, res: anytype, ctx: anytype, next: anytype) !void {
return next.handle(req, res, addFields(ctx, self.values), {});
}
};
}
pub fn InjectContextValue(comptime name: []const u8, comptime V: type) type {
return struct {
val: V,
@ -131,9 +80,6 @@ pub fn InjectContextValue(comptime name: []const u8, comptime V: type) type {
}
};
}
pub fn injectContext(values: anytype) InjectContext(@TypeOf(values)) {
return .{ .values = values };
}
pub fn injectContextValue(comptime name: []const u8, val: anytype) InjectContextValue(name, @TypeOf(val)) {
return .{ .val = val };
@ -165,7 +111,7 @@ pub fn CatchErrors(comptime ErrorHandler: type) type {
return self.error_handler.handle(
req,
res,
addFields(ctx, .{ .err = err }),
addField(ctx, "err", err),
next,
);
};
@ -205,16 +151,20 @@ pub const split_uri = struct {
const path = query_split.first();
const query = query_split.rest();
const added_ctx = .{
.path = path,
.query_string = query,
.fragment_string = fragment,
};
const new_ctx = addField(
addField(
addField(ctx, "path", path),
"query_string",
query,
),
"fragment_string",
fragment,
);
return next.handle(
req,
res,
addFields(ctx, added_ctx),
new_ctx,
{},
);
}
@ -282,6 +232,7 @@ pub const Route = struct {
}
pub fn handle(self: @This(), req: anytype, res: anytype, ctx: anytype, next: anytype) !void {
std.log.debug("Testing path {s} against {s}", .{ ctx.path, self.desc.path });
return if (self.applies(req, ctx))
next.handle(req, res, ctx, {})
else

View file

@ -10,7 +10,7 @@ pub const Response = struct {
alloc: std.mem.Allocator,
stream: Stream,
should_close: bool = false,
was_opened: bool = true,
was_opened: bool = false,
pub const ResponseStream = response.ResponseStream(Stream.Writer);
pub fn open(self: *Response, status: http.Status, headers: *const http.Fields) !ResponseStream {
@ -126,6 +126,7 @@ pub const Server = struct {
initial_context: anytype,
handler: anytype,
) void {
defer conn.stream.close();
while (true) {
var req = request.parse(allocator, conn.stream.reader()) catch |err| {
const status: http.Status = switch (err) {

View file

@ -157,10 +157,8 @@ const api_router = mdw.apply(.{
pub const router = mdw.apply(.{
mdw.split_uri,
//mdw.catchErrors(mdw.default_error_handler),
mdw.catchErrors(mdw.default_error_handler),
mdw.router(.{api_router} ++ web_endpoints),
//mdw.router(web_endpoints),
//api_router,
});
pub const AllocationStrategy = enum {

View file

@ -3,7 +3,7 @@ const std = @import("std");
pub const login = struct {
pub const method = .POST;
pub const path = "/api/v0/auth/login";
pub const path = "/auth/login";
pub const Body = struct {
username: []const u8,
@ -21,7 +21,7 @@ pub const login = struct {
pub const verify_login = struct {
pub const method = .GET;
pub const path = "/api/v0/auth/login";
pub const path = "/auth/login";
pub fn handler(_: anytype, res: anytype, srv: anytype) !void {
const info = try srv.verifyAuthorization();

View file

@ -5,7 +5,7 @@ const QueryArgs = api.CommunityQueryArgs;
pub const create = struct {
pub const method = .POST;
pub const path = "/api/v0/communities";
pub const path = "/communities";
pub const Body = struct {
origin: []const u8,
@ -20,7 +20,7 @@ pub const create = struct {
pub const query = struct {
pub const method = .GET;
pub const path = "/api/v0/communities";
pub const path = "/communities";
pub const Query = QueryArgs;

View file

@ -2,7 +2,7 @@ const api = @import("api");
pub const create = struct {
pub const method = .POST;
pub const path = "/api/v0/invites";
pub const path = "/invites";
pub const Body = api.InviteOptions;

View file

@ -3,7 +3,7 @@ const util = @import("util");
pub const create = struct {
pub const method = .POST;
pub const path = "/api/v0/notes";
pub const path = "/notes";
pub const Body = struct {
content: []const u8,
@ -18,7 +18,7 @@ pub const create = struct {
pub const get = struct {
pub const method = .GET;
pub const path = "/api/v0/notes/:id";
pub const path = "/notes/:id";
pub const Args = struct {
id: util.Uuid,

View file

@ -3,7 +3,7 @@ const std = @import("std");
pub const streaming = struct {
pub const method = .GET;
pub const path = "/api/v0/streaming";
pub const path = "/streaming";
pub fn handler(req: anytype, response: anytype, _: anytype) !void {
var iter = req.headers.iterator();

View file

@ -4,7 +4,7 @@ const controller_utils = @import("../../controllers.zig").helpers;
pub const global = struct {
pub const method = .GET;
pub const path = "/api/v0/timelines/global";
pub const path = "/timelines/global";
pub const Query = api.TimelineArgs;
@ -16,7 +16,7 @@ pub const global = struct {
pub const local = struct {
pub const method = .GET;
pub const path = "/api/v0/timelines/local";
pub const path = "/timelines/local";
pub const Query = api.TimelineArgs;
@ -28,7 +28,7 @@ pub const local = struct {
pub const home = struct {
pub const method = .GET;
pub const path = "/api/v0/timelines/home";
pub const path = "/timelines/home";
pub const Query = api.TimelineArgs;

View file

@ -2,7 +2,7 @@ const api = @import("api");
pub const create = struct {
pub const method = .POST;
pub const path = "/api/v0/users";
pub const path = "/users";
pub const Body = struct {
username: []const u8,

View file

@ -6,7 +6,7 @@ const Uuid = util.Uuid;
pub const create = struct {
pub const method = .POST;
pub const path = "/api/v0/users/:id/follow";
pub const path = "/users/:id/follow";
pub const Args = struct {
id: Uuid,
@ -21,7 +21,7 @@ pub const create = struct {
pub const delete = struct {
pub const method = .DELETE;
pub const path = "/api/v0/users/:id/follow";
pub const path = "/users/:id/follow";
pub const Args = struct {
id: Uuid,
@ -36,7 +36,7 @@ pub const delete = struct {
pub const query_followers = struct {
pub const method = .GET;
pub const path = "/api/v0/users/:id/followers";
pub const path = "/users/:id/followers";
pub const Args = struct {
id: Uuid,
@ -53,7 +53,7 @@ pub const query_followers = struct {
pub const query_following = struct {
pub const method = .GET;
pub const path = "/api/v0/users/:id/following";
pub const path = "/users/:id/following";
pub const Args = struct {
id: Uuid,