mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
Update http.zig
Refactor routes generation to standalone exe (fixes some build-time vs. run-time issues).
This commit is contained in:
parent
32da79ca21
commit
e98c5ec3df
79
build.zig
79
build.zig
@ -2,13 +2,13 @@ const std = @import("std");
|
||||
|
||||
pub const Routes = @import("src/Routes.zig");
|
||||
pub const GenerateMimeTypes = @import("src/GenerateMimeTypes.zig");
|
||||
pub const TemplateFn = @import("src/jetzig.zig").TemplateFn;
|
||||
pub const StaticRequest = @import("src/jetzig.zig").StaticRequest;
|
||||
pub const http = @import("src/jetzig/http.zig");
|
||||
pub const data = @import("src/jetzig/data.zig");
|
||||
pub const views = @import("src/jetzig/views.zig");
|
||||
pub const Route = views.Route;
|
||||
pub const Job = @import("src/jetzig.zig").Job;
|
||||
// pub const TemplateFn = @import("src/jetzig.zig").TemplateFn;
|
||||
// pub const StaticRequest = @import("src/jetzig.zig").StaticRequest;
|
||||
// pub const http = @import("src/jetzig/http.zig");
|
||||
// pub const data = @import("src/jetzig/data.zig");
|
||||
// pub const views = @import("src/jetzig/views.zig");
|
||||
// pub const Route = views.Route;
|
||||
// pub const Job = @import("src/jetzig.zig").Job;
|
||||
|
||||
const zmpl_build = @import("zmpl");
|
||||
|
||||
@ -155,39 +155,31 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
|
||||
&[_][]const u8{ root_path, "src", "app", "mailers" },
|
||||
);
|
||||
|
||||
var routes = try Routes.init(
|
||||
b.allocator,
|
||||
const exe_routes_file = b.addExecutable(.{
|
||||
.name = "routes",
|
||||
.root_source_file = jetzig_dep.path("src/routes_file.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
exe_routes_file.root_module.addImport("jetzig", jetzig_module);
|
||||
exe_routes_file.root_module.addImport("jetkv", jetzig_module);
|
||||
exe_routes_file.root_module.addImport("httpz", jetzig_module);
|
||||
exe_routes_file.root_module.addImport("zmpl", zmpl_module);
|
||||
|
||||
const run_routes_file_cmd = b.addRunArtifact(exe_routes_file);
|
||||
const routes_file_path = run_routes_file_cmd.addOutputFileArg("routes.zig");
|
||||
run_routes_file_cmd.addArgs(&.{
|
||||
root_path,
|
||||
b.pathFromRoot("src"),
|
||||
templates_path,
|
||||
views_path,
|
||||
jobs_path,
|
||||
mailers_path,
|
||||
);
|
||||
const generated_routes = try routes.generateRoutes();
|
||||
const routes_write_files = b.addWriteFiles();
|
||||
const routes_file = routes_write_files.add("routes.zig", generated_routes);
|
||||
const tests_write_files = b.addWriteFiles();
|
||||
const tests_file = tests_write_files.add("tests.zig", generated_routes);
|
||||
const routes_module = b.createModule(.{ .root_source_file = routes_file });
|
||||
|
||||
var src_dir = try std.fs.openDirAbsolute(b.pathFromRoot("src"), .{ .iterate = true });
|
||||
defer src_dir.close();
|
||||
var walker = try src_dir.walk(b.allocator);
|
||||
defer walker.deinit();
|
||||
|
||||
while (try walker.next()) |entry| {
|
||||
if (entry.kind == .file) {
|
||||
const stat = try src_dir.statFile(entry.path);
|
||||
const src_data = try src_dir.readFileAlloc(b.allocator, entry.path, @intCast(stat.size));
|
||||
defer b.allocator.free(src_data);
|
||||
|
||||
const relpath = try std.fs.path.join(b.allocator, &[_][]const u8{ "src", entry.path });
|
||||
defer b.allocator.free(relpath);
|
||||
|
||||
_ = routes_write_files.add(relpath, src_data);
|
||||
_ = tests_write_files.add(relpath, src_data);
|
||||
}
|
||||
}
|
||||
});
|
||||
const routes_module = b.createModule(.{ .root_source_file = routes_file_path });
|
||||
routes_module.addImport("jetzig", jetzig_module);
|
||||
exe.root_module.addImport("routes", routes_module);
|
||||
|
||||
const exe_static_routes = b.addExecutable(.{
|
||||
.name = "static",
|
||||
@ -196,12 +188,9 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
exe.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("zmpl", zmpl_module);
|
||||
// exe_static_routes.root_module.addImport("jetzig_app", &exe.root_module);
|
||||
|
||||
const markdown_fragments_write_files = b.addWriteFiles();
|
||||
const path = markdown_fragments_write_files.add("markdown_fragments.zig", try generateMarkdownFragments(b));
|
||||
@ -215,8 +204,19 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
|
||||
|
||||
run_static_routes_cmd.expectExitCode(0);
|
||||
|
||||
const run_tests_file_cmd = b.addRunArtifact(exe_routes_file);
|
||||
const tests_file_path = run_tests_file_cmd.addOutputFileArg("tests.zig");
|
||||
run_tests_file_cmd.addArgs(&.{
|
||||
root_path,
|
||||
b.pathFromRoot("src"),
|
||||
templates_path,
|
||||
views_path,
|
||||
jobs_path,
|
||||
mailers_path,
|
||||
});
|
||||
|
||||
const exe_unit_tests = b.addTest(.{
|
||||
.root_source_file = tests_file,
|
||||
.root_source_file = tests_file_path,
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.test_runner = jetzig_dep.path("src/test_runner.zig"),
|
||||
@ -238,6 +238,7 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
|
||||
const test_step = b.step("jetzig:test", "Run tests");
|
||||
test_step.dependOn(&run_static_routes_cmd.step);
|
||||
test_step.dependOn(&run_exe_unit_tests.step);
|
||||
test_step.dependOn(&run_tests_file_cmd.step);
|
||||
exe_unit_tests.root_module.addImport("routes", routes_module);
|
||||
|
||||
const routes_step = b.step("jetzig:routes", "List all routes in your app");
|
||||
|
@ -11,20 +11,20 @@
|
||||
.hash = "12207c30c6fbcb8c7519719fc47ff9d0acca72a3557ec671984d16260bdf1c832740",
|
||||
},
|
||||
.jetkv = .{
|
||||
.url = "https://github.com/jetzig-framework/jetkv/archive/78bcdcc6b0cbd3ca808685c64554a15701f13250.tar.gz",
|
||||
.hash = "12201944769794b2f18e3932ec3d5031066d0ccb3293cf7c3fb1f5b269e56b76c57e",
|
||||
.url = "https://github.com/jetzig-framework/jetkv/archive/cf1c611a9c406ccadb50b1bf7798e2b88c43fd2b.tar.gz",
|
||||
.hash = "1220434169bfbfbb3a0bbe7073f9bcff5401979a967d25ba3915046f6ea44e40839d",
|
||||
},
|
||||
.args = .{
|
||||
.url = "https://github.com/ikskuh/zig-args/archive/03af1b6c5bfda9646a562c861055024daed5b238.tar.gz",
|
||||
.hash = "1220904d2fdcd970dd0d216211d092eb3ef6da01117163cc9393ab845a1d66c029d9",
|
||||
},
|
||||
.smtp_client = .{
|
||||
.url = "https://github.com/karlseguin/smtp_client.zig/archive/48971bc919fe22d2bf6accb832b73ab53e534836.tar.gz",
|
||||
.hash = "122024d52d160022a6fefdbebfd94ac664f637583c8a44c2a50d0ad12acca160cb17",
|
||||
.url = "https://github.com/karlseguin/smtp_client.zig/archive/8fcfad9ca2d9e446612c79f4e54050cfbe81b38d.tar.gz",
|
||||
.hash = "1220cebfcf6c63295819df92ec54abe62aad91b1d16666781194c29a7874bb7bbbda",
|
||||
},
|
||||
.httpz = .{
|
||||
.url = "https://github.com/karlseguin/http.zig/archive/fbca868592dc83ee3ee3cad414c62afa266f4866.tar.gz",
|
||||
.hash = "122089946af5ba1cdfae3f515f0fa1c96327f42a462515fbcecc719fe94fab38d9b8",
|
||||
.url = "https://github.com/karlseguin/http.zig/archive/e1df408cd530d058a7e1652bd8dcfa7191ff776e.tar.gz",
|
||||
.hash = "1220efbc744de6e13e57ab9fa6370ea9690601e55ba6ced74179c081a295c99fd422",
|
||||
},
|
||||
},
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
<div>
|
||||
<span>Content goes here</span>
|
||||
<div>Hello</div>
|
||||
</div>
|
||||
|
@ -1,5 +1,5 @@
|
||||
const std = @import("std");
|
||||
const jetzig = @import("jetzig.zig");
|
||||
const jetzig = @import("jetzig");
|
||||
|
||||
ast: std.zig.Ast = undefined,
|
||||
allocator: std.mem.Allocator,
|
||||
|
@ -2,7 +2,7 @@ const std = @import("std");
|
||||
|
||||
pub const zmpl = @import("zmpl").zmpl;
|
||||
pub const zmd = @import("zmd").zmd;
|
||||
pub const jetkv = @import("jetkv");
|
||||
pub const jetkv = @import("jetkv").jetkv;
|
||||
|
||||
pub const http = @import("jetzig/http.zig");
|
||||
pub const loggers = @import("jetzig/loggers.zig");
|
||||
|
@ -79,7 +79,7 @@ const Dispatcher = struct {
|
||||
pub fn listen(self: *Server) !void {
|
||||
try self.decodeStaticParams();
|
||||
|
||||
var httpz_server = try httpz.ServerCtx(Dispatcher, Dispatcher).init(
|
||||
var httpz_server = try httpz.Server(Dispatcher).init(
|
||||
self.allocator,
|
||||
.{
|
||||
.port = self.options.port,
|
||||
|
@ -135,7 +135,7 @@ pub fn request(
|
||||
return .{
|
||||
.allocator = self.arena.allocator(),
|
||||
.status = httpz_response.status,
|
||||
.body = try self.arena.allocator().dupe(u8, httpz_response.body orelse ""),
|
||||
.body = try self.arena.allocator().dupe(u8, httpz_response.body),
|
||||
.headers = try headers.toOwnedSlice(),
|
||||
.jobs = try jobs.toOwnedSlice(),
|
||||
};
|
||||
@ -208,6 +208,7 @@ fn stubbedRequest(
|
||||
multipart_boundary: ?[]const u8,
|
||||
options: RequestOptions,
|
||||
) !httpz.Request {
|
||||
// TODO: Use httpz.testing
|
||||
var request_headers = try keyValue(allocator, 32);
|
||||
for (options.headers) |header| request_headers.add(header.name, header.value);
|
||||
if (options.json != null) {
|
||||
@ -256,6 +257,7 @@ fn stubbedRequest(
|
||||
}
|
||||
|
||||
fn stubbedResponse(allocator: std.mem.Allocator) !httpz.Response {
|
||||
// TODO: Use httpz.testing
|
||||
return .{
|
||||
.conn = undefined,
|
||||
.pos = 0,
|
||||
@ -265,9 +267,9 @@ fn stubbedResponse(allocator: std.mem.Allocator) !httpz.Response {
|
||||
.arena = allocator,
|
||||
.written = false,
|
||||
.chunked = false,
|
||||
.disowned = false,
|
||||
.keepalive = false,
|
||||
.body = null,
|
||||
.body = "",
|
||||
.buffer = .{ .pos = 0, .data = "" },
|
||||
};
|
||||
}
|
||||
|
||||
|
53
src/routes_file.zig
Normal file
53
src/routes_file.zig
Normal file
@ -0,0 +1,53 @@
|
||||
const std = @import("std");
|
||||
const Routes = @import("Routes.zig");
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer std.debug.assert(gpa.deinit() == .ok);
|
||||
const gpa_allocator = gpa.allocator();
|
||||
|
||||
var arena = std.heap.ArenaAllocator.init(gpa_allocator);
|
||||
const allocator = arena.allocator();
|
||||
defer arena.deinit();
|
||||
|
||||
var it = try std.process.argsWithAllocator(allocator);
|
||||
_ = it.next().?;
|
||||
const output_path = it.next().?;
|
||||
const root_path = it.next().?;
|
||||
const src_path = it.next().?;
|
||||
const templates_path = it.next().?;
|
||||
const views_path = it.next().?;
|
||||
const jobs_path = it.next().?;
|
||||
const mailers_path = it.next().?;
|
||||
|
||||
var routes = try Routes.init(
|
||||
allocator,
|
||||
root_path,
|
||||
templates_path,
|
||||
views_path,
|
||||
jobs_path,
|
||||
mailers_path,
|
||||
);
|
||||
const generated_routes = try routes.generateRoutes();
|
||||
var src_dir = try std.fs.openDirAbsolute(src_path, .{ .iterate = true });
|
||||
defer src_dir.close();
|
||||
var walker = try src_dir.walk(allocator);
|
||||
defer walker.deinit();
|
||||
|
||||
while (try walker.next()) |entry| {
|
||||
if (entry.kind == .file) {
|
||||
const stat = try src_dir.statFile(entry.path);
|
||||
const src_data = try src_dir.readFileAlloc(allocator, entry.path, @intCast(stat.size));
|
||||
const relpath = try std.fs.path.join(allocator, &[_][]const u8{ "src", entry.path });
|
||||
var dir = try std.fs.openDirAbsolute(std.fs.path.dirname(output_path).?, .{});
|
||||
const dest_dir = try dir.makeOpenPath(std.fs.path.dirname(relpath).?, .{});
|
||||
const src_file = try dest_dir.createFile(std.fs.path.basename(relpath), .{});
|
||||
try src_file.writeAll(src_data);
|
||||
src_file.close();
|
||||
}
|
||||
}
|
||||
|
||||
const file = try std.fs.createFileAbsolute(output_path, .{ .truncate = true });
|
||||
try file.writeAll(generated_routes);
|
||||
file.close();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user