mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-15 06:26:07 +00:00
WIP
This commit is contained in:
parent
4707fdf744
commit
3f0731baa4
@ -52,6 +52,7 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.jetquery_migrations_path = @as([]const u8, "src/app/database/migrations"),
|
.jetquery_migrations_path = @as([]const u8, "src/app/database/migrations"),
|
||||||
|
.jetquery_config_path = @as([]const u8, "config/database.zig"),
|
||||||
});
|
});
|
||||||
const zmd_dep = b.dependency("zmd", .{ .target = target, .optimize = optimize });
|
const zmd_dep = b.dependency("zmd", .{ .target = target, .optimize = optimize });
|
||||||
const httpz_dep = b.dependency("httpz", .{ .target = target, .optimize = optimize });
|
const httpz_dep = b.dependency("httpz", .{ .target = target, .optimize = optimize });
|
||||||
|
@ -14,21 +14,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = arena.allocator();
|
const allocator = arena.allocator();
|
||||||
|
|
||||||
// FIXME: Load config from app
|
var repo = try jetquery.Repo.loadConfig(allocator, .{});
|
||||||
var repo = try jetquery.Repo.init(
|
|
||||||
allocator,
|
|
||||||
.{
|
|
||||||
.adapter = .{
|
|
||||||
.postgresql = .{
|
|
||||||
.database = "jetzig_website",
|
|
||||||
.username = "postgres",
|
|
||||||
.hostname = "127.0.0.1",
|
|
||||||
.password = "password",
|
|
||||||
.port = 5432,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
|
||||||
defer repo.deinit();
|
defer repo.deinit();
|
||||||
|
|
||||||
const migrate = Migrate.init(&repo);
|
const migrate = Migrate.init(&repo);
|
||||||
|
@ -70,11 +70,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
|
|||||||
);
|
);
|
||||||
defer log_thread.join();
|
defer log_thread.join();
|
||||||
|
|
||||||
var repo = try jetzig.database.repo(
|
var repo = try jetzig.database.repo(self.allocator, self);
|
||||||
self.allocator,
|
|
||||||
jetzig.config.get(?jetzig.database.DatabaseOptions, "database"),
|
|
||||||
self,
|
|
||||||
);
|
|
||||||
defer repo.deinit();
|
defer repo.deinit();
|
||||||
|
|
||||||
if (self.env.detach) {
|
if (self.env.detach) {
|
||||||
|
@ -151,6 +151,15 @@ pub fn init(allocator: std.mem.Allocator) !Environment {
|
|||||||
std.process.exit(1);
|
std.process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (jetzig.jetquery.adapter == .null) {
|
||||||
|
try logger.WARN("No database configured in `config/database.zig`. Database operations are not available.", .{});
|
||||||
|
} else {
|
||||||
|
try logger.INFO(
|
||||||
|
"Using `{s}` database adapter with database: `{s}`.",
|
||||||
|
.{ @tagName(jetzig.jetquery.adapter), jetzig.jetquery.config.database.database },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.logger = logger,
|
.logger = logger,
|
||||||
|
@ -13,12 +13,11 @@ pub const DatabaseOptions = struct {
|
|||||||
|
|
||||||
pub const Schema = jetzig.get(type, "Schema");
|
pub const Schema = jetzig.get(type, "Schema");
|
||||||
|
|
||||||
pub fn repo(allocator: std.mem.Allocator, maybe_options: ?DatabaseOptions, app: *const jetzig.App) !jetzig.jetquery.Repo {
|
pub fn Query(comptime table: std.meta.DeclEnum(jetzig.config.get(type, "Schema"))) type {
|
||||||
const options = maybe_options orelse return try jetzig.jetquery.Repo.init(
|
return jetzig.jetquery.Query(@field(jetzig.config.get(type, "Schema"), @tagName(table)));
|
||||||
allocator,
|
}
|
||||||
.{ .adapter = .null },
|
|
||||||
);
|
|
||||||
|
|
||||||
|
pub fn repo(allocator: std.mem.Allocator, app: *const jetzig.App) !jetzig.jetquery.Repo {
|
||||||
// XXX: Is this terrible ?
|
// XXX: Is this terrible ?
|
||||||
const Callback = struct {
|
const Callback = struct {
|
||||||
var jetzig_app: *const jetzig.App = undefined;
|
var jetzig_app: *const jetzig.App = undefined;
|
||||||
@ -28,24 +27,10 @@ pub fn repo(allocator: std.mem.Allocator, maybe_options: ?DatabaseOptions, app:
|
|||||||
};
|
};
|
||||||
Callback.jetzig_app = app;
|
Callback.jetzig_app = app;
|
||||||
|
|
||||||
return switch (options.adapter) {
|
return try jetzig.jetquery.Repo.loadConfig(
|
||||||
.postgresql => try jetzig.jetquery.Repo.init(
|
allocator,
|
||||||
allocator,
|
.{ .eventCallback = Callback.callbackFn, .lazy_connect = true },
|
||||||
.{
|
);
|
||||||
.adapter = .{
|
|
||||||
.postgresql = .{
|
|
||||||
.hostname = options.hostname,
|
|
||||||
.port = options.port,
|
|
||||||
.username = options.username,
|
|
||||||
.password = options.password,
|
|
||||||
.database = options.database,
|
|
||||||
.lazy_connect = true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
.eventCallback = Callback.callbackFn,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eventCallback(event: jetzig.jetquery.events.Event, app: *const jetzig.App) !void {
|
fn eventCallback(event: jetzig.jetquery.events.Event, app: *const jetzig.App) !void {
|
||||||
|
@ -520,15 +520,6 @@ pub fn mail(self: *Request, name: []const u8, mail_params: jetzig.mail.MailParam
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn query(
|
|
||||||
self: *const Request,
|
|
||||||
comptime table: std.meta.DeclEnum(jetzig.config.get(type, "Schema")),
|
|
||||||
) jetzig.jetquery.Query(@field(jetzig.config.get(type, "Schema"), @tagName(table))) {
|
|
||||||
return jetzig.jetquery.Query(
|
|
||||||
@field(jetzig.config.get(type, "Schema"), @tagName(table)),
|
|
||||||
).init(self.allocator);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn extensionFormat(self: *const Request) ?jetzig.http.Request.Format {
|
fn extensionFormat(self: *const Request) ?jetzig.http.Request.Format {
|
||||||
const extension = self.path.extension orelse return null;
|
const extension = self.path.extension orelse return null;
|
||||||
if (std.mem.eql(u8, extension, ".html")) {
|
if (std.mem.eql(u8, extension, ".html")) {
|
||||||
|
@ -324,7 +324,9 @@ fn renderView(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try self.logger.WARN("`request.render` was not invoked. Rendering empty content.", .{});
|
if (!request.redirected) {
|
||||||
|
try self.logger.WARN("`request.render` was not invoked. Rendering empty content.", .{});
|
||||||
|
}
|
||||||
request.response_data.reset();
|
request.response_data.reset();
|
||||||
return .{
|
return .{
|
||||||
.view = .{ .data = request.response_data, .status_code = .no_content },
|
.view = .{ .data = request.response_data, .status_code = .no_content },
|
||||||
|
Loading…
x
Reference in New Issue
Block a user