From a641dcc0827ac765d11c99e684e64e45104faa57 Mon Sep 17 00:00:00 2001 From: Ed Yu Date: Tue, 16 May 2023 14:34:47 -0700 Subject: [PATCH] add http method PATCH for endpoint listener --- examples/endpoint/endpoint.zig | 1 + examples/endpoint/html/index.html | 11 +++++------ src/endpoint.zig | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/examples/endpoint/endpoint.zig b/examples/endpoint/endpoint.zig index 6335075..b4e9f7f 100644 --- a/examples/endpoint/endpoint.zig +++ b/examples/endpoint/endpoint.zig @@ -23,6 +23,7 @@ pub fn init( .get = getUser, .post = postUser, .put = putUser, + .patch = putUser, .delete = deleteUser, }), }; diff --git a/examples/endpoint/html/index.html b/examples/endpoint/html/index.html index 9206349..9f21038 100644 --- a/examples/endpoint/html/index.html +++ b/examples/endpoint/html/index.html @@ -174,26 +174,25 @@ getUserList(); }) .catch((error) => { - log("Error posting data"); + log("Error deleting data"); }); } function changeUser(id) { - var firstname = document.getElementById("first_" + id); - firstname = firstname.value; + 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, "PUT") + sendJSON(data, "/users/" + id, "PATCH") .then((response) => response.json()) .then((data) => { log("SUCESS: " + JSON.stringify(data)); getUserList(); }) .catch((error) => { - log("Error posting data"); + log("Error updating data"); }); } @@ -249,7 +248,7 @@ showTable(data); }) .catch((error) => { - log("Error posting data"); + log("Error fetching data"); }); } diff --git a/src/endpoint.zig b/src/endpoint.zig index f84937a..499dc57 100644 --- a/src/endpoint.zig +++ b/src/endpoint.zig @@ -13,6 +13,7 @@ pub const SimpleEndpointSettings = struct { post: ?RequestFn = null, put: ?RequestFn = null, delete: ?RequestFn = null, + patch: ?RequestFn = null, /// only applicable to AuthenticatingEndpoint unauthorized: ?RequestFn = null, }; @@ -30,6 +31,7 @@ pub const SimpleEndpoint = struct { .post = s.post orelse &nop, .put = s.put orelse &nop, .delete = s.delete orelse &nop, + .patch = s.patch orelse &nop, .unauthorized = s.unauthorized orelse &nop, }, }; @@ -50,6 +52,8 @@ pub const SimpleEndpoint = struct { return self.settings.put.?(self, r); if (std.mem.eql(u8, m, "DELETE")) return self.settings.delete.?(self, r); + if (std.mem.eql(u8, m, "PATCH")) + return self.settings.patch.?(self, r); } } }; @@ -74,6 +78,7 @@ pub fn AuthenticatingEndpoint(comptime Authenticator: type) type { .post = if (e.settings.post != null) post else null, .put = if (e.settings.put != null) put else null, .delete = if (e.settings.delete != null) delete else null, + .patch = if (e.settings.patch != null) patch else null, .unauthorized = e.settings.unauthorized, }), }; @@ -161,6 +166,25 @@ pub fn AuthenticatingEndpoint(comptime Authenticator: type) type { .Handled => {}, } } + + /// here, the auth_endpoint will be passed in + pub fn patch(e: *SimpleEndpoint, r: zap.SimpleRequest) void { + const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e); + switch (authEp.authenticator.authenticateRequest(&r)) { + .AuthFailed => { + if (e.settings.unauthorized) |unauthorized| { + unauthorized(authEp.endpoint, r); + return; + } else { + r.setStatus(.unauthorized); + r.sendBody("UNAUTHORIZED") catch return; + return; + } + }, + .AuthOK => authEp.endpoint.settings.patch.?(authEp.endpoint, r), + .Handled => {}, + } + } }; }