mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 22:16:08 +00:00
WIP
This commit is contained in:
parent
7fe559cfae
commit
3a5c2f888a
@ -203,6 +203,14 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
|
||||
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);
|
||||
// TODO: Prevent failure if schema file not present.
|
||||
const schema_module = b.createModule(.{ .root_source_file = b.path("src/app/database/Schema.zig") });
|
||||
schema_module.addImport("jetzig", jetzig_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("zmpl", zmpl_module);
|
||||
exe_static_routes.root_module.addImport("Schema", schema_module);
|
||||
|
||||
const markdown_fragments_write_files = b.addWriteFiles();
|
||||
const path = markdown_fragments_write_files.add("markdown_fragments.zig", try generateMarkdownFragments(b));
|
||||
|
@ -7,8 +7,8 @@
|
||||
.hash = "1220d0e8734628fd910a73146e804d10a3269e3e7d065de6bb0e3e88d5ba234eb163",
|
||||
},
|
||||
.zmpl = .{
|
||||
.url = "https://github.com/jetzig-framework/zmpl/archive/bf903d8e3decc1124aa90ec4e812e41e2aea382c.tar.gz",
|
||||
.hash = "12209a9d6f652ce712448da1bd41517cceef8eebf86884c1bb29c02fc3a933713afd",
|
||||
.url = "https://github.com/jetzig-framework/zmpl/archive/a88a06cf43ece8b303a87f52d611b874ad06af9c.tar.gz",
|
||||
.hash = "12202af84e839619429496900c3280fbfb7c3a3ce80ac0c2e70b99080c480fb22aa3",
|
||||
},
|
||||
.jetkv = .{
|
||||
.url = "https://github.com/jetzig-framework/jetkv/archive/2b1130a48979ea2871c8cf6ca89c38b1e7062839.tar.gz",
|
||||
|
@ -8,6 +8,12 @@ pub const Global = if (@hasDecl(@import("main"), "Global"))
|
||||
else
|
||||
jetzig.DefaultGlobal;
|
||||
|
||||
pub const database_lazy_connect = true;
|
||||
|
||||
pub const jetzig_options = struct {
|
||||
pub const Schema = @import("Schema");
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer std.debug.assert(gpa.deinit() == .ok);
|
||||
|
@ -11,6 +11,7 @@ env: jetzig.Environment,
|
||||
allocator: std.mem.Allocator,
|
||||
custom_routes: std.ArrayList(jetzig.views.Route),
|
||||
initHook: ?*const fn (*App) anyerror!void,
|
||||
server: *jetzig.http.Server = undefined,
|
||||
|
||||
pub fn deinit(self: *const App) void {
|
||||
@constCast(self).custom_routes.deinit();
|
||||
@ -72,6 +73,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
|
||||
var repo = try jetzig.database.repo(
|
||||
self.allocator,
|
||||
jetzig.config.get(?jetzig.database.DatabaseOptions, "database"),
|
||||
self,
|
||||
);
|
||||
defer repo.deinit();
|
||||
|
||||
@ -104,6 +106,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
|
||||
&repo,
|
||||
options.global,
|
||||
);
|
||||
@constCast(self).server = &server;
|
||||
|
||||
var mutex = std.Thread.Mutex{};
|
||||
var worker_pool = jetzig.jobs.Pool.init(
|
||||
|
@ -103,14 +103,6 @@ pub const database: ?db.DatabaseOptions = null;
|
||||
/// Database Schema.
|
||||
pub const Schema: type = struct {
|
||||
pub const _null = struct {}; // https://github.com/ziglang/zig/pull/21331
|
||||
pub const Blogs = struct {
|
||||
pub const table_name = "blogs";
|
||||
pub const Definition = struct {
|
||||
id: []const u8,
|
||||
title: []const u8,
|
||||
content: []const u8,
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
/// Key-value store options. Set backend to `.file` to use a file-based store.
|
||||
|
@ -13,12 +13,21 @@ pub const DatabaseOptions = struct {
|
||||
|
||||
pub const Schema = jetzig.get(type, "Schema");
|
||||
|
||||
pub fn repo(allocator: std.mem.Allocator, maybe_options: ?DatabaseOptions) !jetzig.jetquery.Repo {
|
||||
pub fn repo(allocator: std.mem.Allocator, maybe_options: ?DatabaseOptions, app: *const jetzig.App) !jetzig.jetquery.Repo {
|
||||
const options = maybe_options orelse return try jetzig.jetquery.Repo.init(
|
||||
allocator,
|
||||
.{ .adapter = .null },
|
||||
);
|
||||
|
||||
// XXX: Is this terrible ?
|
||||
const Callback = struct {
|
||||
var jetzig_app: *const jetzig.App = undefined;
|
||||
pub fn callbackFn(event: jetzig.jetquery.events.Event) !void {
|
||||
try eventCallback(event, jetzig_app);
|
||||
}
|
||||
};
|
||||
Callback.jetzig_app = app;
|
||||
|
||||
return switch (options.adapter) {
|
||||
.postgresql => try jetzig.jetquery.Repo.init(
|
||||
allocator,
|
||||
@ -30,9 +39,15 @@ pub fn repo(allocator: std.mem.Allocator, maybe_options: ?DatabaseOptions) !jetz
|
||||
.username = options.username,
|
||||
.password = options.password,
|
||||
.database = options.database,
|
||||
.lazy_connect = @hasField(@import("root"), "database_lazy_connect"),
|
||||
},
|
||||
},
|
||||
.eventCallback = Callback.callbackFn,
|
||||
},
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
fn eventCallback(event: jetzig.jetquery.events.Event, app: *const jetzig.App) !void {
|
||||
try app.server.logger.INFO("[database] {?s}", .{event.sql});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user