Init
This commit is contained in:
commit
c018ea3c0e
14 changed files with 381 additions and 0 deletions
65
src/app/middleware/DemoMiddleware.zig
Normal file
65
src/app/middleware/DemoMiddleware.zig
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
/// Demo middleware. Assign middleware by declaring `pub const middleware` in the
|
||||
/// `jetzig_options` defined in your application's `src/main.zig`.
|
||||
///
|
||||
/// Middleware is called before and after the request, providing full access to the active
|
||||
/// request, allowing you to execute any custom code for logging, tracking, inserting response
|
||||
/// headers, etc.
|
||||
///
|
||||
/// This middleware is configured in the demo app's `src/main.zig`:
|
||||
///
|
||||
/// ```
|
||||
/// pub const jetzig_options = struct {
|
||||
/// pub const middleware: []const type = &.{@import("app/middleware/DemoMiddleware.zig")};
|
||||
/// };
|
||||
/// ```
|
||||
const std = @import("std");
|
||||
const jetzig = @import("jetzig");
|
||||
|
||||
/// Define any custom data fields you want to store here. Assigning to these fields in the `init`
|
||||
/// function allows you to access them in various middleware callbacks defined below, where they
|
||||
/// can also be modified.
|
||||
my_custom_value: []const u8,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
/// Initialize middleware.
|
||||
pub fn init(request: *jetzig.http.Request) !*Self {
|
||||
var middleware = try request.allocator.create(Self);
|
||||
middleware.my_custom_value = "initial value";
|
||||
return middleware;
|
||||
}
|
||||
|
||||
/// Invoked immediately after the request is received but before it has started processing.
|
||||
/// Any calls to `request.render` or `request.redirect` will prevent further processing of the
|
||||
/// request, including any other middleware in the chain.
|
||||
pub fn afterRequest(self: *Self, request: *jetzig.http.Request) !void {
|
||||
try request.server.logger.DEBUG(
|
||||
"[DemoMiddleware:afterRequest] my_custom_value: {s}",
|
||||
.{self.my_custom_value},
|
||||
);
|
||||
self.my_custom_value = @tagName(request.method);
|
||||
}
|
||||
|
||||
/// Invoked immediately before the response renders to the client.
|
||||
/// The response can be modified here if needed.
|
||||
pub fn beforeResponse(self: *Self, request: *jetzig.http.Request, response: *jetzig.http.Response) !void {
|
||||
try request.server.logger.DEBUG(
|
||||
"[DemoMiddleware:beforeResponse] my_custom_value: {s}, response status: {s}",
|
||||
.{ self.my_custom_value, @tagName(response.status_code) },
|
||||
);
|
||||
}
|
||||
|
||||
/// Invoked immediately after the response has been finalized and sent to the client.
|
||||
/// Response data can be accessed for logging, but any modifications will have no impact.
|
||||
pub fn afterResponse(self: *Self, request: *jetzig.http.Request, response: *jetzig.http.Response) !void {
|
||||
_ = self;
|
||||
_ = response;
|
||||
try request.server.logger.DEBUG("[DemoMiddleware:afterResponse] response completed", .{});
|
||||
}
|
||||
|
||||
/// Invoked after `afterResponse` is called. Use this function to do any clean-up.
|
||||
/// Note that `request.allocator` is an arena allocator, so any allocations are automatically
|
||||
/// freed before the next request starts processing.
|
||||
pub fn deinit(self: *Self, request: *jetzig.http.Request) void {
|
||||
request.allocator.destroy(self);
|
||||
}
|
||||
43
src/app/views/root.zig
Normal file
43
src/app/views/root.zig
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
const jetzig = @import("jetzig");
|
||||
|
||||
/// `src/app/views/root.zig` represents the root URL `/`
|
||||
/// The `index` view function is invoked when when the HTTP verb is `GET`.
|
||||
/// Other view types are invoked either by passing a resource ID value (e.g. `/1234`) or by using
|
||||
/// a different HTTP verb:
|
||||
///
|
||||
/// GET / => index(request, data)
|
||||
/// GET /1234 => get(id, request, data)
|
||||
/// POST / => post(request, data)
|
||||
/// PUT /1234 => put(id, request, data)
|
||||
/// PATCH /1234 => patch(id, request, data)
|
||||
/// DELETE /1234 => delete(id, request, data)
|
||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
||||
// The first call to `data.object()` or `data.array()` sets the root response data value.
|
||||
// JSON requests return a JSON string representation of the root data value.
|
||||
// Zmpl templates can access all values in the root data value.
|
||||
var root = try data.object();
|
||||
|
||||
// Add a string to the root object.
|
||||
try root.put("message", data.string("Welcome to Jetzig!"));
|
||||
|
||||
// Request params have the same type as a `data.object()` so they can be inserted them
|
||||
// directly into the response data. Fetch `http://localhost:8080/?message=hello` to set the
|
||||
// param. JSON data is also accepted when the `content-type: application/json` header is
|
||||
// present.
|
||||
const params = try request.params();
|
||||
|
||||
if (params.get("message")) |value| {
|
||||
try root.put("message_param", value);
|
||||
}
|
||||
|
||||
// Set arbitrary response headers as required. `content-type` is automatically assigned for
|
||||
// HTML, JSON responses.
|
||||
//
|
||||
// Static files located in `public/` in the root of your project directory are accessible
|
||||
// from the root path (e.g. `public/jetzig.png`) is available at `/jetzig.png` and the
|
||||
// content type is inferred from the extension using MIME types.
|
||||
try request.response.headers.append("x-example-header", "example header value");
|
||||
|
||||
// Render the response and set the response code.
|
||||
return request.render(.ok);
|
||||
}
|
||||
18
src/app/views/root/_content.zmpl
Normal file
18
src/app/views/root/_content.zmpl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Renders the `message` response data value.
|
||||
<h3 class="message text-[#39b54a]">{.message}</h3>
|
||||
|
||||
<div><img class="p-3 mx-auto" src="/jetzig.png" /></div>
|
||||
|
||||
<div>
|
||||
<a href="https://github.com/jetzig-framework/zmpl">
|
||||
<img class="p-3 m-3 mx-auto" src="/zmpl.png" />
|
||||
</a>
|
||||
|
||||
</div>
|
||||
|
||||
<div>Visit <a class="font-bold text-[#39b54a]" href="https://jetzig.dev/">jetzig.dev</a> to get started.
|
||||
<div>Join our Discord server and introduce yourself:</div>
|
||||
<div>
|
||||
<a class="font-bold text-[#39b54a]" href="https://discord.gg/eufqssz7X6">https://discord.gg/eufqssz7X6</a>
|
||||
</div>
|
||||
</div>
|
||||
20
src/app/views/root/index.zmpl
Normal file
20
src/app/views/root/index.zmpl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<html>
|
||||
<head>
|
||||
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<link rel="stylesheet" href="/styles.css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="text-center pt-10 m-auto">
|
||||
// If present, renders the `message_param` response data value, add `?message=hello` to the
|
||||
// URL to see the output:
|
||||
<h2 class="param text-3xl text-[#f7931e]">{.message_param}</h2>
|
||||
|
||||
// Renders `src/app/views/root/_content.zmpl` with the same template data available:
|
||||
<div>{^root/content}</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue