Use Zig compiler

Default to not using LLVM for application compilation. This gives more
than 2x performance improvement for compilation stage.
This commit is contained in:
Bob Farrell 2024-12-25 13:27:35 +00:00
parent c0d6fb162b
commit 099a2a5349
14 changed files with 130 additions and 80 deletions

View File

@ -36,6 +36,7 @@ pub fn build(b: *std.Build) !void {
.{ .{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.use_llvm = b.option(bool, "use_llvm", "Use LLVM") orelse false,
.zmpl_templates_paths = templates_paths, .zmpl_templates_paths = templates_paths,
.zmpl_auto_build = false, .zmpl_auto_build = false,
.zmpl_markdown_fragments = try generateMarkdownFragments(b), .zmpl_markdown_fragments = try generateMarkdownFragments(b),
@ -46,6 +47,11 @@ pub fn build(b: *std.Build) !void {
}, },
); );
const zmpl_steps = zmpl_dep.builder.top_level_steps;
const zmpl_compile_step = zmpl_steps.get("compile").?;
const compile_step = b.step("compile", "Compile Zmpl templates");
compile_step.dependOn(&zmpl_compile_step.step);
const zmpl_module = zmpl_dep.module("zmpl"); const zmpl_module = zmpl_dep.module("zmpl");
const jetkv_dep = b.dependency("jetkv", .{ .target = target, .optimize = optimize }); const jetkv_dep = b.dependency("jetkv", .{ .target = target, .optimize = optimize });
@ -58,18 +64,15 @@ pub fn build(b: *std.Build) !void {
const jetcommon_dep = b.dependency("jetcommon", .{ .target = target, .optimize = optimize }); const jetcommon_dep = b.dependency("jetcommon", .{ .target = target, .optimize = optimize });
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 });
const pg_dep = b.dependency("pg", .{ .target = target, .optimize = optimize });
// This is the way to make it look nice in the zig build script // This is the way to make it look nice in the zig build script
// If we would do it the other way around, we would have to do // If we would do it the other way around, we would have to do
// b.dependency("jetzig",.{}).builder.dependency("zmpl",.{}).module("zmpl"); // b.dependency("jetzig",.{}).builder.dependency("zmpl",.{}).module("zmpl");
b.modules.put("zmpl", zmpl_dep.module("zmpl")) catch @panic("Out of memory"); b.modules.put("zmpl", zmpl_dep.module("zmpl")) catch @panic("Out of memory");
b.modules.put("zmd", zmd_dep.module("zmd")) catch @panic("Out of memory"); b.modules.put("zmd", zmd_dep.module("zmd")) catch @panic("Out of memory");
b.modules.put("pg", pg_dep.module("pg")) catch @panic("Out of memory");
b.modules.put("jetquery", jetquery_dep.module("jetquery")) catch @panic("Out of memory"); b.modules.put("jetquery", jetquery_dep.module("jetquery")) catch @panic("Out of memory");
b.modules.put("jetcommon", jetcommon_dep.module("jetcommon")) catch @panic("Out of memory"); b.modules.put("jetcommon", jetcommon_dep.module("jetcommon")) catch @panic("Out of memory");
b.modules.put("jetquery_migrate", jetquery_dep.module("jetquery_migrate")) catch @panic("Out of memory"); b.modules.put("jetquery_migrate", jetquery_dep.module("jetquery_migrate")) catch @panic("Out of memory");
jetquery_dep.module("jetquery").addImport("pg", pg_dep.module("pg"));
const smtp_client_dep = b.dependency("smtp_client", .{ const smtp_client_dep = b.dependency("smtp_client", .{
.target = target, .target = target,
@ -132,6 +135,8 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
const target = exe.root_module.resolved_target orelse @panic("Unable to detect compile target."); const target = exe.root_module.resolved_target orelse @panic("Unable to detect compile target.");
const optimize = exe.root_module.optimize orelse .Debug; const optimize = exe.root_module.optimize orelse .Debug;
exe.use_llvm = exe.use_llvm orelse (optimize != .Debug);
if (optimize != .Debug) exe.linkLibC(); if (optimize != .Debug) exe.linkLibC();
const environment = b.option( const environment = b.option(
@ -172,7 +177,6 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
const jetzig_module = jetzig_dep.module("jetzig"); const jetzig_module = jetzig_dep.module("jetzig");
const zmpl_module = jetzig_dep.module("zmpl"); const zmpl_module = jetzig_dep.module("zmpl");
const zmd_module = jetzig_dep.module("zmd"); const zmd_module = jetzig_dep.module("zmd");
const pg_module = jetzig_dep.module("pg");
const jetquery_module = jetzig_dep.module("jetquery"); const jetquery_module = jetzig_dep.module("jetquery");
const jetcommon_module = jetzig_dep.module("jetcommon"); const jetcommon_module = jetzig_dep.module("jetcommon");
const jetquery_migrate_module = jetzig_dep.module("jetquery_migrate"); const jetquery_migrate_module = jetzig_dep.module("jetquery_migrate");
@ -187,7 +191,6 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
exe.root_module.addImport("jetzig", jetzig_module); exe.root_module.addImport("jetzig", jetzig_module);
exe.root_module.addImport("zmpl", zmpl_module); exe.root_module.addImport("zmpl", zmpl_module);
exe.root_module.addImport("zmd", zmd_module); exe.root_module.addImport("zmd", zmd_module);
exe.root_module.addImport("pg", pg_module);
if (b.option(bool, "jetzig_runner", "Used internally by `jetzig server` command.")) |jetzig_runner| { if (b.option(bool, "jetzig_runner", "Used internally by `jetzig server` command.")) |jetzig_runner| {
if (jetzig_runner) { if (jetzig_runner) {
@ -197,7 +200,10 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
} }
} }
const root_path = b.build_root.path orelse try std.fs.cwd().realpathAlloc(b.allocator, "."); const root_path = if (b.build_root.path) |build_root_path|
try std.fs.path.join(b.allocator, &.{ build_root_path, "." })
else
try std.fs.cwd().realpathAlloc(b.allocator, ".");
const templates_path: []const u8 = try std.fs.path.join( const templates_path: []const u8 = try std.fs.path.join(
b.allocator, b.allocator,
&[_][]const u8{ root_path, "src", "app" }, &[_][]const u8{ root_path, "src", "app" },
@ -220,6 +226,7 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
.root_source_file = jetzig_dep.path("src/routes_file.zig"), .root_source_file = jetzig_dep.path("src/routes_file.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.use_llvm = exe.use_llvm,
}); });
exe_routes_file.root_module.addImport("jetzig", jetzig_module); exe_routes_file.root_module.addImport("jetzig", jetzig_module);
@ -228,6 +235,7 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
exe_routes_file.root_module.addImport("zmpl", zmpl_module); exe_routes_file.root_module.addImport("zmpl", zmpl_module);
const run_routes_file_cmd = b.addRunArtifact(exe_routes_file); const run_routes_file_cmd = b.addRunArtifact(exe_routes_file);
run_routes_file_cmd.has_side_effects = true;
const routes_file_path = run_routes_file_cmd.addOutputFileArg("routes.zig"); const routes_file_path = run_routes_file_cmd.addOutputFileArg("routes.zig");
run_routes_file_cmd.addArgs(&.{ run_routes_file_cmd.addArgs(&.{
root_path, root_path,
@ -247,6 +255,7 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
.root_source_file = jetzig_dep.path("src/compile_static_routes.zig"), .root_source_file = jetzig_dep.path("src/compile_static_routes.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.use_llvm = exe.use_llvm,
}); });
const main_module = b.createModule(.{ .root_source_file = b.path("src/main.zig") }); const main_module = b.createModule(.{ .root_source_file = b.path("src/main.zig") });
@ -355,6 +364,7 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
.root_source_file = jetzig_dep.path("src/commands/routes.zig"), .root_source_file = jetzig_dep.path("src/commands/routes.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.use_llvm = exe.use_llvm,
}); });
const auth_user_create_step = b.step("jetzig:auth:user:create", "List all routes in your app"); const auth_user_create_step = b.step("jetzig:auth:user:create", "List all routes in your app");
@ -363,6 +373,7 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
.root_source_file = jetzig_dep.path("src/commands/auth.zig"), .root_source_file = jetzig_dep.path("src/commands/auth.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.use_llvm = exe.use_llvm,
}); });
exe_auth.root_module.addImport("jetquery", jetquery_module); exe_auth.root_module.addImport("jetquery", jetquery_module);
exe_auth.root_module.addImport("jetzig", jetzig_module); exe_auth.root_module.addImport("jetzig", jetzig_module);
@ -384,6 +395,7 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
.root_source_file = jetzig_dep.path("src/commands/database.zig"), .root_source_file = jetzig_dep.path("src/commands/database.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.use_llvm = exe.use_llvm,
}); });
exe_database.root_module.addImport("jetquery", jetquery_module); exe_database.root_module.addImport("jetquery", jetquery_module);
exe_database.root_module.addImport("jetzig", jetzig_module); exe_database.root_module.addImport("jetzig", jetzig_module);

View File

@ -1,42 +1,39 @@
.{ .{
.name = "jetzig", .name = .jetzig,
.version = "0.0.0", .version = "0.0.0",
.fingerprint = 0x93ad8bfa2d209022,
.dependencies = .{ .dependencies = .{
.zmd = .{ .jetquery = .{
.url = "https://github.com/jetzig-framework/zmd/archive/d6c8aa9a9cde99674ccb096d8f94ed09cba8dab.tar.gz", .url = "https://github.com/jetzig-framework/jetquery/archive/795136c43f6d5bd216c136748bafd22892277725.tar.gz",
.hash = "1220d0e8734628fd910a73146e804d10a3269e3e7d065de6bb0e3e88d5ba234eb163", .hash = "jetquery-0.0.0-TNf3zqZFBgDS2CaIG4DvLwaiGeAxxbfAi-DnEnSKx-mf",
}, },
.jetkv = .{ .jetkv = .{
.url = "https://github.com/jetzig-framework/jetkv/archive/9d754e552e7569239a900ed9e0f313a0554ed2d3.tar.gz", .url = "https://github.com/jetzig-framework/jetkv/archive/9d754e552e7569239a900ed9e0f313a0554ed2d3.tar.gz",
.hash = "122013f8596bc615990fd7771c833cab4d2959ecac8d05c4f6c973aa46624e43afea", .hash = "122013f8596bc615990fd7771c833cab4d2959ecac8d05c4f6c973aa46624e43afea",
}, },
.args = .{
.url = "https://github.com/ikskuh/zig-args/archive/968258dc1b1230493d8f1677097c832a3d7e0bd8.tar.gz",
.hash = "1220bdedf1a993d852d8aebcd63922a8fb163fac37b9c6ff72d187b2847a4a3a4248",
},
.jetcommon = .{ .jetcommon = .{
.url = "https://github.com/jetzig-framework/jetcommon/archive/5be57d534b3d469f5570cd4b373b8d61032b1b8b.tar.gz", .url = "https://github.com/jetzig-framework/jetcommon/archive/fb4edc13759d87bfcd9b1f5fcefdf93f8c9c62dd.tar.gz",
.hash = "122079c6ceb28fa93163c2f95e2f175bb8f93f3075fa34af63045671ab7dd824e756", .hash = "jetcommon-0.1.0-jPY_DS1HAAAP8xp5HSWB_ZY7m9JEYUmm8adQFrse0lwB",
}, },
.pg = .{ .zmpl = .{
.url = "https://github.com/karlseguin/pg.zig/archive/0110cfdf387403a5a326115b5184861c4604d711.tar.gz", .url = "https://github.com/jetzig-framework/zmpl/archive/d34fd5aa69cc5f68dc73b1ef844f7cbdebdfc93e.tar.gz",
.hash = "12205019ce2bc2e08c76352ea37a14600d412e5e0ecdd7ddd27b4e83a62f37d8ba94", .hash = "zmpl-0.0.1-SYFGBhRpAwAIJWGukw-MWyAbXq8WCDudBjAAerZ8jZ_u",
},
.zmd = .{
.url = "https://github.com/jetzig-framework/zmd/archive/d6c8aa9a9cde99674ccb096d8f94ed09cba8dab.tar.gz",
.hash = "1220d0e8734628fd910a73146e804d10a3269e3e7d065de6bb0e3e88d5ba234eb163",
}, },
.httpz = .{ .httpz = .{
.url = "https://github.com/karlseguin/http.zig/archive/f16b296a2772be97e48a57259c004aa6584a02c6.tar.gz", .url = "https://github.com/karlseguin/http.zig/archive/eced2d8c37f921a2dfc8ab639964cdc9b505a888.tar.gz",
.hash = "1220e524a72c18aa2585f326902066fda544c5df0cf6618eea885b04041204dc5d7f", .hash = "httpz-0.0.0-AAAAAL6qBgAeyws7FZLTv3e_Sbsjg4PKfB1Fg6AOHctf",
}, },
.smtp_client = .{ .smtp_client = .{
.url = "https://github.com/karlseguin/smtp_client.zig/archive/5163c66cc42cdd93176a6b1cad45f3db3a291a6a.tar.gz", .url = "https://github.com/karlseguin/smtp_client.zig/archive/5163c66cc42cdd93176a6b1cad45f3db3a291a6a.tar.gz",
.hash = "1220a7807b5161550cb0cba772689c4872bfeee8305a26c3cd0e12a8ccde1d546910", .hash = "smtp_client-0.0.1-AAAAAIJkAQCngHtRYVUMsMuncmicSHK_7ugwWibDzQ4S",
}, },
.jetquery = .{ .args = .{
.url = "https://github.com/jetzig-framework/jetquery/archive/55ebed84ad80d3d6c6e026c06e37b7de22168e7b.tar.gz", .url = "https://github.com/ikskuh/zig-args/archive/968258dc1b1230493d8f1677097c832a3d7e0bd8.tar.gz",
.hash = "1220715b9064087cdc114e1a6a087e56d242cea56bc5759b740f4c2d5c1765822add", .hash = "1220bdedf1a993d852d8aebcd63922a8fb163fac37b9c6ff72d187b2847a4a3a4248",
},
.zmpl = .{
.url = "https://github.com/jetzig-framework/zmpl/archive/04d9fa7c3f6369790aac1fc64625e91222072ebc.tar.gz",
.hash = "122078b3cd8ac2be7ba7b122312de7abee6de6be905287e2b3d80b09e5481a39e70f",
}, },
}, },

View File

@ -1,5 +1,6 @@
.{ .{
.name = "jetzig-cli", .name = .jetzig_cli,
.fingerprint = 0x73894a3e0616c96a,
.version = "0.0.0", .version = "0.0.0",
.minimum_zig_version = "0.12.0", .minimum_zig_version = "0.12.0",

View File

@ -149,7 +149,7 @@ pub fn run(
const tmpdir_real_path = try tmpdir.realpathAlloc(allocator, "."); const tmpdir_real_path = try tmpdir.realpathAlloc(allocator, ".");
defer allocator.free(tmpdir_real_path); defer allocator.free(tmpdir_real_path);
try util.runCommandInDir(allocator, tar_argv.items, .{ .path = tmpdir_real_path }); try util.runCommandInDir(allocator, tar_argv.items, .{ .path = tmpdir_real_path }, .{});
switch (builtin.os.tag) { switch (builtin.os.tag) {
.windows => {}, .windows => {},
@ -215,7 +215,7 @@ fn zig_build_install(allocator: std.mem.Allocator, path: []const u8, options: Op
defer project_dir.close(); defer project_dir.close();
project_dir.makePath(".bundle") catch {}; project_dir.makePath(".bundle") catch {};
try util.runCommandInDir(allocator, install_argv.items, .{ .path = path }); try util.runCommandInDir(allocator, install_argv.items, .{ .path = path }, .{});
const install_bin_path = try std.fs.path.join(allocator, &[_][]const u8{ ".bundle", "bin" }); const install_bin_path = try std.fs.path.join(allocator, &[_][]const u8{ ".bundle", "bin" });
defer allocator.free(install_bin_path); defer allocator.free(install_bin_path);

View File

@ -202,6 +202,7 @@ pub fn run(
github_url, github_url,
}, },
.{ .dir = install_dir }, .{ .dir = install_dir },
.{},
); );
// TODO: Use arg or interactive prompt to do Git setup in net project, default to no. // TODO: Use arg or interactive prompt to do Git setup in net project, default to no.
@ -333,6 +334,7 @@ fn gitSetup(allocator: std.mem.Allocator, install_dir: *std.fs.Dir) !void {
".", ".",
}, },
.{ .path = install_dir }, .{ .path = install_dir },
.{},
); );
try util.runCommandInDir( try util.runCommandInDir(
@ -343,6 +345,7 @@ fn gitSetup(allocator: std.mem.Allocator, install_dir: *std.fs.Dir) !void {
".", ".",
}, },
.{ .path = install_dir }, .{ .path = install_dir },
.{},
); );
try util.runCommandInDir( try util.runCommandInDir(
@ -354,5 +357,6 @@ fn gitSetup(allocator: std.mem.Allocator, install_dir: *std.fs.Dir) !void {
"Initialize Jetzig project", "Initialize Jetzig project",
}, },
.{ .path = install_dir }, .{ .path = install_dir },
.{},
); );
} }

View File

@ -78,11 +78,7 @@ pub fn run(
}); });
while (true) { while (true) {
util.runCommandInDir( util.runCommandInDir(allocator, argv.items, .{ .path = realpath }, .{}) catch {
allocator,
argv.items,
.{ .path = realpath },
) catch {
std.debug.print("Build failed, waiting for file change...\n", .{}); std.debug.print("Build failed, waiting for file change...\n", .{});
try awaitFileChange(allocator, cwd, &mtime); try awaitFileChange(allocator, cwd, &mtime);
std.debug.print("Changes detected, restarting server...\n", .{}); std.debug.print("Changes detected, restarting server...\n", .{});

View File

@ -153,7 +153,7 @@ pub fn runCommandStreaming(allocator: std.mem.Allocator, install_path: []const u
pub fn runCommand(allocator: std.mem.Allocator, argv: []const []const u8) !void { pub fn runCommand(allocator: std.mem.Allocator, argv: []const []const u8) !void {
var dir = try detectJetzigProjectDir(); var dir = try detectJetzigProjectDir();
defer dir.close(); defer dir.close();
try runCommandInDir(allocator, argv, .{ .dir = dir }); try runCommandInDir(allocator, argv, .{ .dir = dir }, .{});
} }
const Dir = union(enum) { const Dir = union(enum) {
@ -161,34 +161,53 @@ const Dir = union(enum) {
dir: std.fs.Dir, dir: std.fs.Dir,
}; };
pub const RunOptions = struct {
output: enum { stream, capture } = .capture,
wait: bool = true,
};
/// Runs a command as a child process in the given directory and verifies successful exit code. /// Runs a command as a child process in the given directory and verifies successful exit code.
pub fn runCommandInDir(allocator: std.mem.Allocator, argv: []const []const u8, dir: Dir) !void { pub fn runCommandInDir(allocator: std.mem.Allocator, argv: []const []const u8, dir: Dir, options: RunOptions) !void {
const cwd_path = switch (dir) { const cwd_path = switch (dir) {
.path => |capture| capture, .path => |capture| capture,
.dir => |capture| try capture.realpathAlloc(allocator, "."), .dir => |capture| try capture.realpathAlloc(allocator, "."),
}; };
defer if (dir == .dir) allocator.free(cwd_path); defer if (dir == .dir) allocator.free(cwd_path);
const result = std.process.Child.run(.{ const output_behaviour: std.process.Child.StdIo = switch (options.output) {
.allocator = allocator, .stream => .Inherit,
.argv = argv, .capture => .Pipe,
.cwd = cwd_path, };
}) catch |err| {
switch (err) { var child = std.process.Child.init(argv, allocator);
error.FileNotFound => { child.stdin_behavior = .Ignore;
printFailure(); child.stdout_behavior = output_behaviour;
const cmd_str = try std.mem.join(allocator, " ", argv); child.stderr_behavior = output_behaviour;
defer allocator.free(cmd_str); if (options.output == .stream) {
std.debug.print( child.stdout = std.io.getStdOut();
\\Error: Could not execute command - executable '{s}' not found child.stderr = std.io.getStdErr();
\\Command: {s}
\\Working directory: {s}
\\
, .{ argv[0], cmd_str, cwd_path });
return error.JetzigCommandError;
},
else => return err,
} }
child.cwd = cwd_path;
var stdout = try std.ArrayListUnmanaged(u8).initCapacity(allocator, 0);
var stderr = try std.ArrayListUnmanaged(u8).initCapacity(allocator, 0);
errdefer {
stdout.deinit(allocator);
stderr.deinit(allocator);
}
try child.spawn();
switch (options.output) {
.capture => try child.collectOutput(allocator, &stdout, &stderr, 50 * 1024),
.stream => {},
}
if (!options.wait) return;
const result = std.process.Child.RunResult{
.term = try child.wait(),
.stdout = try stdout.toOwnedSlice(allocator),
.stderr = try stderr.toOwnedSlice(allocator),
}; };
defer allocator.free(result.stdout); defer allocator.free(result.stdout);
defer allocator.free(result.stderr); defer allocator.free(result.stderr);

1
demo/.gitignore vendored
View File

@ -4,3 +4,4 @@ static/
src/app/views/**/.*.zig src/app/views/**/.*.zig
.DS_Store .DS_Store
log/ log/
src/routes.zig

View File

@ -0,0 +1,3 @@
<div>
<span>Content goes here</span>
</div>

View File

@ -12,6 +12,7 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
try root.put("imported_number", importedFunction(100, 200, 300)); try root.put("imported_number", importedFunction(100, 200, 300));
try request.response.headers.append("x-example-header", "example header value"); try request.response.headers.append("x-example-header", "example header value");
try request.server.logger.INFO("data", .{});
return request.render(.ok); return request.render(.ok);
} }

View File

@ -4,7 +4,7 @@
<!-- Renders `src/app/views/root/_quotes.zmpl`: --> <!-- Renders `src/app/views/root/_quotes.zmpl`: -->
<div> <div>
@partial root/quotes(message: .message) @partial root/quotes(message: $.message)
</div> </div>
<div> <div>

View File

@ -24,8 +24,7 @@ const AppOptions = struct {
}; };
/// 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.
/// `@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 {
defer self.env.deinit(); defer self.env.deinit();
@ -35,7 +34,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
defer mime_map.deinit(); defer mime_map.deinit();
try mime_map.build(); try mime_map.build();
const routes = try createRoutes(self.allocator, &routes_module.routes); const routes = try createRoutes(self.allocator, if (@hasDecl(routes_module, "routes")) &routes_module.routes else &.{});
defer { defer {
for (routes) |var_route| { for (routes) |var_route| {
var_route.deinitParams(); var_route.deinitParams();
@ -87,8 +86,8 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
self.env, self.env,
routes, routes,
self.custom_routes.items, self.custom_routes.items,
&routes_module.jobs, if (@hasDecl(routes_module, "jobs")) &routes_module.jobs else &.{},
&routes_module.mailers, if (@hasDecl(routes_module, "jobs")) &routes_module.mailers else &.{},
&mime_map, &mime_map,
&store, &store,
&job_queue, &job_queue,
@ -107,8 +106,8 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
.vars = self.env.vars, .vars = self.env.vars,
.environment = self.env.environment, .environment = self.env.environment,
.routes = routes, .routes = routes,
.jobs = &routes_module.jobs, .jobs = if (@hasDecl(routes_module, "jobs")) &routes_module.jobs else &.{},
.mailers = &routes_module.mailers, .mailers = if (@hasDecl(routes_module, "jobs")) &routes_module.mailers else &.{},
.store = &store, .store = &store,
.cache = &cache, .cache = &cache,
.repo = &repo, .repo = &repo,
@ -132,7 +131,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
return; return;
}, },
else => { else => {
try server.logger.ERROR("Encountered error: {}\nExiting.\n", .{err}); try server.logger.ERROR("Encountered error at server launch: {}\nExiting.\n", .{err});
std.process.exit(1); std.process.exit(1);
}, },
} }

View File

@ -110,6 +110,8 @@ pub fn listen(self: *Server) !void {
self.initialized = true; self.initialized = true;
try jetzig.http.middleware.afterLaunch(self);
return try httpz_server.listen(); return try httpz_server.listen();
} }
@ -151,7 +153,23 @@ pub fn processNextRequest(
try request.process(); try request.process();
var middleware_data = try jetzig.http.middleware.afterRequest(&request); var middleware_data = try jetzig.http.middleware.afterRequest(&request);
if (try maybeMiddlewareRender(&request, &response)) {
try self.logger.logRequest(&request);
return;
}
try self.renderResponse(&request);
try request.response.headers.append("Content-Type", response.content_type);
try jetzig.http.middleware.beforeResponse(&middleware_data, &request);
try request.respond();
try jetzig.http.middleware.afterResponse(&middleware_data, &request);
jetzig.http.middleware.deinit(&middleware_data, &request);
try self.logger.logRequest(&request);
}
fn maybeMiddlewareRender(request: *jetzig.http.Request, response: *const jetzig.http.Response) !bool {
if (request.middleware_rendered) |_| { if (request.middleware_rendered) |_| {
// Request processing ends when a middleware renders or redirects. // Request processing ends when a middleware renders or redirects.
if (request.redirect_state) |state| { if (request.redirect_state) |state| {
@ -162,17 +180,8 @@ pub fn processNextRequest(
} }
try request.response.headers.append("Content-Type", response.content_type); try request.response.headers.append("Content-Type", response.content_type);
try request.respond(); try request.respond();
} else { return true;
try self.renderResponse(&request); } else return false;
try request.response.headers.append("Content-Type", response.content_type);
try jetzig.http.middleware.beforeResponse(&middleware_data, &request);
try request.respond();
try jetzig.http.middleware.afterResponse(&middleware_data, &request);
jetzig.http.middleware.deinit(&middleware_data, &request);
}
try self.logger.logRequest(&request);
} }
fn renderResponse(self: *Server, request: *jetzig.http.Request) !void { fn renderResponse(self: *Server, request: *jetzig.http.Request) !void {

View File

@ -48,6 +48,14 @@ pub fn Type(comptime name: MiddlewareEnum()) type {
} }
} }
pub fn afterLaunch(server: *jetzig.http.Server) !void {
inline for (middlewares) |middleware| {
if (comptime @hasDecl(middleware, "afterLaunch")) {
try middleware.afterLaunch(server);
}
}
}
pub fn afterRequest(request: *jetzig.http.Request) !MiddlewareData { pub fn afterRequest(request: *jetzig.http.Request) !MiddlewareData {
var middleware_data = MiddlewareData.init(0) catch unreachable; var middleware_data = MiddlewareData.init(0) catch unreachable;