From f971f18a605f477b73dd9d81bea093d345016251 Mon Sep 17 00:00:00 2001 From: Bob Farrell Date: Mon, 28 Oct 2024 09:07:06 +0000 Subject: [PATCH] Global data Define `pub const Global = SomeType` at top level in `src/main.zig`, then create a pointer to `SomeType` and pass to `app.start`: ``` app.start(routes, .{ .global = global }); ``` Then access in a view as `request.global`. --- build.zig | 4 ++++ demo/build.zig | 2 +- src/compile_static_routes.zig | 5 ++++- src/jetzig.zig | 2 ++ src/jetzig/App.zig | 7 ++++--- src/jetzig/http/Request.zig | 5 +++++ src/jetzig/http/Server.zig | 3 +++ 7 files changed, 23 insertions(+), 5 deletions(-) diff --git a/build.zig b/build.zig index 0373498..8c0e6d7 100644 --- a/build.zig +++ b/build.zig @@ -190,9 +190,11 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn .optimize = optimize, }); + const main_module = b.createModule(.{ .root_source_file = b.path("src/main.zig") }); exe_static_routes.root_module.addImport("routes", routes_module); exe_static_routes.root_module.addImport("jetzig", jetzig_module); exe_static_routes.root_module.addImport("zmpl", zmpl_module); + exe_static_routes.root_module.addImport("main", main_module); const markdown_fragments_write_files = b.addWriteFiles(); const path = markdown_fragments_write_files.add("markdown_fragments.zig", try generateMarkdownFragments(b)); @@ -233,6 +235,7 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn routes_module.addImport(import.key_ptr.*, import.value_ptr.*); exe_static_routes.root_module.addImport(import.key_ptr.*, import.value_ptr.*); exe_unit_tests.root_module.addImport(import.key_ptr.*, import.value_ptr.*); + main_module.addImport(import.key_ptr.*, import.value_ptr.*); } if (exe.root_module.link_libc == true) { @@ -243,6 +246,7 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn for (exe.root_module.link_objects.items) |link_object| { try exe_static_routes.root_module.link_objects.append(b.allocator, link_object); try exe_unit_tests.root_module.link_objects.append(b.allocator, link_object); + try main_module.link_objects.append(b.allocator, link_object); } const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); diff --git a/demo/build.zig b/demo/build.zig index 56ee763..4df8eac 100644 --- a/demo/build.zig +++ b/demo/build.zig @@ -16,7 +16,7 @@ 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")); - + // // ^ Add all dependencies before `jetzig.jetzigInit()` ^ try jetzig.jetzigInit(b, exe, .{}); diff --git a/src/compile_static_routes.zig b/src/compile_static_routes.zig index ade3e3b..323d244 100644 --- a/src/compile_static_routes.zig +++ b/src/compile_static_routes.zig @@ -3,7 +3,10 @@ const jetzig = @import("jetzig"); const routes = @import("routes").routes; const zmpl = @import("zmpl"); const markdown_fragments = @import("markdown_fragments"); -// const jetzig_options = @import("jetzig_app").jetzig_options; +pub const Global = if (@hasDecl(@import("main"), "Global")) + @import("main").Global +else + jetzig.DefaultGlobal; pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; diff --git a/src/jetzig.zig b/src/jetzig.zig index 779b3e2..bab48f5 100644 --- a/src/jetzig.zig +++ b/src/jetzig.zig @@ -65,6 +65,8 @@ pub const MailerDefinition = mail.MailerDefinition; pub const Logger = loggers.Logger; pub const root = @import("root"); +pub const Global = if (@hasDecl(root, "Global")) root.Global else DefaultGlobal; +pub const DefaultGlobal = struct { __jetzig_default: bool }; /// Global configuration. Override these values by defining in `src/main.zig` with: /// ```zig diff --git a/src/jetzig/App.zig b/src/jetzig/App.zig index 3e9f36a..403ea2e 100644 --- a/src/jetzig/App.zig +++ b/src/jetzig/App.zig @@ -18,14 +18,14 @@ pub fn deinit(self: *const App) void { // Not used yet, but allows us to add new options to `start()` without breaking // backward-compatibility. -const AppOptions = struct {}; +const AppOptions = struct { + global: *anyopaque = undefined, +}; /// Starts an application. `routes` should be `@import("routes").routes`, a generated file /// automatically created at build time. `templates` should be /// `@import("src/app/views/zmpl.manifest.zig").templates`, created by Zmpl at compile time. pub fn start(self: *const App, routes_module: type, options: AppOptions) !void { - _ = options; // See `AppOptions` - defer self.env.deinit(); if (self.initHook) |hook| try hook(@constCast(self)); @@ -96,6 +96,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void { &store, &job_queue, &cache, + options.global, ); var mutex = std.Thread.Mutex{}; diff --git a/src/jetzig/http/Request.zig b/src/jetzig/http/Request.zig index be7bfa9..72c2968 100644 --- a/src/jetzig/http/Request.zig +++ b/src/jetzig/http/Request.zig @@ -43,6 +43,7 @@ rendered_view: ?jetzig.views.View = null, start_time: i128, store: RequestStore, cache: RequestStore, +global: *jetzig.Global, /// Wrapper for KV store that uses the request's arena allocator for fetching values. pub const RequestStore = struct { @@ -130,6 +131,10 @@ pub fn init( .start_time = start_time, .store = .{ .store = server.store, .allocator = allocator }, .cache = .{ .store = server.cache, .allocator = allocator }, + .global = if (@hasField(jetzig.Global, "__jetzig_default")) + undefined + else + @ptrCast(@alignCast(server.global)), }; } diff --git a/src/jetzig/http/Server.zig b/src/jetzig/http/Server.zig index 98a7fbc..04b9e9a 100644 --- a/src/jetzig/http/Server.zig +++ b/src/jetzig/http/Server.zig @@ -19,6 +19,7 @@ initialized: bool = false, store: *jetzig.kv.Store, job_queue: *jetzig.kv.Store, cache: *jetzig.kv.Store, +global: *anyopaque, decoded_static_route_params: []*jetzig.data.Value = &.{}, const Server = @This(); @@ -34,6 +35,7 @@ pub fn init( store: *jetzig.kv.Store, job_queue: *jetzig.kv.Store, cache: *jetzig.kv.Store, + global: *anyopaque, ) Server { return .{ .allocator = allocator, @@ -47,6 +49,7 @@ pub fn init( .store = store, .job_queue = job_queue, .cache = cache, + .global = global, }; }