Something went wrong. Try again.
Dev fork of jetzig jetzig.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129/// This example demonstrates static site generation (SSG).////// Any view function that receives `*jetzig.StaticRequest` is considered as a SSG view, which/// will be invoked at build time and its content (both JSON and HTML) rendered to `static/` in/// the root project directory.////// Define `pub const static_params` as a struct with fields named after each view function, with/// the value for each field being an array of structs with fields `params` and, where/// applicable (i.e. `get`, `put`, `patch`, and `delete`), `id`.////// For each item in the provided array, a separate JSON and HTML output will be generated. At/// run time, requests are matched to the relevant content by comparing the request params and/// resource ID to locate the relevant content.////// Launch the demo app and try the following requests:////// ```console/// curl -H "Accept: application/json" \/// --data-binary '{"foo":"hello", "bar":"goodbye"}' \/// --request GET \/// 'http://localhost:8080/static'/// ```////// ```console/// curl 'http://localhost:8080/static.html?foo=hi&bar=bye'/// ```////// ```console/// curl 'http://localhost:8080/static/123.html?foo=hi&bar=bye'/// ```const std = @import("std");const jetzig = @import("jetzig");
pub const static_params = .{ .index = .{ .{ .params = .{ .foo = "hi", .bar = "bye" } }, .{ .params = .{ .foo = "hello", .bar = "goodbye" } }, }, .get = .{ .{ .id = "123", .params = .{ .foo = "hi", .bar = "bye" } }, .{ .id = "456", .params = .{ .foo = "hello", .bar = "goodbye" } }, },};
pub fn index(request: *jetzig.StaticRequest, data: *jetzig.Data) !jetzig.View { var root = try data.root(.object);
const params = try request.params();
try root.put("foo", params.get("foo")); try root.put("bar", params.get("bar"));
return request.render(.ok);}
pub fn get(id: []const u8, request: *jetzig.StaticRequest, data: *jetzig.Data) !jetzig.View { var root = try data.root(.object);
const params = try request.params();
if (std.mem.eql(u8, id, "123")) { try root.put("message", "id is '123'"); } else { try root.put("message", "id is not '123'"); }
try root.put("foo", params.get("foo")); try root.put("bar", params.get("bar"));
return request.render(.created);}
test "index json" { var app = try jetzig.testing.app(std.testing.allocator, @import("routes")); defer app.deinit();
const response = try app.request( .GET, "/static.json", .{ .json = .{ .foo = "hello", .bar = "goodbye" } }, );
try response.expectStatus(.ok); try response.expectJson(".foo", "hello"); try response.expectJson(".bar", "goodbye");}
test "get json" { var app = try jetzig.testing.app(std.testing.allocator, @import("routes")); defer app.deinit();
const response = try app.request( .GET, "/static/123.json", .{ .json = .{ .foo = "hi", .bar = "bye" } }, );
try response.expectStatus(.ok); try response.expectJson(".message", "id is '123'");}
test "index html" { var app = try jetzig.testing.app(std.testing.allocator, @import("routes")); defer app.deinit();
const response = try app.request( .GET, "/static.html", .{ .params = .{ .foo = "hello", .bar = "goodbye" } }, );
try response.expectStatus(.ok); try response.expectBodyContains("foo: hello"); try response.expectBodyContains("bar: goodbye");}
test "get html" { var app = try jetzig.testing.app(std.testing.allocator, @import("routes")); defer app.deinit();
const response = try app.request( .GET, "/static/123.html", .{ .params = .{ .foo = "hi", .bar = "bye" } }, );
try response.expectStatus(.ok);}