diff --git a/examples/endpoint/html/index.js b/examples/endpoint/html/index.js new file mode 100644 index 0000000..ec47425 --- /dev/null +++ b/examples/endpoint/html/index.js @@ -0,0 +1,143 @@ +var eL = document.getElementById("log"); +var eLt = document.getElementById("logtoggler"); + +var showLog = false; + +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, method="POST") { + json = JSON.stringify(data); + log("SENDING: " + json); + const response = fetch(slug, { + method: method, + 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, "/users", "POST") + .then((response) => response.json()) + .then((data) => { + log("SUCCESS: " + JSON.stringify(data)); + getUserList(); + }) + .catch((error) => { + log("Error posting data"); + }); +} + +function deleteUser(id) { + fetch("/users/" + id, { method: "DELETE", } ) + .then((response) => response.json()) + .then((data) => { + log("SUCCESS: " + JSON.stringify(data)); + getUserList(); + }) + .catch((error) => { + log("Error deleting data"); + }); +} + +function changeUser(id) { + const firstname = document.getElementById("first_" + id).value; + const lastname = document.getElementById("last_" + id).value; + data = { + first_name: firstname, + last_name: lastname, + } + sendJSON(data, "/users/" + id, "PATCH") + .then((response) => response.json()) + .then((data) => { + log("SUCCESS: " + JSON.stringify(data)); + getUserList(); + }) + .catch((error) => { + log("Error updating data"); + }); +} + +function addHeaderCell(tr, text) { + var th = document.createElement('TH'); + th.innerHTML = text; + tr.appendChild(th); +} + +function showTable(users) { + var t = document.getElementById("usertable"); + var new_t = document.createElement("TABLE"); + var header = new_t.createTHead(); + var tr = header.insertRow(); + // insertCell creates TD, we want TH + addHeaderCell(tr, 'id'); + addHeaderCell(tr, 'first name'); + addHeaderCell(tr, 'last name'); + addHeaderCell(tr, ''); + addHeaderCell(tr, ''); + + console.log("showTable()"); + console.log(users); + // add the data rows + for(var i=0; i'; + var c3 = row.insertCell(); + c3.innerHTML = ''; + var c4 = row.insertCell(); + c4.innerHTML = '' + + '
' + ; + var c5 = row.insertCell(); + c5.innerHTML = '' + + '
' + ; + } + console.log("before replace"); + t.innerHTML = new_t.innerHTML; + console.log("after replace"); +} + + +function getUserList() { + fetch("/users", { method: "GET", } ) + .then((response) => response.json()) + .then((data) => { + log("SUCCESS: " + JSON.stringify(data)); + showTable(data); + }) + .catch((error) => { + log("Error fetching data"); + }); +} + +function init() { + getUserList(); +} +init(); + diff --git a/examples/endpoint/main.zig b/examples/endpoint/main.zig index 31bb487..4132756 100644 --- a/examples/endpoint/main.zig +++ b/examples/endpoint/main.zig @@ -16,7 +16,7 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true, }){}; - var allocator = gpa.allocator(); + const allocator = gpa.allocator(); // we scope everything that can allocate within this block for leak detection { diff --git a/examples/endpoint/users.zig b/examples/endpoint/users.zig index 75c114e..7839657 100644 --- a/examples/endpoint/users.zig +++ b/examples/endpoint/users.zig @@ -41,12 +41,12 @@ pub fn addByName(self: *Self, first: ?[]const u8, last: ?[]const u8) !usize { user.lastnamelen = 0; if (first) |firstname| { - std.mem.copy(u8, user.firstnamebuf[0..], firstname); + std.mem.copyForwards(u8, user.firstnamebuf[0..], firstname); user.firstnamelen = firstname.len; } if (last) |lastname| { - std.mem.copy(u8, user.lastnamebuf[0..], lastname); + std.mem.copyForwards(u8, user.lastnamebuf[0..], lastname); user.lastnamelen = lastname.len; } @@ -101,11 +101,11 @@ pub fn update( pUser.firstnamelen = 0; pUser.lastnamelen = 0; if (first) |firstname| { - std.mem.copy(u8, pUser.firstnamebuf[0..], firstname); + std.mem.copyForwards(u8, pUser.firstnamebuf[0..], firstname); pUser.firstnamelen = firstname.len; } if (last) |lastname| { - std.mem.copy(u8, pUser.lastnamebuf[0..], lastname); + std.mem.copyForwards(u8, pUser.lastnamebuf[0..], lastname); pUser.lastnamelen = lastname.len; } } diff --git a/examples/endpoint/userweb.zig b/examples/endpoint/userweb.zig index ca11a6a..be6c06b 100644 --- a/examples/endpoint/userweb.zig +++ b/examples/endpoint/userweb.zig @@ -83,7 +83,7 @@ fn listUsers(self: *Self, r: zap.Request) void { fn postUser(e: *zap.Endpoint, r: zap.Request) void { const self = @fieldParentPtr(Self, "ep", e); if (r.body) |body| { - var maybe_user: ?std.json.Parsed(User) = std.json.parseFromSlice(User, self.alloc, body, .{}) catch null; + const maybe_user: ?std.json.Parsed(User) = std.json.parseFromSlice(User, self.alloc, body, .{}) catch null; if (maybe_user) |u| { defer u.deinit(); if (self._users.addByName(u.value.first_name, u.value.last_name)) |id| { @@ -105,7 +105,7 @@ fn putUser(e: *zap.Endpoint, r: zap.Request) void { if (self.userIdFromPath(path)) |id| { if (self._users.get(id)) |_| { if (r.body) |body| { - var maybe_user: ?std.json.Parsed(User) = std.json.parseFromSlice(User, self.alloc, body, .{}) catch null; + const maybe_user: ?std.json.Parsed(User) = std.json.parseFromSlice(User, self.alloc, body, .{}) catch null; if (maybe_user) |u| { defer u.deinit(); var jsonbuf: [128]u8 = undefined;