1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 23:24:09 +00:00

updated README

This commit is contained in:
Rene Schallner 2023-01-15 02:35:04 +01:00
parent d95f45ef34
commit cb2c3ab74b

View file

@ -29,12 +29,13 @@ Here's what works:
- **[hello_json](examples/hello_json/hello_json.zig)**: serves you json - **[hello_json](examples/hello_json/hello_json.zig)**: serves you json
dependent on HTTP path dependent on HTTP path
- **[endpoints](examples/endpoints/)**: a simple JSON REST API example featuring - **[endpoints](examples/endpoints/)**: a simple JSON REST API example featuring
a `/users` endpoint for adding/deleting/displaying/updating users and a a `/users` endpoint for PUTting/DELETE-ing/GET-ting/POST-ing users and a
`/list` endpoint returning the entire user list. `/list` endpoint returning the entire user list on GET, together with a static
HTML frontend to play around with.
I'll continue wrapping more of facil.io's functionality and adding stuff to zap 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 to a point where I can use it as the JSON REST API backend for real research
research project which is likely to need to go live in the summer of 2023. projects, serving thousands of concurrent clients.
## Getting started ## Getting started
@ -326,6 +327,10 @@ pub fn main() !void {
### [endpoints](examples/endpoints/) ### [endpoints](examples/endpoints/)
The following only shows the GET functionality implemented on both the `/user/`
and the `/list` endpoints. See [endpoints](examples/endpoints/) for a complete
example, including a HTML + JavaScript frontend.
[`main.zig`](examples/endpoints/main.zig): [`main.zig`](examples/endpoints/main.zig):
```zig ```zig
@ -377,9 +382,9 @@ pub fn main() !void {
const std = @import("std"); const std = @import("std");
const zap = @import("zap"); const zap = @import("zap");
const Users = @import("users.zig"); const Users = @import("users.zig");
const User = Users.User;
// the Endpoints // the Endpoints
pub const Self = @This(); pub const Self = @This();
var alloc: std.mem.Allocator = undefined; var alloc: std.mem.Allocator = undefined;
@ -438,7 +443,7 @@ pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
if (r.path) |path| { if (r.path) |path| {
if (userIdFromPath(path)) |id| { if (userIdFromPath(path)) |id| {
if (users.get(id)) |user| { if (users.get(id)) |user| {
if (Users.stringify(user, .{})) |json| { if (zap.stringify(user, .{})) |json| {
_ = r.sendJson(json); _ = r.sendJson(json);
} }
} }
@ -448,11 +453,11 @@ pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
pub fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { pub fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
_ = e; _ = e;
var l: std.ArrayList(Users.User) = std.ArrayList(Users.User).init(alloc); var l: std.ArrayList(User) = std.ArrayList(User).init(alloc);
if (users.list(&l)) {} else |_| { if (users.list(&l)) {} else |_| {
return; return;
} }
if (Users.stringifyUserList(&l, .{})) |maybe_json| { if (zap.stringifyArrayList(std.ArrayList(User, &l, .{})) |maybe_json| {
if (maybe_json) |json| { if (maybe_json) |json| {
_ = r.sendJson(json); _ = r.sendJson(json);
} }