diff --git a/README.md b/README.md index a9eb166..aa4a149 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,9 @@ Here's what works: server with optional dynamic request handling - **[hello_json](examples/hello_json/hello_json.zig)**: serves you json dependent on HTTP path +- **[endpoints](examples/endpoints/)**: a simple JSON REST API example featuring + a `/users` endpoint for adding/deleting/displaying/updating users and a + `/list` endpoint returning the entire user list. I'll continue wrapping more of facil.io's functionality and adding stuff to zap to a point where I can use it as the JSON REST API backend for my current @@ -320,3 +323,49 @@ pub fn main() !void { }); } ``` + +### [endpoints](examples/endpoints/) + +Only showing `main.zig` here: + +```zig +const std = @import("std"); +const zap = @import("zap"); +const Endpoints = @import("endpoints.zig"); + +pub fn main() !void { + const allocator = std.heap.page_allocator; + // setup listener + var listener = zap.SimpleEndpointListener.init( + allocator, + .{ + .port = 3000, + .on_request = null, + .log = true, + .public_folder = "./examples/endpoints/html", + }, + ); + + Endpoints.init(allocator, "/user", "/list"); + + // add endpoints + try listener.addEndpoint(Endpoints.getUserEndpoint()); + try listener.addEndpoint(Endpoints.getUserListEndpoint()); + + // fake some users + var uid: usize = undefined; + uid = try Endpoints.getUsers().addByName("renerocksai", null); + uid = try Endpoints.getUsers().addByName("renerocksai", "your mom"); + + // listen + try listener.listen(); + + std.debug.print("Listening on 0.0.0.0:3000\n", .{}); + + // and run + zap.start(.{ + .threads = 2, + .workers = 2, + }); +} +``` diff --git a/src/deps/facilio b/src/deps/facilio index 063c9bb..57ec8fc 160000 --- a/src/deps/facilio +++ b/src/deps/facilio @@ -1 +1 @@ -Subproject commit 063c9bb86e14bae7441d2e37ab7207d9839a98ce +Subproject commit 57ec8fc0f6df4acf45748758a40dffdffc24cba0