-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0872ba1
commit 30195d6
Showing
14 changed files
with
322 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const std = @import("std"); | ||
const httpz = @import("httpz"); | ||
const Allocator = std.mem.Allocator; | ||
|
||
const PORT = 8805; | ||
|
||
// This example uses the Handler's "handle" function to completely takeover | ||
// request processing from httpz. | ||
|
||
pub fn main() !void { | ||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
const allocator = gpa.allocator(); | ||
|
||
var handler = Handler{}; | ||
var server = try httpz.Server(*Handler).init(allocator, .{.port = PORT}, &handler); | ||
defer server.deinit(); | ||
|
||
// Routes aren't used in this mode | ||
|
||
std.debug.print("listening http://localhost:{d}/\n", .{PORT}); | ||
|
||
// Starts the server, this is blocking. | ||
try server.listen(); | ||
} | ||
|
||
const Handler = struct { | ||
pub fn handle(_: *Handler, _: *httpz.Request, res: *httpz.Response) void { | ||
res.body = | ||
\\ If defined, the "handle" function is called early in httpz' request | ||
\\ processing. Routing, middlewares, not found and error handling are all skipped. | ||
\\ This is an advanced option and is used by frameworks like JetZig to provide | ||
\\ their own flavor and enhancement ontop of httpz. | ||
\\ If you define this, the special "dispatch", "notFound" and "uncaughtError" | ||
\\ functions have no meaning as far as httpz is concerned. | ||
; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const std = @import("std"); | ||
const httpz = @import("httpz"); | ||
const Logger = @import("middleware/Logger.zig"); | ||
|
||
const Allocator = std.mem.Allocator; | ||
|
||
const PORT = 8806; | ||
|
||
// This example show how to use and create middleware. There's overlap between | ||
// what you can achieve with a custom dispatch function and per-route | ||
// configuration (shown in the next example) and middleware. | ||
// see | ||
|
||
// See middleware/Logger.zig for an example of how to write a middleware | ||
|
||
pub fn main() !void { | ||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
const allocator = gpa.allocator(); | ||
|
||
var server = try httpz.Server(void).init(allocator, .{.port = PORT}, {}); | ||
defer server.deinit(); | ||
|
||
// creates an instance of the middleware with the given configuration | ||
// see example/middleware/Logger.zig | ||
const logger = try server.middleware(Logger, .{.query = true}); | ||
|
||
var router = server.router(); | ||
|
||
// Apply middleware to all routes created from this point on | ||
router.middlewares = &.{logger}; | ||
|
||
router.get("/", index, .{}); | ||
router.get("/other", other, .{.middlewares = &.{}}); | ||
|
||
std.debug.print("listening http://localhost:{d}/\n", .{PORT}); | ||
|
||
// Starts the server, this is blocking. | ||
try server.listen(); | ||
} | ||
|
||
fn index(_: *httpz.Request, res: *httpz.Response) !void { | ||
res.content_type = .HTML; | ||
res.body = | ||
\\<!DOCTYPE html> | ||
\\<p>There's overlap between a custom dispatch function and middlewares. | ||
\\<p>This page includes the example Logger middleware, so requesting it logs information. | ||
\\<p>The <a href="/other">other</a> endpoint uses a custom route config which | ||
\\ has no middleware, effectively disabling the Logger for that route. | ||
; | ||
} | ||
|
||
fn other(_: *httpz.Request, res: *httpz.Response) !void { | ||
// Called with a custom config which had no middlewares | ||
// (effectively disabling the logging middleware) | ||
res.body = | ||
\\ Accessing this endpoint will NOT generate a log line in the console, | ||
\\ because the Logger middleware is disabled. | ||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const std = @import("std"); | ||
const httpz = @import("httpz"); | ||
|
||
const Allocator = std.mem.Allocator; | ||
|
||
const PORT = 8807; | ||
|
||
// This example shows more advanced routing example, namely route groups | ||
// and route configuration. (The previous example, with middleware, also showed | ||
// per-route configuration for middleware specifically). | ||
|
||
pub fn main() !void { | ||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
const allocator = gpa.allocator(); | ||
|
||
var default_handler = Handler{ | ||
.log = true, | ||
}; | ||
|
||
var nolog_handler = Handler{ | ||
.log = false, | ||
}; | ||
|
||
var server = try httpz.Server(*Handler).init(allocator, .{.port = PORT}, &default_handler); | ||
defer server.deinit(); | ||
|
||
var router = server.router(); | ||
|
||
router.get("/", index, .{}); | ||
|
||
// We can define a dispatch function per-route. This will be used instead of Handler.dispatch | ||
// But, sadly, every dispatch method must have the same signature (they must all accept the same type of action) | ||
router.get("/page1", page1, .{.dispatcher = Handler.infoDispatch}); | ||
|
||
// We can define a handler instance per-route. This will be used instead of the | ||
// handler instance passed to the init method above. | ||
router.get("/page2", page2, .{.handler = &nolog_handler}); | ||
|
||
std.debug.print("listening http://localhost:{d}/\n", .{PORT}); | ||
|
||
// Starts the server, this is blocking. | ||
try server.listen(); | ||
} | ||
|
||
const Handler = struct { | ||
log: bool, | ||
|
||
// special dispatch set in the info route | ||
pub fn infoDispatch(h: *Handler, action: httpz.Action(*Handler), req: *httpz.Request, res: *httpz.Response) !void { | ||
return action(h, req, res); | ||
} | ||
|
||
pub fn dispatch(h: *Handler, action: httpz.Action(*Handler), req: *httpz.Request, res: *httpz.Response) !void { | ||
try action(h, req, res); | ||
if (h.log) { | ||
std.debug.print("ts={d} path={s} status={d}\n", .{std.time.timestamp(), req.url.path, res.status}); | ||
} | ||
} | ||
}; | ||
|
||
fn index(_: *Handler, _: *httpz.Request, res: *httpz.Response) !void { | ||
res.content_type = .HTML; | ||
res.body = | ||
\\<!DOCTYPE html> | ||
\\ <p>It's possible to define a custom dispatch method, custom handler instance and/or custom middleware per-route. | ||
\\ <p>It's also possible to create a route group, which is a group of routes who share a common prefix and/or a custom configration. | ||
\\ <ul> | ||
\\ <li><a href="/page1">page with custom dispatch</a> | ||
\\ <li><a href="/page2">page with custom handler</a> | ||
; | ||
} | ||
|
||
fn page1(_: *Handler, _: *httpz.Request, res: *httpz.Response) !void { | ||
// Called with a custom config which specified a custom dispatch method | ||
res.body = | ||
\\ Accessing this endpoint will NOT generate a log line in the console, | ||
\\ because a custom dispatch method is used | ||
; | ||
} | ||
|
||
fn page2(_: *Handler, _: *httpz.Request, res: *httpz.Response) !void { | ||
// Called with a custom config which specified a custom handler instance | ||
res.body = | ||
\\ Accessing this endpoint will NOT generate a log line in the console, | ||
\\ because a custom handler instance is used | ||
; | ||
} |
Oops, something went wrong.