diff --git a/.gitignore b/.gitignore index 33ed699..6ffec0a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/zig-out -/zig-cache \ No newline at end of file +**/zig-out +**/zig-cache diff --git a/src/db.zig b/src/db.zig new file mode 100644 index 0000000..0a0f106 --- /dev/null +++ b/src/db.zig @@ -0,0 +1,21 @@ +pub const Actor = struct { + id: u128, + display_name: ?[]const u8 = null, + handle: []const u8, + host: []const u8, + bio: ?[]const u8 = null, +}; + +const this_host = "localhost:8080"; + +const actor = Actor{ + .id = 1234, + .handle = "testacct", + .host = this_host, +}; + +pub fn getActorById(id: u128) !?Actor { + if (id == actor.id) return actor; + + return null; +} diff --git a/src/main.zig b/src/main.zig index 2114359..6ef975c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,11 +1,12 @@ const std = @import("std"); +const db = @import("./db.zig"); pub const io_mode = .evented; const Reader = std.net.Stream.Reader; const Writer = std.net.Stream.Writer; -const case_insensitive_utf8 = struct { +const ciutf8 = struct { const Hash = std.hash.Wyhash; const View = std.unicode.Utf8View; const toLower = std.ascii.toLower; @@ -76,11 +77,11 @@ const case_insensitive_utf8 = struct { const HeaderMap = std.HashMap([]const u8, []const u8, struct { pub fn eql(_: @This(), a: []const u8, b: []const u8) bool { - return case_insensitive_utf8.eql(a, b); + return ciutf8.eql(a, b); } pub fn hash(_: @This(), str: []const u8) u64 { - return case_insensitive_utf8.hash(str); + return ciutf8.hash(str); } }, std.hash_map.default_max_load_percentage); @@ -431,7 +432,7 @@ const Route = struct { const slice = path[segment_start..index]; const match = switch (seg) { - .literal => |str| case_insensitive_utf8.eql(slice, str), + .literal => |str| ciutf8.eql(slice, str), .param => true, }; @@ -504,14 +505,8 @@ const routes = [_]Route{ const this_scheme = "http"; const this_host = "localhost:8080"; -const account = .{ - .id = "abc123", - .handle = "testacct", - .host = this_host, -}; - fn getUser(ctx: *Context) anyerror!void { - const id = ctx.request.arg("id"); + const id_str = ctx.request.arg("id"); const host = ctx.request.headers.get("host"); if (host == null) { @@ -519,8 +514,11 @@ fn getUser(ctx: *Context) anyerror!void { return; } - if (!std.mem.eql(u8, account.id, id) or - !std.mem.eql(u8, account.host, ctx.request.headers.get("host").?)) + const id = try std.fmt.parseInt(u128, id_str, 10); + const actor = try db.getActorById(id); + + if (actor == null or + !std.mem.eql(u8, actor.?.host, ctx.request.headers.get("host").?)) { try ctx.response.statusOnly(404); return; @@ -530,8 +528,8 @@ fn getUser(ctx: *Context) anyerror!void { var writer = try ctx.response.open(200); try writer.writeAll("{\"type\":\"Person\","); - try writer.print("\"id\":\"{s}://{s}/user/{s}\",", .{ this_scheme, this_host, id }); - try writer.print("\"preferredUsername\":\"{s}\"", .{account.handle}); + try writer.print("\"id\":\"{s}://{s}/user/{}\",", .{ this_scheme, this_host, id }); + try writer.print("\"preferredUsername\":\"{s}\"", .{actor.?.handle}); try writer.writeAll("}"); }