mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 05:56:07 +00:00
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`.
This commit is contained in:
parent
06ee58eb8b
commit
f971f18a60
@ -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);
|
||||
|
@ -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, .{});
|
||||
|
@ -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(.{}){};
|
||||
|
@ -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
|
||||
|
@ -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{};
|
||||
|
@ -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)),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user