Demo showing global usage

This commit is contained in:
Bob Farrell 2024-10-28 09:10:59 +00:00
parent f971f18a60
commit 275f1544d9
4 changed files with 27 additions and 2 deletions

View File

@ -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, .{});

View File

@ -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

View File

@ -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);

View File

@ -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 });
}