mirror of
https://github.com/zigzap/zap.git
synced 2025-10-21 07:34:08 +00:00
endpoint example: post user
This commit is contained in:
parent
b79d4d58c6
commit
bc07f3393b
4 changed files with 148 additions and 23 deletions
|
@ -1,6 +1,7 @@
|
|||
const std = @import("std");
|
||||
const zap = @import("zap");
|
||||
const Users = @import("users.zig");
|
||||
const User = Users.User;
|
||||
|
||||
// the Endpoints
|
||||
|
||||
|
@ -21,7 +22,7 @@ pub fn init(
|
|||
endpoint = zap.SimpleEndpoint.init(.{
|
||||
.path = user_path,
|
||||
.get = getUser,
|
||||
.post = null,
|
||||
.post = postUser,
|
||||
.put = null,
|
||||
.delete = null,
|
||||
});
|
||||
|
@ -72,11 +73,11 @@ fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
|||
|
||||
fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||
_ = 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 |_| {
|
||||
return;
|
||||
}
|
||||
if (zap.stringifyArrayList(Users.User, &l, .{})) |maybe_json| {
|
||||
if (zap.stringifyArrayList(User, &l, .{})) |maybe_json| {
|
||||
if (maybe_json) |json| {
|
||||
_ = r.sendJson(json);
|
||||
}
|
||||
|
@ -87,12 +88,21 @@ fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
|||
|
||||
fn postUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||
_ = e;
|
||||
if (r.path) |path| {
|
||||
if (userIdFromPath(path)) |id| {
|
||||
if (users.get(id)) |user| {
|
||||
if (zap.stringify(user, .{})) |json| {
|
||||
if (r.body) |body| {
|
||||
var stream = std.json.TokenStream.init(body);
|
||||
var maybe_user: ?User = std.json.parse(
|
||||
User,
|
||||
&stream,
|
||||
.{ .allocator = alloc },
|
||||
) catch null;
|
||||
if (maybe_user) |u| {
|
||||
defer std.json.parseFree(User, u, .{ .allocator = alloc });
|
||||
if (users.addByName(u.first_name, u.last_name)) |id| {
|
||||
if (zap.stringify(.{ .status = "OK", .id = id }, .{})) |json| {
|
||||
_ = r.sendJson(json);
|
||||
}
|
||||
} else |_| {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,120 @@
|
|||
<html lang='en'>
|
||||
<head>
|
||||
<meta charset='utf-8'>
|
||||
<meta name='viewport' content='width=device-width'>
|
||||
<title>NIM Sensor Sync</title>
|
||||
<style>
|
||||
body {background-color: #0d1117; }
|
||||
h1 {color: #cdb4db; text-align: center;}
|
||||
h2 {color: #ffafcc; text-align: center;}
|
||||
h3 {color: #ffc8dd; text-align: center; text-decoration: underline;}
|
||||
.brighter {color: #bde0fe;}
|
||||
p {color: #ffc927; padding-left: 50px; margin-right: 50px;}
|
||||
|
||||
label {color: #B0B0B0;}
|
||||
a {color: #ffc927; padding-left:15px;}
|
||||
input {color: #B06060; margin-bottom: 20px; font-weight: bold; }
|
||||
textarea {
|
||||
background-color:#181818;
|
||||
color: #cdb4db;
|
||||
font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;
|
||||
font-size: 1em;
|
||||
border-radius: 12px;
|
||||
border: 2px solid #cdb4db;
|
||||
padding: 15px;
|
||||
margin-top: 10px;
|
||||
}
|
||||
button {
|
||||
background-color: #ffafcc;
|
||||
border-radius: 12px;
|
||||
border: 2px solid #cdb4db;
|
||||
padding-left: 20px;
|
||||
padding-right: 20px;
|
||||
padding-top: 5px;
|
||||
padding-bottom: 5px;
|
||||
}
|
||||
.center {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
</style>
|
||||
<html>
|
||||
<body>
|
||||
<p><a href="/user/1">Show example user 1</a></p>
|
||||
<p><a href="/user/2">Show example user 2</a></p>
|
||||
<p><a href="/list">Show ALL users</a></p>
|
||||
<div class="center">
|
||||
<p><a href="/user/1">Show example user 1</a></p>
|
||||
<p><a href="/user/2">Show example user 2</a></p>
|
||||
<p><a href="/list">Show ALL users</a></p>
|
||||
</div>
|
||||
<form style="text-align:center">
|
||||
<div>
|
||||
<label>First name:</label><br>
|
||||
<input id="first_name"></input>
|
||||
</div>
|
||||
<div>
|
||||
<label>Last name:</label><br>
|
||||
<input id="last_name"></input>
|
||||
</div>
|
||||
<div class="center">
|
||||
<button type="button" onclick="onNewUser();">Add new user!</button>
|
||||
</div>
|
||||
</form>
|
||||
<div style="padding:20px; margin-top: 1rem;">
|
||||
<label style="margin-bottom: 10px;">Log Output:</label>
|
||||
<a id="logtoggler" onclick="toggleLog();" href="#">(hide)</a>
|
||||
<div class="center">
|
||||
<textarea id="log" style="width:80%; height: 18rem; display:block;"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
var eL = document.getElementById("log");
|
||||
var eLt = document.getElementById("logtoggler");
|
||||
|
||||
var showLog = true;
|
||||
|
||||
function toggleLog() {
|
||||
if(showLog) {
|
||||
eL.style.display = "none";
|
||||
eLt.textContent = "(show)";
|
||||
} else {
|
||||
eL.style.display = "block";
|
||||
eLt.textContent = "(hide)";
|
||||
}
|
||||
showLog = !showLog;
|
||||
}
|
||||
function log(s) {
|
||||
eL.value += s.toString();
|
||||
eL.value += "\n"
|
||||
eL.scrollTop = eL.scrollHeight;
|
||||
}
|
||||
|
||||
function sendJSON(data, slug) {
|
||||
json = JSON.stringify(data);
|
||||
log("SENDING: " + json);
|
||||
const response = fetch(slug, {
|
||||
method: "POST",
|
||||
body: json,
|
||||
headers: {
|
||||
"Content-Type": "application/json; charset=UTF-8"
|
||||
}
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
function onNewUser() {
|
||||
var first_name = document.getElementById("first_name").value;
|
||||
var last_name = document.getElementById("last_name").value;
|
||||
data = {
|
||||
first_name: first_name,
|
||||
last_name: last_name,
|
||||
}
|
||||
sendJSON(data, "/user")
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
log("SUCESS: " + JSON.stringify(data));
|
||||
})
|
||||
.catch((error) => {
|
||||
log("Error posting data");
|
||||
});}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -3,7 +3,9 @@ const std = @import("std");
|
|||
//
|
||||
// JSON helpers
|
||||
//
|
||||
var jsonbuf: [100 * 1024]u8 = undefined;
|
||||
|
||||
// 1MB JSON buffer
|
||||
var jsonbuf: [1024 * 1024]u8 = undefined;
|
||||
|
||||
pub fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 {
|
||||
var fba = std.heap.FixedBufferAllocator.init(&jsonbuf);
|
||||
|
|
24
src/zap.zig
24
src/zap.zig
|
@ -99,18 +99,18 @@ pub const SimpleRequest = struct {
|
|||
// C.fiobj_free(new_fiobj_str);
|
||||
}
|
||||
|
||||
// pub fn nextParam(self: *const Self) ?HttpParam {
|
||||
// if (self.h.*.params == 0) return null;
|
||||
// var key: C.FIOBJ = undefined;
|
||||
// const value = C.fiobj_hash_pop(self.h.*.params, &key);
|
||||
// if (value == C.FIOBJ_INVALID) {
|
||||
// return null;
|
||||
// }
|
||||
// return HttpParam{
|
||||
// .key = fio2str(key).?,
|
||||
// .value = fio2str(value).?,
|
||||
// };
|
||||
// }
|
||||
pub fn nextParam(self: *const Self) ?HttpParam {
|
||||
if (self.h.*.params == 0) return null;
|
||||
var key: C.FIOBJ = undefined;
|
||||
const value = C.fiobj_hash_pop(self.h.*.params, &key);
|
||||
if (value == C.FIOBJ_INVALID) {
|
||||
return null;
|
||||
}
|
||||
return HttpParam{
|
||||
.key = fio2str(key).?,
|
||||
.value = fio2str(value).?,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const HttpRequestFn = *const fn (r: [*c]C.http_s) callconv(.C) void;
|
||||
|
|
Loading…
Add table
Reference in a new issue