2022-04-02 20:23:18 +00:00
|
|
|
const std = @import("std");
|
2022-04-23 07:11:40 +00:00
|
|
|
|
|
|
|
pub const db = @import("./db.zig");
|
|
|
|
pub const util = @import("./util.zig");
|
2022-04-23 07:25:45 +00:00
|
|
|
pub const http = @import("./http.zig");
|
2022-04-02 20:23:18 +00:00
|
|
|
|
2022-04-23 07:25:45 +00:00
|
|
|
pub const Uuid = util.Uuid;
|
|
|
|
pub const ciutf8 = util.ciutf8;
|
2022-04-02 20:23:18 +00:00
|
|
|
|
2022-04-23 07:25:45 +00:00
|
|
|
pub const io_mode = .evented;
|
2022-04-02 20:23:18 +00:00
|
|
|
|
2022-04-24 10:23:08 +00:00
|
|
|
const Route = http.Router.Route;
|
2022-04-02 20:23:18 +00:00
|
|
|
|
2022-04-23 07:25:45 +00:00
|
|
|
pub const routes = [_]Route{
|
2022-04-02 20:23:18 +00:00
|
|
|
Route.from(.GET, "/", staticString("Index Page")),
|
2022-04-04 03:36:32 +00:00
|
|
|
Route.from(.GET, "/abc", staticString("abc")),
|
|
|
|
Route.from(.GET, "/user/:id", getUser),
|
2022-05-13 01:49:26 +00:00
|
|
|
Route.from(.POST, "/note/", postNote),
|
2022-04-04 03:36:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const this_scheme = "http";
|
|
|
|
const this_host = "localhost:8080";
|
|
|
|
|
2022-05-13 01:49:26 +00:00
|
|
|
fn postNote(ctx: *http.Context, _: *const Route) anyerror!void {
|
2022-05-13 03:29:18 +00:00
|
|
|
const id = try db.createNote(.{
|
|
|
|
.author = Uuid.randV4(util.getRandom()),
|
2022-05-13 01:49:26 +00:00
|
|
|
.content = "test post",
|
|
|
|
});
|
2022-05-13 03:29:18 +00:00
|
|
|
|
|
|
|
var writer = try ctx.response.open(200);
|
|
|
|
try writer.writeAll("{\"id\":\"");
|
|
|
|
try writer.print("{}", .{id});
|
|
|
|
try writer.writeAll("\"}");
|
2022-05-13 01:49:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 10:23:08 +00:00
|
|
|
fn getUser(ctx: *http.Context, route: *const Route) anyerror!void {
|
2022-04-23 10:18:46 +00:00
|
|
|
const id_str = route.arg("id", ctx.request.path);
|
2022-04-04 03:36:32 +00:00
|
|
|
|
2022-04-21 09:34:04 +00:00
|
|
|
const host = ctx.request.headers.get("host") orelse {
|
2022-04-04 03:36:32 +00:00
|
|
|
try ctx.response.statusOnly(400);
|
|
|
|
return;
|
2022-04-21 09:34:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const id = Uuid.parse(id_str) catch {
|
|
|
|
try ctx.response.statusOnly(400);
|
|
|
|
return;
|
|
|
|
};
|
2022-04-04 03:36:32 +00:00
|
|
|
|
2022-04-21 06:26:08 +00:00
|
|
|
const actor = try db.getActorById(id);
|
|
|
|
|
2022-04-21 09:34:04 +00:00
|
|
|
if (actor == null or !std.mem.eql(u8, actor.?.host, host)) {
|
2022-04-04 03:36:32 +00:00
|
|
|
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\",");
|
2022-04-21 06:26:08 +00:00
|
|
|
try writer.print("\"id\":\"{s}://{s}/user/{}\",", .{ this_scheme, this_host, id });
|
|
|
|
try writer.print("\"preferredUsername\":\"{s}\"", .{actor.?.handle});
|
2022-04-04 03:36:32 +00:00
|
|
|
try writer.writeAll("}");
|
|
|
|
}
|
|
|
|
|
2022-04-02 20:23:18 +00:00
|
|
|
fn staticString(comptime str: []const u8) Route.Handler {
|
|
|
|
return (struct {
|
2022-04-24 10:23:08 +00:00
|
|
|
fn func(ctx: *http.Context, _: *const Route) anyerror!void {
|
2022-04-04 03:36:32 +00:00
|
|
|
try ctx.response.headers.put("Content-Type", "text/plain");
|
2022-04-02 20:23:18 +00:00
|
|
|
try ctx.response.write(200, str);
|
|
|
|
}
|
|
|
|
}).func;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() anyerror!void {
|
|
|
|
var srv = std.net.StreamServer.init(.{ .reuse_address = true });
|
|
|
|
defer srv.deinit();
|
|
|
|
|
2022-04-21 09:34:04 +00:00
|
|
|
const uuid = try Uuid.parse("f75f5160-12d3-42c2-a81d-ad2245b7a74b");
|
|
|
|
std.log.debug("{}", .{uuid});
|
|
|
|
|
2022-04-02 20:23:18 +00:00
|
|
|
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
|
2022-04-23 07:25:45 +00:00
|
|
|
_ = async http.handleConnection(conn);
|
2022-04-02 20:23:18 +00:00
|
|
|
}
|
|
|
|
}
|