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; pub const routes = [_]Route{ Route.from(.GET, "/", staticString("Index Page")), Route.from(.GET, "/abc", staticString("abc")), Route.from(.GET, "/user/:id", getUser), }; const this_scheme = "http"; const this_host = "localhost:8080"; fn getUser(ctx: *http.Context, route: *const router.Route) anyerror!void { const id_str = route.arg("id", ctx.request.path); const host = ctx.request.headers.get("host") orelse { try ctx.response.statusOnly(400); return; }; const id = Uuid.parse(id_str) catch { try ctx.response.statusOnly(400); return; }; const actor = try db.getActorById(id); if (actor == null or !std.mem.eql(u8, actor.?.host, host)) { try ctx.response.statusOnly(404); return; } try ctx.response.headers.put("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""); var writer = try ctx.response.open(200); try writer.writeAll("{\"type\":\"Person\","); try writer.print("\"id\":\"{s}://{s}/user/{}\",", .{ this_scheme, this_host, id }); try writer.print("\"preferredUsername\":\"{s}\"", .{actor.?.handle}); try writer.writeAll("}"); } fn staticString(comptime str: []const u8) Route.Handler { return (struct { fn func(ctx: *http.Context, _: *const router.Route) anyerror!void { try ctx.response.headers.put("Content-Type", "text/plain"); try ctx.response.write(200, str); } }).func; } pub fn main() anyerror!void { var srv = std.net.StreamServer.init(.{ .reuse_address = true }); defer srv.deinit(); const uuid = try Uuid.parse("f75f5160-12d3-42c2-a81d-ad2245b7a74b"); std.log.debug("{}", .{uuid}); try srv.listen(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable); while (true) { const conn = try srv.accept(); // todo: keep track of connections _ = async http.handleConnection(conn); } }