fediglam/src/main/main.zig

102 lines
3.3 KiB
Zig
Raw Normal View History

2022-04-02 20:23:18 +00:00
const std = @import("std");
2022-07-10 22:19:21 +00:00
const builtin = @import("builtin");
2022-07-09 22:07:51 +00:00
const http = @import("http");
2022-07-13 04:16:33 +00:00
const util = @import("util");
2022-07-13 03:40:48 +00:00
const api = @import("./api.zig");
2022-09-05 09:15:16 +00:00
const models = @import("./db/models.zig");
2022-07-13 04:16:33 +00:00
const Uuid = util.Uuid;
2022-07-13 07:57:21 +00:00
const c = @import("./controllers.zig");
2022-07-10 05:05:01 +00:00
// this thing is overcomplicated and weird. stop this
const Router = http.Router(*RequestServer);
const Route = Router.Route;
const RouteArgs = http.RouteArgs;
const router = Router{
.routes = &[_]Route{
2022-09-05 10:33:54 +00:00
Route.new(.GET, "/healthcheck", &c.healthcheck),
2022-07-16 18:41:09 +00:00
2022-09-05 08:52:49 +00:00
//Route.new(.POST, "/users", c.users.create),
2022-09-05 07:10:03 +00:00
//Route.new(.POST, "/auth/register", &c.auth.register),
2022-09-05 08:52:49 +00:00
Route.new(.POST, "/login", &c.auth.login),
2022-09-05 10:33:54 +00:00
Route.new(.GET, "/login", &c.auth.verifyLogin),
2022-07-17 23:21:03 +00:00
2022-09-07 23:14:52 +00:00
Route.new(.POST, "/communities", &c.admin.communities.create),
//Route.new(.POST, "/invites", &c.admin.invites.create),
2022-09-05 07:10:03 +00:00
//Route.new(.POST, "/notes", &c.notes.create),
//Route.new(.GET, "/notes/:id", &c.notes.get),
2022-07-21 05:26:13 +00:00
2022-09-05 07:10:03 +00:00
//Route.new(.GET, "/notes/:id/reacts", &c.notes.reacts.list),
//Route.new(.POST, "/notes/:id/reacts", &c.notes.reacts.create),
2022-07-22 06:53:05 +00:00
2022-09-05 07:10:03 +00:00
//Route.new(.GET, "/actors/:id", &c.actors.get),
2022-07-27 05:02:09 +00:00
2022-09-05 07:10:03 +00:00
//Route.new(.GET, "/admin/invites/:id", &c.admin.invites.get),
2022-08-02 04:33:23 +00:00
2022-09-05 07:10:03 +00:00
//Route.new(.GET, "/admin/communities/:host", &c.admin.communities.get),
2022-07-10 05:05:01 +00:00
},
};
2022-07-13 07:57:21 +00:00
pub const RequestServer = struct {
2022-07-10 05:05:01 +00:00
alloc: std.mem.Allocator,
2022-07-26 02:07:05 +00:00
api: api.ApiSource,
2022-07-30 06:14:42 +00:00
config: Config,
2022-07-10 05:05:01 +00:00
2022-07-30 06:14:42 +00:00
fn init(alloc: std.mem.Allocator, config: Config) !RequestServer {
2022-07-10 05:05:01 +00:00
return RequestServer{
.alloc = alloc,
2022-07-30 06:14:42 +00:00
.api = try api.ApiSource.init(alloc, config),
.config = config,
2022-07-10 05:05:01 +00:00
};
}
2022-07-18 07:37:10 +00:00
fn listenAndRun(self: *RequestServer, addr: std.net.Address) !void {
var srv = http.Server.listen(addr) catch unreachable;
defer srv.shutdown();
2022-04-04 03:36:32 +00:00
while (true) {
2022-07-21 05:26:13 +00:00
const buf = try self.alloc.alloc(u8, 1 << 28); // 4mb
defer self.alloc.free(buf);
var fba = std.heap.FixedBufferAllocator.init(buf);
const alloc = fba.allocator();
2022-04-02 20:23:18 +00:00
2022-07-18 07:37:10 +00:00
var ctx = try srv.accept(alloc);
defer ctx.close();
2022-04-21 09:34:04 +00:00
2022-07-11 00:48:54 +00:00
router.dispatch(self, &ctx, ctx.request.method, ctx.request.path) catch |err| switch (err) {
2022-07-13 07:57:21 +00:00
error.NotFound, error.RouteNotApplicable => c.notFound(self, &ctx),
2022-08-02 06:24:16 +00:00
else => {
2022-09-07 23:14:52 +00:00
std.log.err("Unhandled error in controller ({s}): {}\nStack Trace\n{?}", .{ ctx.request.path, err, @errorReturnTrace() });
2022-08-02 06:24:16 +00:00
c.internalServerError(self, &ctx);
},
2022-07-11 00:48:54 +00:00
};
}
}
};
2022-07-30 06:14:42 +00:00
pub const Config = struct {
2022-09-05 08:52:49 +00:00
cluster_host: []const u8,
2022-07-30 06:14:42 +00:00
};
fn loadConfig(alloc: std.mem.Allocator) !Config {
var file = try std.fs.cwd().openFile("config.json", .{});
defer file.close();
const bytes = try file.reader().readAllAlloc(alloc, 1 << 63);
defer alloc.free(bytes);
var ts = std.json.TokenStream.init(bytes);
return std.json.parse(Config, &ts, .{ .allocator = alloc });
}
pub fn main() anyerror!void {
2022-07-10 05:05:01 +00:00
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
2022-07-30 06:14:42 +00:00
const cfg = try loadConfig(gpa.allocator());
var srv = try RequestServer.init(gpa.allocator(), cfg);
2022-07-25 00:18:25 +00:00
api.initThreadPrng(@bitCast(u64, std.time.milliTimestamp()));
2022-07-18 07:37:10 +00:00
return srv.listenAndRun(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable);
2022-05-20 04:46:39 +00:00
}