diff options
author | Martin Ashby <martin@ashbysoft.com> | 2024-07-20 22:40:39 +0100 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2024-07-20 22:40:39 +0100 |
commit | 8a0464bcf2582084bd765f67de6f283545af2c7d (patch) | |
tree | c8c92b8ab5e6ec7692c559d219ffd480b6e0a50b /comments/src | |
parent | 6257db275bd9bb5794effaa9f7c4f5d6995024ce (diff) | |
download | mfashby.net-8a0464bcf2582084bd765f67de6f283545af2c7d.tar.gz mfashby.net-8a0464bcf2582084bd765f67de6f283545af2c7d.tar.bz2 mfashby.net-8a0464bcf2582084bd765f67de6f283545af2c7d.tar.xz mfashby.net-8a0464bcf2582084bd765f67de6f283545af2c7d.zip |
Use forked pg.zig which has connection URI parsing facility
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{ |