mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
Clean up static params and init script
This commit is contained in:
parent
055c018368
commit
3882eba2f3
@ -203,6 +203,11 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
|
|||||||
exe_static_routes.root_module.addImport("zmpl", zmpl_module);
|
exe_static_routes.root_module.addImport("zmpl", zmpl_module);
|
||||||
// exe_static_routes.root_module.addImport("jetzig_app", &exe.root_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));
|
||||||
|
const markdown_fragments_module = b.createModule(.{ .root_source_file = path });
|
||||||
|
exe_static_routes.root_module.addImport("markdown_fragments", markdown_fragments_module);
|
||||||
|
|
||||||
const run_static_routes_cmd = b.addRunArtifact(exe_static_routes);
|
const run_static_routes_cmd = b.addRunArtifact(exe_static_routes);
|
||||||
const static_outputs_path = run_static_routes_cmd.addOutputFileArg("static.zig");
|
const static_outputs_path = run_static_routes_cmd.addOutputFileArg("static.zig");
|
||||||
const static_module = b.createModule(.{ .root_source_file = static_outputs_path });
|
const static_module = b.createModule(.{ .root_source_file = static_outputs_path });
|
||||||
@ -218,7 +223,6 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn
|
|||||||
});
|
});
|
||||||
exe_unit_tests.root_module.addImport("jetzig", jetzig_module);
|
exe_unit_tests.root_module.addImport("jetzig", jetzig_module);
|
||||||
exe_unit_tests.root_module.addImport("static", static_module);
|
exe_unit_tests.root_module.addImport("static", static_module);
|
||||||
exe_unit_tests.root_module.addImport("__jetzig_project", &exe.root_module);
|
|
||||||
|
|
||||||
var it = exe.root_module.import_table.iterator();
|
var it = exe.root_module.import_table.iterator();
|
||||||
while (it.next()) |import| {
|
while (it.next()) |import| {
|
||||||
|
@ -191,13 +191,6 @@ pub fn run(
|
|||||||
github_url,
|
github_url,
|
||||||
});
|
});
|
||||||
|
|
||||||
try util.runCommand(allocator, realpath, &[_][]const u8{
|
|
||||||
"zig",
|
|
||||||
"fetch",
|
|
||||||
"--save",
|
|
||||||
"https://github.com/jetzig-framework/iguanas/archive/89c2abf29de0bc31054a9a6feac5a6a83bab0459.tar.gz",
|
|
||||||
});
|
|
||||||
|
|
||||||
// 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.
|
||||||
// const git_setup = false;
|
// const git_setup = false;
|
||||||
// if (git_setup) try gitSetup(allocator, install_dir);
|
// if (git_setup) try gitSetup(allocator, install_dir);
|
||||||
|
1
demo/.ruby-version
Normal file
1
demo/.ruby-version
Normal file
@ -0,0 +1 @@
|
|||||||
|
3.2.2
|
@ -12,11 +12,12 @@ pub fn build(b: *std.Build) !void {
|
|||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Example dependency:
|
// Example Dependency
|
||||||
const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target });
|
// -------------------
|
||||||
exe.root_module.addImport("iguanas", iguanas_dep.module("iguanas"));
|
// const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target });
|
||||||
|
// exe.root_module.addImport("iguanas", iguanas_dep.module("iguanas"));
|
||||||
|
|
||||||
// All dependencies **must** be added to imports above this line.
|
// ^ Add all dependencies before `jetzig.jetzigInit()` ^
|
||||||
|
|
||||||
try jetzig.jetzigInit(b, exe, .{});
|
try jetzig.jetzigInit(b, exe, .{});
|
||||||
|
|
||||||
|
@ -6,10 +6,6 @@
|
|||||||
.jetzig = .{
|
.jetzig = .{
|
||||||
.path = "../",
|
.path = "../",
|
||||||
},
|
},
|
||||||
.iguanas = .{
|
|
||||||
.url = "https://github.com/jetzig-framework/iguanas/archive/045f34b3455fb5bbec8cbf81fdc5d19ee54bd625.tar.gz",
|
|
||||||
.hash = "12207f8cca149669f7c4bdc02103f6194189a312bbaae97b54e6dc3fb8483d6107bf",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
.paths = .{
|
.paths = .{
|
||||||
// This makes *all* files, recursively, included in this package. It is generally
|
// This makes *all* files, recursively, included in this package. It is generally
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
const jetzig = @import("jetzig");
|
|
||||||
const iguanas = @import("iguanas");
|
|
||||||
|
|
||||||
/// This example uses a layout. A layout is a template that exists in `src/app/views/layouts` and
|
|
||||||
/// references `{zmpl.content}`.
|
|
||||||
///
|
|
||||||
/// The content is the rendered template for the current view which is then injected into the
|
|
||||||
/// layout in place of `{zmpl.content}`.
|
|
||||||
///
|
|
||||||
/// See `demo/src/app/views/layouts/application.zmpl`
|
|
||||||
/// and `demo/src/app/views/iguanas/index.zmpl`
|
|
||||||
pub const layout = "application";
|
|
||||||
|
|
||||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
|
||||||
var root = try data.array();
|
|
||||||
|
|
||||||
const params = try request.params();
|
|
||||||
|
|
||||||
const count = params.getT(.integer, "iguanas") orelse 10;
|
|
||||||
|
|
||||||
const iguanas_slice = try iguanas.iguanas(request.allocator, @intCast(count));
|
|
||||||
|
|
||||||
for (iguanas_slice) |iguana| {
|
|
||||||
try root.append(data.string(iguana));
|
|
||||||
}
|
|
||||||
|
|
||||||
return request.render(.ok);
|
|
||||||
}
|
|
||||||
|
|
||||||
test "index" {
|
|
||||||
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
|
|
||||||
defer app.deinit();
|
|
||||||
|
|
||||||
const response = try app.request(.GET, "/iguanas", .{ .json = .{ .iguanas = 10 } });
|
|
||||||
try response.expectJson(".1", "iguana");
|
|
||||||
try response.expectJson(".2", "iguana");
|
|
||||||
try response.expectJson(".3", "iguana");
|
|
||||||
try response.expectJson(".4", "iguana");
|
|
||||||
try response.expectJson(".5", "iguana");
|
|
||||||
try response.expectJson(".6", "iguana");
|
|
||||||
try response.expectJson(".7", "iguana");
|
|
||||||
try response.expectJson(".8", "iguana");
|
|
||||||
try response.expectJson(".9", "iguana");
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
<div>
|
|
||||||
@zig {
|
|
||||||
for (zmpl.items(.array)) |iguana| {
|
|
||||||
<div>{{iguana}}</div>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</div>
|
|
@ -64,8 +64,8 @@ pub fn get(id: []const u8, request: *jetzig.StaticRequest, data: *jetzig.Data) !
|
|||||||
try root.put("message", "id is not '123'");
|
try root.put("message", "id is not '123'");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.get("foo")) |foo| try root.put("foo", foo);
|
try root.put("foo", params.get("foo"));
|
||||||
if (params.get("bar")) |bar| try root.put("bar", bar);
|
try root.put("bar", params.get("bar"));
|
||||||
|
|
||||||
return request.render(.created);
|
return request.render(.created);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ const std = @import("std");
|
|||||||
const jetzig = @import("jetzig");
|
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 jetzig_options = @import("jetzig_app").jetzig_options;
|
// const jetzig_options = @import("jetzig_app").jetzig_options;
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
@ -114,10 +115,9 @@ fn renderMarkdown(
|
|||||||
route: jetzig.views.Route,
|
route: jetzig.views.Route,
|
||||||
view: jetzig.views.View,
|
view: jetzig.views.View,
|
||||||
) !?[]const u8 {
|
) !?[]const u8 {
|
||||||
const fragments = null;
|
|
||||||
const path = try std.mem.join(allocator, "/", &[_][]const u8{ route.uri_path, @tagName(route.action) });
|
const path = try std.mem.join(allocator, "/", &[_][]const u8{ route.uri_path, @tagName(route.action) });
|
||||||
defer allocator.free(path);
|
defer allocator.free(path);
|
||||||
const content = try jetzig.markdown.render(allocator, path, fragments) orelse return null;
|
const content = try jetzig.markdown.render(allocator, path, markdown_fragments) orelse return null;
|
||||||
|
|
||||||
if (route.layout) |layout_name| {
|
if (route.layout) |layout_name| {
|
||||||
try view.data.addConst("jetzig_view", view.data.string(route.name));
|
try view.data.addConst("jetzig_view", view.data.string(route.name));
|
||||||
|
@ -642,11 +642,6 @@ fn matchStaticContent(self: *Server, request: *jetzig.http.Request) !?[]const u8
|
|||||||
.HTML, .UNKNOWN => static_output.output.html,
|
.HTML, .UNKNOWN => static_output.output.html,
|
||||||
.JSON => static_output.output.json,
|
.JSON => static_output.output.json,
|
||||||
};
|
};
|
||||||
} else {
|
|
||||||
return switch (request_format) {
|
|
||||||
.HTML, .UNKNOWN => static_output.output.html,
|
|
||||||
.JSON => static_output.output.json,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -659,34 +654,38 @@ fn matchStaticContent(self: *Server, request: *jetzig.http.Request) !?[]const u8
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn decodeStaticParams(self: *Server) !void {
|
pub fn decodeStaticParams(self: *Server) !void {
|
||||||
|
if (!@hasDecl(jetzig.root, "static")) return;
|
||||||
|
|
||||||
// Store decoded static params (i.e. declared in views) for faster comparison at request time.
|
// Store decoded static params (i.e. declared in views) for faster comparison at request time.
|
||||||
var decoded = std.ArrayList(*jetzig.data.Value).init(self.allocator);
|
var decoded = std.ArrayList(*jetzig.data.Value).init(self.allocator);
|
||||||
for (jetzig.root.static.compiled) |compiled| {
|
for (jetzig.root.static.compiled) |compiled| {
|
||||||
if (compiled.output.params) |params| {
|
const data = try self.allocator.create(jetzig.data.Data);
|
||||||
const data = try self.allocator.create(jetzig.data.Data);
|
data.* = jetzig.data.Data.init(self.allocator);
|
||||||
data.* = jetzig.data.Data.init(self.allocator);
|
try data.fromJson(compiled.output.params orelse "{}");
|
||||||
try data.fromJson(params);
|
try decoded.append(data.value.?);
|
||||||
try decoded.append(data.value.?);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.decoded_static_route_params = try decoded.toOwnedSlice();
|
self.decoded_static_route_params = try decoded.toOwnedSlice();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn matchStaticOutput(
|
fn matchStaticOutput(
|
||||||
maybe_id: ?[]const u8,
|
maybe_expected_id: ?[]const u8,
|
||||||
maybe_params: ?*jetzig.data.Value,
|
maybe_expected_params: ?*jetzig.data.Value,
|
||||||
route: jetzig.views.Route,
|
route: jetzig.views.Route,
|
||||||
request: *const jetzig.http.Request,
|
request: *const jetzig.http.Request,
|
||||||
params: *jetzig.data.Value,
|
params: *jetzig.data.Value,
|
||||||
) bool {
|
) bool {
|
||||||
return if (maybe_params) |expected_params| blk: {
|
return if (maybe_expected_params) |expected_params| blk: {
|
||||||
|
const params_match = expected_params.count() == 0 or expected_params.eql(params);
|
||||||
break :blk switch (route.action) {
|
break :blk switch (route.action) {
|
||||||
.index, .post => expected_params.count() == 0 or expected_params.eql(params),
|
.index, .post => params_match,
|
||||||
inline else => if (maybe_id) |id|
|
inline else => if (maybe_expected_id) |expected_id|
|
||||||
std.mem.eql(u8, id, request.path.resource_id) and expected_params.eql(params)
|
std.mem.eql(u8, expected_id, request.path.resource_id) and params_match
|
||||||
else
|
else
|
||||||
false,
|
false,
|
||||||
};
|
};
|
||||||
} else if (maybe_id) |id| std.mem.eql(u8, id, request.path.resource_id) else maybe_params == null;
|
} else if (maybe_expected_id) |id|
|
||||||
|
std.mem.eql(u8, id, request.path.resource_id)
|
||||||
|
else
|
||||||
|
true; // We reached a params filter (possibly the default catch-all) with no params set.
|
||||||
}
|
}
|
||||||
|
@ -222,7 +222,7 @@ pub fn expectJson(expected_path: []const u8, expected_value: anytype, response:
|
|||||||
switch (@typeInfo(@TypeOf(expected_value))) {
|
switch (@typeInfo(@TypeOf(expected_value))) {
|
||||||
.Pointer, .Array => {
|
.Pointer, .Array => {
|
||||||
logFailure(
|
logFailure(
|
||||||
"Expected \"" ++ jetzig.colors.red("{s}") ++ "\" in " ++ jetzig.colors.cyan("{s}") ++ ", found \"" ++ jetzig.colors.green("{s}") ++ "\nJSON:" ++ json_banner,
|
"Expected \"" ++ jetzig.colors.red("{s}") ++ "\" in " ++ jetzig.colors.cyan("{s}") ++ ", found \"" ++ jetzig.colors.green("{s}") ++ "\"\nJSON:" ++ json_banner,
|
||||||
.{ expected_value, expected_path, string.value, try jsonPretty(response) },
|
.{ expected_value, expected_path, string.value, try jsonPretty(response) },
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
@ -37,7 +37,7 @@ const Test = struct {
|
|||||||
trace: ?[]const u8,
|
trace: ?[]const u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
const name_template = jetzig.colors.blue("{s}") ++ ":" ++ jetzig.colors.cyan("{s}") ++ " ";
|
const name_template = jetzig.colors.blue("{s}") ++ jetzig.colors.yellow("->") ++ "\"" ++ jetzig.colors.cyan("{s}") ++ "\" ";
|
||||||
|
|
||||||
pub fn init(test_fn: std.builtin.TestFn) Test {
|
pub fn init(test_fn: std.builtin.TestFn) Test {
|
||||||
return if (std.mem.indexOf(u8, test_fn.name, ".test.")) |index|
|
return if (std.mem.indexOf(u8, test_fn.name, ".test.")) |index|
|
||||||
@ -117,7 +117,7 @@ const Test = struct {
|
|||||||
fn printFailureDetail(self: Test, index: usize, failure: Failure, writer: anytype) !void {
|
fn printFailureDetail(self: Test, index: usize, failure: Failure, writer: anytype) !void {
|
||||||
try writer.print("\n", .{});
|
try writer.print("\n", .{});
|
||||||
|
|
||||||
const count = " FAILURE: ".len + (self.module orelse "tests").len + ":".len + self.name.len + 1;
|
const count = " FAILURE: ".len + (self.module orelse "tests").len + ":".len + self.name.len + 4;
|
||||||
|
|
||||||
try writer.writeAll(jetzig.colors.red("┌"));
|
try writer.writeAll(jetzig.colors.red("┌"));
|
||||||
for (0..count) |_| try writer.writeAll(jetzig.colors.red("─"));
|
for (0..count) |_| try writer.writeAll(jetzig.colors.red("─"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user