const std = @import("std"); const builtin = @import("builtin"); const sql = @import("sql"); const http = @import("http"); const util = @import("util"); pub const api = @import("./api.zig"); const models = @import("./db/models.zig"); const Uuid = util.Uuid; const c = @import("./controllers.zig"); // 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{ Route.new(.GET, "/healthcheck", &c.healthcheck), prepare(c.auth.login), prepare(c.auth.verify_login), prepare(c.communities.create), prepare(c.invites.create), prepare(c.users.create), prepare(c.notes.create), prepare(c.notes.get), //Route.new(.GET, "/notes/:id/reacts", &c.notes.reacts.list), //Route.new(.POST, "/notes/:id/reacts", &c.notes.reacts.create), //Route.new(.GET, "/actors/:id", &c.actors.get), //Route.new(.GET, "/admin/invites/:id", &c.admin.invites.get), //Route.new(.GET, "/admin/communities/:host", &c.admin.communities.get), }, }; fn prepare(comptime route_desc: type) Route { return Route.new(route_desc.method, route_desc.path, &route_desc.handler); } pub const RequestServer = struct { alloc: std.mem.Allocator, api: *api.ApiSource, config: Config, fn init(alloc: std.mem.Allocator, src: *api.ApiSource, config: Config) !RequestServer { return RequestServer{ .alloc = alloc, .api = src, .config = config, }; } fn listenAndRun(self: *RequestServer, addr: std.net.Address) !void { var srv = http.Server.listen(addr) catch unreachable; defer srv.shutdown(); while (true) { 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(); var ctx = try srv.accept(alloc); defer ctx.close(); router.dispatch(self, &ctx, ctx.request.method, ctx.request.path) catch |err| switch (err) { error.NotFound, error.RouteNotApplicable => c.notFound(self, &ctx), else => { std.log.err("Unhandled error in controller ({s}): {}\nStack Trace\n{?}", .{ ctx.request.path, err, @errorReturnTrace() }); c.internalServerError(self, &ctx); }, }; } } }; pub const Config = struct { cluster_host: []const u8, db: sql.Config, root_password: ?[]const u8 = null, }; 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 }); } const root_password_envvar = "CLUSTER_ROOT_PASSWORD"; pub fn main() anyerror!void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var cfg = try loadConfig(gpa.allocator()); var db_conn = try sql.Db.open(cfg.db); var api_src = api.ApiSource.init(gpa.allocator(), cfg, std.os.getenv(root_password_envvar), db_conn) catch |err| switch (err) { error.NeedRootPassword => { std.log.err( "No root user created and no password specified. Please provide the password for the root user by the ${s} environment variable for initial startup. This only needs to be done once", .{root_password_envvar}, ); return err; }, else => return err, }; var srv = try RequestServer.init(gpa.allocator(), &api_src, cfg); api.initThreadPrng(@bitCast(u64, std.time.milliTimestamp())); return srv.listenAndRun(std.net.Address.parseIp("0.0.0.0", 8080) catch unreachable); }