From ab42f971a0ef93d0352e589b873ad418c3fe8ee7 Mon Sep 17 00:00:00 2001 From: renerocksai Date: Sun, 16 Mar 2025 16:17:42 +0100 Subject: [PATCH] remove usingnamespace --- examples/accept/accept.zig | 2 +- examples/endpoint/userweb.zig | 14 ++++----- examples/hello_json/hello_json.zig | 2 +- src/http_auth.zig | 7 +++-- src/request.zig | 2 +- src/tests/test_http_params.zig | 2 +- src/zap.zig | 47 ++---------------------------- 7 files changed, 17 insertions(+), 59 deletions(-) diff --git a/examples/accept/accept.zig b/examples/accept/accept.zig index 477e973..55c9b53 100644 --- a/examples/accept/accept.zig +++ b/examples/accept/accept.zig @@ -41,7 +41,7 @@ fn on_request_verbose(r: zap.Request) void { }, .JSON => { var buffer: [128]u8 = undefined; - const json = zap.stringifyBuf(&buffer, .{ .message = "Hello from ZAP!!!" }, .{}) orelse return; + const json = zap.util.stringifyBuf(&buffer, .{ .message = "Hello from ZAP!!!" }, .{}) orelse return; r.sendJson(json) catch return; }, .XHTML => { diff --git a/examples/endpoint/userweb.zig b/examples/endpoint/userweb.zig index f95504a..31cc587 100644 --- a/examples/endpoint/userweb.zig +++ b/examples/endpoint/userweb.zig @@ -52,7 +52,7 @@ pub fn get(self: *Self, r: zap.Request) void { var jsonbuf: [256]u8 = undefined; if (self.userIdFromPath(path)) |id| { if (self._users.get(id)) |user| { - if (zap.stringifyBuf(&jsonbuf, user, .{})) |json| { + if (zap.util.stringifyBuf(&jsonbuf, user, .{})) |json| { r.sendJson(json) catch return; } } @@ -76,7 +76,7 @@ pub fn post(self: *Self, r: zap.Request) void { defer u.deinit(); if (self._users.addByName(u.value.first_name, u.value.last_name)) |id| { var jsonbuf: [128]u8 = undefined; - if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { + if (zap.util.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { r.sendJson(json) catch return; } } else |err| { @@ -97,11 +97,11 @@ pub fn patch(self: *Self, r: zap.Request) void { defer u.deinit(); var jsonbuf: [128]u8 = undefined; if (self._users.update(id, u.value.first_name, u.value.last_name)) { - if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { + if (zap.util.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { r.sendJson(json) catch return; } } else { - if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| { + if (zap.util.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| { r.sendJson(json) catch return; } } @@ -117,11 +117,11 @@ pub fn delete(self: *Self, r: zap.Request) void { if (self.userIdFromPath(path)) |id| { var jsonbuf: [128]u8 = undefined; if (self._users.delete(id)) { - if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { + if (zap.util.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { r.sendJson(json) catch return; } } else { - if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| { + if (zap.util.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| { r.sendJson(json) catch return; } } @@ -132,6 +132,6 @@ pub fn delete(self: *Self, r: zap.Request) void { pub fn options(_: *Self, r: zap.Request) void { r.setHeader("Access-Control-Allow-Origin", "*") catch return; r.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") catch return; - r.setStatus(zap.StatusCode.no_content); + r.setStatus(zap.http.StatusCode.no_content); r.markAsFinished(true); } diff --git a/examples/hello_json/hello_json.zig b/examples/hello_json/hello_json.zig index 9f26381..56c5cd8 100644 --- a/examples/hello_json/hello_json.zig +++ b/examples/hello_json/hello_json.zig @@ -19,7 +19,7 @@ fn on_request(r: zap.Request) void { var buf: [100]u8 = undefined; var json_to_send: []const u8 = undefined; - if (zap.stringifyBuf(&buf, user, .{})) |json| { + if (zap.util.stringifyBuf(&buf, user, .{})) |json| { json_to_send = json; } else { json_to_send = "null"; diff --git a/src/http_auth.zig b/src/http_auth.zig index ca67d88..894b74e 100644 --- a/src/http_auth.zig +++ b/src/http_auth.zig @@ -61,8 +61,9 @@ pub const AuthResult = enum { /// The authenticator handled the request that didn't pass authentication / /// authorization. /// This is used to implement authenticators that redirect to a login - /// page. An Authenticating endpoint will not do the default, which is trying - /// to call the `unauthorized` callback if one exists orelse ignore the request. + /// page. An Authenticating endpoint will not do the default, which is + /// trying to call the `unauthorized` callback. `unauthorized()` must be + /// implemented in the endpoint. Handled, }; @@ -338,7 +339,7 @@ pub const UserPassSessionArgs = struct { /// cookie max age in seconds; 0 -> session cookie cookieMaxAge: u8 = 0, /// redirect status code, defaults to 302 found - redirectCode: zap.StatusCode = .found, + redirectCode: zap.http.StatusCode = .found, }; /// UserPassSession supports the following use case: diff --git a/src/request.zig b/src/request.zig index 07050ef..a0c2a7e 100644 --- a/src/request.zig +++ b/src/request.zig @@ -495,7 +495,7 @@ pub fn getHeaderCommon(self: *const Self, which: HttpHeaderCommon) ?[]const u8 { .upgrade => fio.HTTP_HEADER_UPGRADE, }; const fiobj = zap.fio.fiobj_hash_get(self.h.*.headers, field); - return zap.fio2str(fiobj); + return zap.util.fio2str(fiobj); } /// Set header. diff --git a/src/tests/test_http_params.zig b/src/tests/test_http_params.zig index 5e8ee8e..31f8b0d 100644 --- a/src/tests/test_http_params.zig +++ b/src/tests/test_http_params.zig @@ -26,7 +26,7 @@ test "http parameters" { var strParams: ?zap.Request.HttpParamStrKVList = null; var params: ?zap.Request.HttpParamKVList = null; - var paramOneStr: ?zap.FreeOrNot = null; + var paramOneStr: ?zap.util.FreeOrNot = null; var paramOneSlice: ?[]const u8 = null; var paramSlices: zap.Request.ParamSliceIterator = undefined; diff --git a/src/zap.zig b/src/zap.zig index 8a777ef..bce9853 100644 --- a/src/zap.zig +++ b/src/zap.zig @@ -9,52 +9,10 @@ pub const fio = @import("fio.zig"); /// Server-Side TLS function wrapper pub const Tls = @import("tls.zig"); -/// Endpoint and supporting types. -/// Create one and define all callbacks. Then, pass it to a HttpListener's -/// `register()` function to register with the listener. -/// -/// **NOTE**: Endpoints must define the following: -/// -/// ```zig -/// path: []const u8, -/// pub fn get(_: *Self, _: zap.Request) void {} -/// pub fn post(_: *Self, _: zap.Request) void {} -/// pub fn put(_: *Self, _: zap.Request) void {} -/// pub fn delete(_: *Self, _: zap.Request) void {} -/// pub fn patch(_: *Self, _: zap.Request) void {} -/// pub fn options(_: *Self, _: zap.Request) void {} -/// // optional, if auth stuff is used: -/// pub fn unauthorized(_: *Self, _: zap.Request) void {} -/// ``` -/// -/// Example: -/// A simple endpoint listening on the /stop route that shuts down zap. The -/// main thread usually continues at the instructions after the call to -/// zap.start(). -/// -/// ```zig -/// const StopEndpoint = struct { -/// -/// pub fn init( path: []const u8,) StopEndpoint { -/// return .{ -/// .path = path, -/// }; -/// } -/// -/// pub fn get(self: *StopEndpoint, r: zap.Request) void { -/// _ = self; -/// _ = r; -/// zap.stop(); -/// } -/// }; -/// ``` pub const Endpoint = @import("endpoint.zig"); pub const Router = @import("router.zig"); -pub usingnamespace @import("util.zig"); -pub usingnamespace @import("http.zig"); - /// A struct to handle Mustache templating. /// /// This is a wrapper around fiobj's mustache template handling. @@ -78,9 +36,8 @@ pub const Middleware = @import("middleware.zig"); pub const WebSockets = @import("websockets.zig"); pub const Log = @import("log.zig"); -const http = @import("http.zig"); - -const util = @import("util.zig"); +pub const http = @import("http.zig"); +pub const util = @import("util.zig"); // TODO: replace with comptime debug logger like in log.zig var _debug: bool = false;