mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-15 06:26:07 +00:00
Demo showing global usage
This commit is contained in:
parent
f971f18a60
commit
275f1544d9
@ -16,7 +16,9 @@ pub fn build(b: *std.Build) !void {
|
|||||||
// -------------------
|
// -------------------
|
||||||
// const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target });
|
// const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target });
|
||||||
// exe.root_module.addImport("iguanas", iguanas_dep.module("iguanas"));
|
// 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()` ^
|
// ^ Add all dependencies before `jetzig.jetzigInit()` ^
|
||||||
|
|
||||||
try jetzig.jetzigInit(b, exe, .{});
|
try jetzig.jetzigInit(b, exe, .{});
|
||||||
|
@ -6,6 +6,10 @@
|
|||||||
.jetzig = .{
|
.jetzig = .{
|
||||||
.path = "../",
|
.path = "../",
|
||||||
},
|
},
|
||||||
|
.pg = .{
|
||||||
|
.url = "https://github.com/karlseguin/pg.zig/archive/d3ff7ab558b6ec1cc9b0de1606dbea364cf2d3b7.tar.gz",
|
||||||
|
.hash = "1220829e07672808e21d2b07544ed3567299b03d4fb694cd6dd0ece6c433b3a1309f",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
// This makes *all* files, recursively, included in this package. It is generally
|
// This makes *all* files, recursively, included in this package. It is generally
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const jetzig = @import("jetzig");
|
const jetzig = @import("jetzig");
|
||||||
|
const pg = @import("pg");
|
||||||
|
|
||||||
const importedFunction = @import("../lib/example.zig").exampleFunction;
|
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("custom_number", customFunction(100, 200, 300));
|
||||||
try root.put("imported_number", importedFunction(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");
|
try request.response.headers.append("x-example-header", "example header value");
|
||||||
|
|
||||||
return request.render(.ok);
|
return request.render(.ok);
|
||||||
|
@ -4,6 +4,9 @@ const builtin = @import("builtin");
|
|||||||
const jetzig = @import("jetzig");
|
const jetzig = @import("jetzig");
|
||||||
const zmd = @import("zmd");
|
const zmd = @import("zmd");
|
||||||
|
|
||||||
|
const pg = @import("pg");
|
||||||
|
pub const Global = pg.Pool;
|
||||||
|
|
||||||
pub const routes = @import("routes");
|
pub const routes = @import("routes");
|
||||||
pub const static = @import("static");
|
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;
|
const allocator = if (builtin.mode == .Debug) gpa.allocator() else std.heap.c_allocator;
|
||||||
defer if (builtin.mode == .Debug) std.debug.assert(gpa.deinit() == .ok);
|
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);
|
var app = try jetzig.init(allocator);
|
||||||
defer app.deinit();
|
defer app.deinit();
|
||||||
|
|
||||||
try app.start(routes, .{});
|
try app.start(routes, .{ .global = pool });
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user