diff options
Diffstat (limited to 'comments/src')
-rw-r--r-- | comments/src/main.zig | 44 |
1 files changed, 1 insertions, 43 deletions
diff --git a/comments/src/main.zig b/comments/src/main.zig index 161f250..c423b2b 100644 --- a/comments/src/main.zig +++ b/comments/src/main.zig @@ -139,56 +139,14 @@ const router = Rtr{ .notfound = notfound, }; -const UriParams = struct { - connOpts: pg.Conn.Opts, - authOpts: pg.Conn.AuthOpts, - aa: std.heap.ArenaAllocator, - pub fn deinit(self: *UriParams) void { - self.aa.deinit(); - } -}; - -fn toParams(uri: std.Uri, parent_a: std.mem.Allocator) !UriParams { - var aa = std.heap.ArenaAllocator.init(parent_a); - errdefer aa.deinit(); - const a = aa.allocator(); - const co: pg.Conn.Opts = .{ - .host = if (uri.host) |host| try host.toRawMaybeAlloc(a) else "localhost", - .port = uri.port orelse 5432, - }; - const ao: pg.Conn.AuthOpts = .{ - .username = if (uri.user) |user| try user.toRawMaybeAlloc(a) else "postgres", - .password = if (uri.password) |password| try password.toRawMaybeAlloc(a) else "", - .database = if (uri.path.isEmpty()) "postgres" else std.mem.trimLeft(u8, try uri.path.toRawMaybeAlloc(a), "/"), - // TODO startup parameters - }; - return .{ - .connOpts = co, - .authOpts = ao, - .aa = aa, - }; -} - /// Run as a CGI program! pub fn main() !void { const allocator = gpa.allocator(); const db_url = std.posix.getenv("DATABASE_URL") orelse "postgresql://comments@localhost/comments"; const uri = try std.Uri.parse(db_url); - var uriParams = try toParams(uri, allocator); - defer uriParams.deinit(); - var db = try pg.Conn.open(allocator, uriParams.connOpts); + var db = try pg.Conn.openAndAuthUri(allocator, uri); defer db.deinit(); - db.auth(uriParams.authOpts) catch |e| switch (e) { - error.PG => { - std.log.err("PG error authenticating {s}", .{db.err.?.message}); - return; - }, - else => { - std.log.err("Error authenticating! {}", .{e}); - return; - }, - }; // try db.exec(@embedFile("migrations/0_init.sql")); // try db.exec(@embedFile("migrations/1_capcha.sql")); const req = Request{ |