mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +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,
|
.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("routes", routes_module);
|
||||||
exe_static_routes.root_module.addImport("jetzig", jetzig_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("zmpl", zmpl_module);
|
||||||
|
exe_static_routes.root_module.addImport("main", main_module);
|
||||||
|
|
||||||
const markdown_fragments_write_files = b.addWriteFiles();
|
const markdown_fragments_write_files = b.addWriteFiles();
|
||||||
const path = markdown_fragments_write_files.add("markdown_fragments.zig", try generateMarkdownFragments(b));
|
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.*);
|
routes_module.addImport(import.key_ptr.*, import.value_ptr.*);
|
||||||
exe_static_routes.root_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.*);
|
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) {
|
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| {
|
for (exe.root_module.link_objects.items) |link_object| {
|
||||||
try exe_static_routes.root_module.link_objects.append(b.allocator, 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 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);
|
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 });
|
// 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"));
|
||||||
|
//
|
||||||
// ^ Add all dependencies before `jetzig.jetzigInit()` ^
|
// ^ Add all dependencies before `jetzig.jetzigInit()` ^
|
||||||
|
|
||||||
try jetzig.jetzigInit(b, exe, .{});
|
try jetzig.jetzigInit(b, exe, .{});
|
||||||
|
@ -3,7 +3,10 @@ const jetzig = @import("jetzig");
|
|||||||
const routes = @import("routes").routes;
|
const routes = @import("routes").routes;
|
||||||
const zmpl = @import("zmpl");
|
const zmpl = @import("zmpl");
|
||||||
const markdown_fragments = @import("markdown_fragments");
|
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 {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
@ -65,6 +65,8 @@ pub const MailerDefinition = mail.MailerDefinition;
|
|||||||
pub const Logger = loggers.Logger;
|
pub const Logger = loggers.Logger;
|
||||||
|
|
||||||
pub const root = @import("root");
|
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:
|
/// Global configuration. Override these values by defining in `src/main.zig` with:
|
||||||
/// ```zig
|
/// ```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
|
// Not used yet, but allows us to add new options to `start()` without breaking
|
||||||
// backward-compatibility.
|
// backward-compatibility.
|
||||||
const AppOptions = struct {};
|
const AppOptions = struct {
|
||||||
|
global: *anyopaque = undefined,
|
||||||
|
};
|
||||||
|
|
||||||
/// Starts an application. `routes` should be `@import("routes").routes`, a generated file
|
/// Starts an application. `routes` should be `@import("routes").routes`, a generated file
|
||||||
/// automatically created at build time. `templates` should be
|
/// automatically created at build time. `templates` should be
|
||||||
/// `@import("src/app/views/zmpl.manifest.zig").templates`, created by Zmpl at compile time.
|
/// `@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 {
|
pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
|
||||||
_ = options; // See `AppOptions`
|
|
||||||
|
|
||||||
defer self.env.deinit();
|
defer self.env.deinit();
|
||||||
|
|
||||||
if (self.initHook) |hook| try hook(@constCast(self));
|
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,
|
&store,
|
||||||
&job_queue,
|
&job_queue,
|
||||||
&cache,
|
&cache,
|
||||||
|
options.global,
|
||||||
);
|
);
|
||||||
|
|
||||||
var mutex = std.Thread.Mutex{};
|
var mutex = std.Thread.Mutex{};
|
||||||
|
@ -43,6 +43,7 @@ rendered_view: ?jetzig.views.View = null,
|
|||||||
start_time: i128,
|
start_time: i128,
|
||||||
store: RequestStore,
|
store: RequestStore,
|
||||||
cache: RequestStore,
|
cache: RequestStore,
|
||||||
|
global: *jetzig.Global,
|
||||||
|
|
||||||
/// Wrapper for KV store that uses the request's arena allocator for fetching values.
|
/// Wrapper for KV store that uses the request's arena allocator for fetching values.
|
||||||
pub const RequestStore = struct {
|
pub const RequestStore = struct {
|
||||||
@ -130,6 +131,10 @@ pub fn init(
|
|||||||
.start_time = start_time,
|
.start_time = start_time,
|
||||||
.store = .{ .store = server.store, .allocator = allocator },
|
.store = .{ .store = server.store, .allocator = allocator },
|
||||||
.cache = .{ .store = server.cache, .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,
|
store: *jetzig.kv.Store,
|
||||||
job_queue: *jetzig.kv.Store,
|
job_queue: *jetzig.kv.Store,
|
||||||
cache: *jetzig.kv.Store,
|
cache: *jetzig.kv.Store,
|
||||||
|
global: *anyopaque,
|
||||||
decoded_static_route_params: []*jetzig.data.Value = &.{},
|
decoded_static_route_params: []*jetzig.data.Value = &.{},
|
||||||
|
|
||||||
const Server = @This();
|
const Server = @This();
|
||||||
@ -34,6 +35,7 @@ pub fn init(
|
|||||||
store: *jetzig.kv.Store,
|
store: *jetzig.kv.Store,
|
||||||
job_queue: *jetzig.kv.Store,
|
job_queue: *jetzig.kv.Store,
|
||||||
cache: *jetzig.kv.Store,
|
cache: *jetzig.kv.Store,
|
||||||
|
global: *anyopaque,
|
||||||
) Server {
|
) Server {
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
@ -47,6 +49,7 @@ pub fn init(
|
|||||||
.store = store,
|
.store = store,
|
||||||
.job_queue = job_queue,
|
.job_queue = job_queue,
|
||||||
.cache = cache,
|
.cache = cache,
|
||||||
|
.global = global,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user