From 275f1544d9dedabd9abb739f866430fba496287e Mon Sep 17 00:00:00 2001 From: Bob Farrell Date: Mon, 28 Oct 2024 09:10:59 +0000 Subject: [PATCH] Demo showing global usage --- demo/build.zig | 4 +++- demo/build.zig.zon | 4 ++++ demo/src/app/views/root.zig | 5 +++++ demo/src/main.zig | 16 +++++++++++++++- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/demo/build.zig b/demo/build.zig index 4df8eac..333b645 100644 --- a/demo/build.zig +++ b/demo/build.zig @@ -16,7 +16,9 @@ pub fn build(b: *std.Build) !void { // ------------------- // const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target }); // exe.root_module.addImport("iguanas", iguanas_dep.module("iguanas")); - // + const pg_dep = b.dependency("pg", .{ .optimize = optimize, .target = target }); + exe.root_module.addImport("pg", pg_dep.module("pg")); + // ^ Add all dependencies before `jetzig.jetzigInit()` ^ try jetzig.jetzigInit(b, exe, .{}); diff --git a/demo/build.zig.zon b/demo/build.zig.zon index 321158c..ae3082c 100644 --- a/demo/build.zig.zon +++ b/demo/build.zig.zon @@ -6,6 +6,10 @@ .jetzig = .{ .path = "../", }, + .pg = .{ + .url = "https://github.com/karlseguin/pg.zig/archive/d3ff7ab558b6ec1cc9b0de1606dbea364cf2d3b7.tar.gz", + .hash = "1220829e07672808e21d2b07544ed3567299b03d4fb694cd6dd0ece6c433b3a1309f", + }, }, .paths = .{ // This makes *all* files, recursively, included in this package. It is generally diff --git a/demo/src/app/views/root.zig b/demo/src/app/views/root.zig index f868587..4d920e5 100644 --- a/demo/src/app/views/root.zig +++ b/demo/src/app/views/root.zig @@ -1,5 +1,6 @@ const std = @import("std"); const jetzig = @import("jetzig"); +const pg = @import("pg"); const importedFunction = @import("../lib/example.zig").exampleFunction; @@ -11,6 +12,10 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View { try root.put("custom_number", customFunction(100, 200, 300)); try root.put("imported_number", importedFunction(100, 200, 300)); + var conn = try request.global.acquire(); + defer conn.deinit(); + std.debug.print("connection acquired: {any}\n", .{conn}); + try request.response.headers.append("x-example-header", "example header value"); return request.render(.ok); diff --git a/demo/src/main.zig b/demo/src/main.zig index 06fb87d..39d33f9 100644 --- a/demo/src/main.zig +++ b/demo/src/main.zig @@ -4,6 +4,9 @@ const builtin = @import("builtin"); const jetzig = @import("jetzig"); const zmd = @import("zmd"); +const pg = @import("pg"); +pub const Global = pg.Pool; + pub const routes = @import("routes"); pub const static = @import("static"); @@ -197,8 +200,19 @@ pub fn main() !void { const allocator = if (builtin.mode == .Debug) gpa.allocator() else std.heap.c_allocator; defer if (builtin.mode == .Debug) std.debug.assert(gpa.deinit() == .ok); + var pool = try pg.Pool.init(allocator, .{ + .size = 2, + .connect = .{ .host = "localhost", .port = 5432 }, + .auth = .{ + .username = "postgres", + .password = "password", + .database = "postgres", + }, + }); + defer pool.deinit(); + var app = try jetzig.init(allocator); defer app.deinit(); - try app.start(routes, .{}); + try app.start(routes, .{ .global = pool }); }