diff options
-rw-r--r-- | comments/build.zig.zon | 6 | ||||
-rw-r--r-- | comments/src/main.zig | 44 |
2 files changed, 4 insertions, 46 deletions
diff --git a/comments/build.zig.zon b/comments/build.zig.zon index 88bc50c..26ff1eb 100644 --- a/comments/build.zig.zon +++ b/comments/build.zig.zon @@ -2,9 +2,9 @@ .name = "comments", .version = "0.0.1", .dependencies = .{ .pg = .{ - //.url = "https://github.com/karlseguin/pg.zig/archive/fe2965b7651462258bcdabbb9a5d3aa09a404746.tar.gz", - //.hash = "12208adc9d7072c98cdfe9dba33d72430e95be1763b6bc4b1091148b997a9281741b", - .path = "../../pg.zig" + .url = "https://github.com/MFAshby/pg.zig/archive/148cbe78ad8ed23b7d71ffab2e1e4537dd8a65e6.tar.gz", + .hash = "122093dd239eefdaf3112933a35bb55851e487dac392b8ad92e4c874aea21bd41244", + //.path = "../../pg.zig" }, .smtp = .{ .url = "https://code.mfashby.net/smtp-zig/snapshot/smtp-zig-main.tar.xz", .hash = "1220b42673da5f313d4ce816e41de2f17e4b7ebbae1669904b9c17a385d9997e72ed", 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{ |