diff --git a/frameworks/Zig/zap/.gitignore b/frameworks/Zig/zap/.gitignore index 170dc0f1403..3389c86c994 100644 --- a/frameworks/Zig/zap/.gitignore +++ b/frameworks/Zig/zap/.gitignore @@ -1,2 +1,2 @@ -zig-cache/**/*', -zig-out: 'zig-out/**/*', +.zig-cache/ +zig-out/ diff --git a/frameworks/Zig/zap/benchmark_config.json b/frameworks/Zig/zap/benchmark_config.json index 0b77f27d77d..8ce89ab396e 100644 --- a/frameworks/Zig/zap/benchmark_config.json +++ b/frameworks/Zig/zap/benchmark_config.json @@ -1,5 +1,6 @@ { "framework": "zap", + "maintainers": ["dragosv"], "tests": [{ "default": { "json_url": "/json", diff --git a/frameworks/Zig/zap/build.zig b/frameworks/Zig/zap/build.zig index 2da55a07981..617ea757666 100644 --- a/frameworks/Zig/zap/build.zig +++ b/frameworks/Zig/zap/build.zig @@ -1,80 +1,41 @@ const std = @import("std"); -const ModuleMap = std.StringArrayHashMap(*std.Build.Module); -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -const allocator = gpa.allocator(); -// Although this function looks imperative, note that its job is to -// declaratively construct a build graph that will be executed by an external -// runner. -pub fn build(b: *std.Build) !void { - // Standard target options allows the person running `zig build` to choose - // what target to build for. Here we do not override the defaults, which - // means any target is allowed, and the default is native. Other options - // for restricting supported target set are available. +pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); - - // Standard optimization options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do nots - // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); - const dep_opts = .{ .target = target, .optimize = optimize }; - - const exe = b.addExecutable(.{ - .name = "zap", - // In this case the main source file is merely a path, however, in more - // complicated build scripts, this could be a generated file. - .root_source_file = b.path("src/main.zig"), + const zap = b.dependency("zap", .{ .target = target, .optimize = optimize, + .openssl = false, }); - const zap = b.dependency("zap", .{ + const pg = b.dependency("pg", .{ .target = target, .optimize = optimize, - .openssl = false, // set to true to enable TLS support }); - var modules = ModuleMap.init(allocator); - defer modules.deinit(); - - const zap_module = b.dependency("zap", dep_opts).module("zap"); - const pg_module = b.dependency("pg", dep_opts).module("pg"); - - try modules.put("zap", zap_module); - try modules.put("pg", pg_module); - - // // Expose this as a module that others can import - exe.root_module.addImport("zap", zap_module); - exe.root_module.addImport("pg", pg_module); - - exe.linkLibrary(zap.artifact("facil.io")); + const exe = b.addExecutable(.{ + .name = "zap", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "zap", .module = zap.module("zap") }, + .{ .name = "pg", .module = pg.module("pg") }, + }, + }), + }); - // This declares intent for the executable to be installed into the - // standard location when the user invokes the "install" step (the default - // step when running `zig build`). b.installArtifact(exe); - // This *creates* a Run step in the build graph, to be executed when another - // step is evaluated that depends on it. The next line below will establish - // such a dependency. const run_cmd = b.addRunArtifact(exe); - - // By making the run step depend on the install step, it will be run from the - // installation directory rather than directly from within the cache directory. - // This is not necessary, however, if the application depends on other installed - // files, this ensures they will be present and in the expected location. run_cmd.step.dependOn(b.getInstallStep()); - - // This allows the user to pass arguments to the application in the build - // command itself, like this: `zig build run -- arg1 arg2 etc` if (b.args) |args| { run_cmd.addArgs(args); } - // This creates a build step. It will be visible in the `zig build --help` menu, - // and can be selected like this: `zig build run` - // This will evaluate the `run` step rather than the default, which is "install". const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); } diff --git a/frameworks/Zig/zap/build.zig.zon b/frameworks/Zig/zap/build.zig.zon index 3f492b7d954..80e2b69f948 100644 --- a/frameworks/Zig/zap/build.zig.zon +++ b/frameworks/Zig/zap/build.zig.zon @@ -1,12 +1,20 @@ -.{ .name = "Zap testing", .version = "0.1.1", .paths = .{ - "build.zig", - "build.zig.zon", - "src", -}, .dependencies = .{ - .zap = .{ - .url = "https://github.com/zigzap/zap/archive/refs/tags/v0.8.0.tar.gz", - .hash = "12209936c3333b53b53edcf453b1670babb9ae8c2197b1ca627c01e72670e20c1a21", +.{ + .name = .zap_testing, + .version = "0.2.1", + .fingerprint = 0x40157312a106e70e, + .paths = .{ + "build.zig", + "build.zig.zon", + "src", }, - .pg = .{ .url = "https://github.com/karlseguin/pg.zig/archive/239a4468163a49d8c0d03285632eabe96003e9e2.tar.gz", - .hash = "1220a1d7e51e2fa45e547c76a9e099c09d06e14b0b9bfc6baa89367f56f1ded399a0" }, -} } + .dependencies = .{ + .zap = .{ + .url = "git+https://github.com/zigzap/zap?ref=v0.11.0#66c5dc42c781bbb8a9100afda3c7e69ee96eddf3", + .hash = "zap-0.10.6-GoeB8xCEJABLgoiZjWZMMT5TsoZ5OO2EZe6j24RTUYEH", + }, + .pg = .{ + .url = "git+https://github.com/karlseguin/pg.zig?ref=master#e58b318b7867ef065b3135983f829219c5eef891", + .hash = "pg-0.0.0-Wp_7gXFoBgD0fQ72WICKa-bxLga03AXXQ3BbIsjjohQ3", + }, + }, +} diff --git a/frameworks/Zig/zap/src/endpoints.zig b/frameworks/Zig/zap/src/endpoints.zig index 44a3afc43e2..4f39049ecf2 100644 --- a/frameworks/Zig/zap/src/endpoints.zig +++ b/frameworks/Zig/zap/src/endpoints.zig @@ -2,7 +2,7 @@ const std = @import("std"); const zap = @import("zap"); const pg = @import("pg"); -const Mustache = @import("zap").Mustache; +const Mustache = zap.Mustache; const Thread = std.Thread; const Mutex = Thread.Mutex; @@ -23,7 +23,7 @@ const Fortune = struct { }; pub const FortunesEndpoint = struct { - ep: zap.Endpoint = undefined, + path: []const u8 = "/fortunes", mustache: Mustache, mutex: Mutex, @@ -34,10 +34,6 @@ pub const FortunesEndpoint = struct { const mustache = Mustache.fromData(template) catch unreachable; return .{ - .ep = zap.Endpoint.init(.{ - .path = "/fortunes", - .get = get, - }), .mustache = mustache, .mutex = Mutex{}, }; @@ -47,37 +43,30 @@ pub const FortunesEndpoint = struct { self.mustache.deinit(); } - pub fn endpoint(self: *Self) *zap.Endpoint { - return &self.ep; - } - - fn compareStrings(_: void, lhs: []const u8, rhs: []const u8) bool { - return std.mem.order(u8, lhs, rhs).compare(std.math.CompareOperator.lt); - } - fn cmpFortuneByMessage(_: void, a: Fortune, b: Fortune) bool { return std.mem.order(u8, a.message, b.message).compare(std.math.CompareOperator.lt); } fn getFortunes(pool: *pg.Pool) ![]const Fortune { + const alloc = middleware.SharedAllocator.getAllocator(); var conn = try pool.acquire(); defer conn.release(); var rows = try conn.query("SELECT id, message FROM Fortune", .{}); defer rows.deinit(); - var fortunes = std.ArrayList(Fortune).init(middleware.SharedAllocator.getAllocator()); - defer fortunes.deinit(); + var fortunes: std.ArrayListUnmanaged(Fortune) = .empty; + defer fortunes.deinit(alloc); while (try rows.next()) |row| { - const fortune = Fortune{ .id = row.get(i32, 0), .message = row.get([]const u8, 1) }; - try fortunes.append(fortune); + const fortune = Fortune{ .id = try row.get(i32, 0), .message = try row.get([]const u8, 1) }; + try fortunes.append(alloc, fortune); } const fortune = Fortune{ .id = 0, .message = "Additional fortune added at request time." }; - try fortunes.append(fortune); + try fortunes.append(alloc, fortune); - const fortunes_slice = try fortunes.toOwnedSlice(); + const fortunes_slice = try fortunes.toOwnedSlice(alloc); std.mem.sort(Fortune, fortunes_slice, {}, cmpFortuneByMessage); return fortunes_slice; @@ -92,22 +81,13 @@ pub const FortunesEndpoint = struct { self.mutex.unlock(); const raw = ret.str().?; - - // std.debug.print("mustache output {s}\n", .{raw}); - const html = try deescapeHtml(raw); - // std.debug.print("html output {s}\n", .{html}); - return html; } - pub fn get(ep: *zap.Endpoint, req: zap.Request) void { - const self: *FortunesEndpoint = @fieldParentPtr("ep", ep); - - if (!checkPath(ep, req)) return; - - req.setHeader("content-type", "text/html; charset=utf-8") catch return; + pub fn get(self: *Self, req: zap.Request) !void { + try req.setHeader("content-type", "text/html; charset=utf-8"); var pool: *pg.Pool = undefined; @@ -118,39 +98,26 @@ pub const FortunesEndpoint = struct { } } - const fortunes_html = getFortunesHtml(self, pool) catch return; + const fortunes_html = try self.getFortunesHtml(pool); - req.sendBody(fortunes_html) catch return; - - return; + try req.sendBody(fortunes_html); } }; pub const DbEndpoint = struct { - ep: zap.Endpoint = undefined, + path: []const u8 = "/db", mutex: Mutex, + const Self = @This(); pub fn init() Self { return .{ - .ep = zap.Endpoint.init(.{ - .path = "/db", - .get = get, - }), .mutex = Mutex{}, }; } - pub fn endpoint(self: *Self) *zap.Endpoint { - return &self.ep; - } - - pub fn get(ep: *zap.Endpoint, req: zap.Request) void { - const self: *DbEndpoint = @fieldParentPtr("ep", ep); - - if (!checkPath(ep, req)) return; - - req.setContentType(.JSON) catch return; + pub fn get(self: *Self, req: zap.Request) !void { + try req.setContentType(.JSON); var random_number: u32 = 0; var pool: *pg.Pool = undefined; @@ -172,17 +139,12 @@ pub const DbEndpoint = struct { return; } - const json_to_send = getJson(pool, random_number) catch |err| { - std.debug.print("Error querying database: {}\n", .{err}); - return; - }; - - req.sendBody(json_to_send) catch return; + const json_to_send = try getJson(pool, random_number); - return; + try req.sendBody(json_to_send); } - fn getJson(pool: *pg.Pool, random_number: u32) ![]const u8{ + fn getJson(pool: *pg.Pool, random_number: u32) ![]const u8 { var conn = try pool.acquire(); defer conn.release(); @@ -191,149 +153,97 @@ pub const DbEndpoint = struct { var row = row_result.?; defer row.deinit() catch {}; - const world = World{ .id = row.get(i32, 0), .randomNumber = row.get(i32, 1) }; + const world = World{ .id = try row.get(i32, 0), .randomNumber = try row.get(i32, 1) }; var buf: [100]u8 = undefined; - var json_to_send: []const u8 = undefined; - if (zap.stringifyBuf(&buf, world, .{})) |json_message| { - json_to_send = json_message; - } else { - json_to_send = "null"; - } - + const json_to_send = zap.util.stringifyBuf(&buf, world, .{}) catch return "null"; return json_to_send; } }; pub const PlaintextEndpoint = struct { - ep: zap.Endpoint = undefined, + path: []const u8 = "/plaintext", + const Self = @This(); pub fn init() Self { - return .{ - .ep = zap.Endpoint.init(.{ - .path = "/plaintext", - .get = get, - }), - }; - } - - pub fn endpoint(self: *Self) *zap.Endpoint { - return &self.ep; + return .{}; } - pub fn get(ep: *zap.Endpoint, req: zap.Request) void { - const self: *PlaintextEndpoint = @fieldParentPtr("ep", ep); - _ = self; - - if (!checkPath(ep, req)) return; - - req.setContentType(.TEXT) catch return; - - req.sendBody("Hello, World!") catch return; - return; + pub fn get(_: *Self, req: zap.Request) !void { + try req.setContentType(.TEXT); + try req.sendBody("Hello, World!"); } }; pub const JsonEndpoint = struct { - ep: zap.Endpoint = undefined, + path: []const u8 = "/json", + const Self = @This(); pub fn init() Self { - return .{ - .ep = zap.Endpoint.init(.{ - .path = "/json", - .get = get, - }), - }; - } - - pub fn endpoint(self: *Self) *zap.Endpoint { - return &self.ep; + return .{}; } - pub fn get(ep: *zap.Endpoint, req: zap.Request) void { - const self: *JsonEndpoint = @fieldParentPtr("ep", ep); - _ = self; - - if (!checkPath(ep, req)) return; - - req.setContentType(.JSON) catch return; + pub fn get(_: *Self, req: zap.Request) !void { + try req.setContentType(.JSON); const message = Message{ .message = "Hello, World!" }; var buf: [100]u8 = undefined; - var json_to_send: []const u8 = undefined; - if (zap.stringifyBuf(&buf, message, .{})) |json_message| { - json_to_send = json_message; - } else { - json_to_send = "null"; - } - - req.sendBody(json_to_send) catch return; - return; + const json_to_send = zap.util.stringifyBuf(&buf, message, .{}) catch "null"; + try req.sendBody(json_to_send); } }; -fn checkPath(ep: *zap.Endpoint, req: zap.Request) bool { - if (!std.mem.eql(u8, ep.settings.path, req.path.?)) { - // std.debug.print("Path mismatch: {s} != {s}\n", .{ ep.settings.path, req.path.? }); - - return false; - } - - // std.debug.print("Path match: {s} == {s}\n", .{ ep.settings.path, req.path.? }); - - return true; -} - fn deescapeHtml(input: []const u8) ![]const u8 { - var output = std.ArrayList(u8).init(middleware.SharedAllocator.getAllocator()); - defer output.deinit(); + const alloc = middleware.SharedAllocator.getAllocator(); + var output: std.ArrayListUnmanaged(u8) = .empty; + defer output.deinit(alloc); var i: usize = 0; while (i < input.len) { if (std.mem.startsWith(u8, input[i..], " ")) { - try output.append(' '); + try output.append(alloc, ' '); i += 5; } else if (std.mem.startsWith(u8, input[i..], """)) { - try output.append('"'); + try output.append(alloc, '"'); i += 5; } else if (std.mem.startsWith(u8, input[i..], "&")) { - try output.append('&'); + try output.append(alloc, '&'); i += 5; } else if (std.mem.startsWith(u8, input[i..], "'")) { - try output.append('\''); + try output.append(alloc, '\''); i += 5; } else if (std.mem.startsWith(u8, input[i..], "(")) { - try output.append('('); + try output.append(alloc, '('); i += 5; } else if (std.mem.startsWith(u8, input[i..], ")")) { - try output.append(')'); + try output.append(alloc, ')'); i += 5; } else if (std.mem.startsWith(u8, input[i..], "+")) { - try output.append('+'); + try output.append(alloc, '+'); i += 5; } else if (std.mem.startsWith(u8, input[i..], ",")) { - try output.append(','); + try output.append(alloc, ','); i += 5; } else if (std.mem.startsWith(u8, input[i..], ".")) { - try output.append('.'); + try output.append(alloc, '.'); i += 5; } else if (std.mem.startsWith(u8, input[i..], "/")) { - try output.append('/'); + try output.append(alloc, '/'); i += 5; } else if (std.mem.startsWith(u8, input[i..], ":")) { - try output.append(':'); + try output.append(alloc, ':'); i += 5; } else if (std.mem.startsWith(u8, input[i..], ";")) { - try output.append(';'); + try output.append(alloc, ';'); i += 5; } else { - try output.append(input[i]); + try output.append(alloc, input[i]); i += 1; } } - return output.toOwnedSlice(); + return output.toOwnedSlice(alloc); } diff --git a/frameworks/Zig/zap/src/main.zig b/frameworks/Zig/zap/src/main.zig index e2792726f04..2c1702b5faf 100644 --- a/frameworks/Zig/zap/src/main.zig +++ b/frameworks/Zig/zap/src/main.zig @@ -1,14 +1,11 @@ const std = @import("std"); -const builtin = @import("builtin"); const zap = @import("zap"); const pg = @import("pg"); -const regex = @import("regex"); const pool = @import("pool.zig"); const endpoints = @import("endpoints.zig"); const middleware = @import("middleware.zig"); -const RndGen = std.rand.DefaultPrng; const Allocator = std.mem.Allocator; const Pool = pg.Pool; @@ -16,12 +13,7 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true, }){}; - - var tsa = std.heap.ThreadSafeAllocator{ - .child_allocator = gpa.allocator(), - }; - - const allocator = tsa.allocator(); + const allocator = gpa.allocator(); var zap_port: []u8 = undefined; var arg_string = try std.fmt.allocPrint(allocator, "{s}", .{"0"}); @@ -31,8 +23,7 @@ pub fn main() !void { defer args.deinit(); while (args.next()) |arg| { arg_string = try std.fmt.allocPrint(allocator, "{s}", .{arg}); - - zap_port = arg_string; // use arg + zap_port = arg_string; } var port = try std.fmt.parseInt(u16, zap_port, 0); @@ -44,39 +35,39 @@ pub fn main() !void { var pg_pool = try pool.initPool(allocator); defer pg_pool.deinit(); - var prng = std.rand.DefaultPrng.init(@as(u64, @bitCast(std.time.milliTimestamp()))); + var prng = std.Random.DefaultPrng.init(@as(u64, @bitCast(std.time.milliTimestamp()))); middleware.SharedAllocator.init(allocator); - // create the endpoint + // create the endpoints var dbEndpoint = endpoints.DbEndpoint.init(); var plaintextEndpoint = endpoints.PlaintextEndpoint.init(); var jsonEndpoint = endpoints.JsonEndpoint.init(); var fortunesEndpoint = endpoints.FortunesEndpoint.init(); - // we wrap the endpoint with a middleware handler - var jsonEndpointHandler = zap.Middleware.EndpointHandler(middleware.Handler, middleware.Context).init( - jsonEndpoint.endpoint(), // the endpoint - null, // no other handler (we are the last in the chain) - false, // break on finish. See EndpointHandler for this. Not applicable here. + // we wrap the endpoints with middleware handlers + var jsonEndpointHandler = zap.Middleware.EndpointHandler(middleware.Handler, endpoints.JsonEndpoint, middleware.Context).init( + &jsonEndpoint, + null, + .{ .checkPath = true }, ); - var plaintextEndpointHandler = zap.Middleware.EndpointHandler(middleware.Handler, middleware.Context).init( - plaintextEndpoint.endpoint(), + var plaintextEndpointHandler = zap.Middleware.EndpointHandler(middleware.Handler, endpoints.PlaintextEndpoint, middleware.Context).init( + &plaintextEndpoint, jsonEndpointHandler.getHandler(), - false, + .{ .checkPath = true }, ); - var fortunesEndpointHandler = zap.Middleware.EndpointHandler(middleware.Handler, middleware.Context).init( - fortunesEndpoint.endpoint(), // the endpoint - plaintextEndpointHandler.getHandler(), // no other handler (we are the last in the chain) - false, + var fortunesEndpointHandler = zap.Middleware.EndpointHandler(middleware.Handler, endpoints.FortunesEndpoint, middleware.Context).init( + &fortunesEndpoint, + plaintextEndpointHandler.getHandler(), + .{ .checkPath = true }, ); - var dbEndpointHandler = zap.Middleware.EndpointHandler(middleware.Handler, middleware.Context).init( - dbEndpoint.endpoint(), // the endpoint - fortunesEndpointHandler.getHandler(), // no other handler (we are the last in the chain) - false, + var dbEndpointHandler = zap.Middleware.EndpointHandler(middleware.Handler, endpoints.DbEndpoint, middleware.Context).init( + &dbEndpoint, + fortunesEndpointHandler.getHandler(), + .{ .checkPath = true }, ); var headerHandler = middleware.HeaderMiddleWare.init(dbEndpointHandler.getHandler()); @@ -85,22 +76,19 @@ pub fn main() !void { var listener = try zap.Middleware.Listener(middleware.Context).init( .{ - .on_request = null, // must be null + .on_request = null, .port = port, .log = false, .max_clients = 100000, }, pgHandler.getHandler(), - middleware.SharedAllocator.getAllocator, ); try listener.listen(); - //const cpuCount = @as(i16, @intCast(std.Thread.getCpuCount() catch 1)); - //const workers = if (builtin.mode == .Debug) 1 else cpuCount; const threads = 128; - std.debug.print("Listening at 0.0.0.0:{d} on {d} threads\n", .{port, threads}); + std.debug.print("Listening at 0.0.0.0:{d} on {d} threads\n", .{ port, threads }); // start worker threads zap.start(.{ diff --git a/frameworks/Zig/zap/src/middleware.zig b/frameworks/Zig/zap/src/middleware.zig index 99fb9255c0c..c134ba7a5f7 100644 --- a/frameworks/Zig/zap/src/middleware.zig +++ b/frameworks/Zig/zap/src/middleware.zig @@ -7,8 +7,6 @@ pub const SharedAllocator = struct { // static var allocator: std.mem.Allocator = undefined; - const Self = @This(); - // just a convenience function pub fn init(a: std.mem.Allocator) void { allocator = a; @@ -45,12 +43,12 @@ pub const HeaderMiddleWare = struct { } // note that the first parameter is of type *Handler, not *Self !!! - pub fn onRequest(handler: *Handler, req: zap.Request, context: *Context) bool { + pub fn onRequest(handler: *Handler, req: zap.Request, context: *Context) !bool { // this is how we would get our self pointer const self: *Self = @fieldParentPtr("handler", handler); _ = self; - req.setHeader("Server", "Zap") catch return false; + try req.setHeader("Server", "Zap"); // continue in the chain return handler.handleOther(req, context); @@ -59,15 +57,15 @@ pub const HeaderMiddleWare = struct { pub const RandomMiddleWare = struct { handler: Handler, - rnd: *std.rand.DefaultPrng, + rnd: *std.Random.DefaultPrng, const Self = @This(); - const Prng = struct { - rnd: *std.rand.DefaultPrng = undefined, + pub const Prng = struct { + rnd: *std.Random.DefaultPrng = undefined, }; - pub fn init(other: ?*Handler, rnd: *std.rand.DefaultPrng) Self { + pub fn init(other: ?*Handler, rnd: *std.Random.DefaultPrng) Self { return .{ .handler = Handler.init(onRequest, other), .rnd = rnd, @@ -80,8 +78,7 @@ pub const RandomMiddleWare = struct { } // note that the first parameter is of type *Handler, not *Self !!! - pub fn onRequest(handler: *Handler, req: zap.Request, context: *Context) bool { - + pub fn onRequest(handler: *Handler, req: zap.Request, context: *Context) !bool { // this is how we would get our self pointer const self: *RandomMiddleWare = @fieldParentPtr("handler", handler); @@ -98,7 +95,7 @@ pub const PgMiddleWare = struct { const Self = @This(); - const Pg = struct { + pub const Pg = struct { pool: *pg.Pool = undefined, }; @@ -115,8 +112,7 @@ pub const PgMiddleWare = struct { } // note that the first parameter is of type *Handler, not *Self !!! - pub fn onRequest(handler: *Handler, req: zap.Request, context: *Context) bool { - + pub fn onRequest(handler: *Handler, req: zap.Request, context: *Context) !bool { // this is how we would get our self pointer const self: *Self = @fieldParentPtr("handler", handler); diff --git a/frameworks/Zig/zap/src/pool.zig b/frameworks/Zig/zap/src/pool.zig index 6615ae217ce..de18e15c510 100644 --- a/frameworks/Zig/zap/src/pool.zig +++ b/frameworks/Zig/zap/src/pool.zig @@ -1,16 +1,11 @@ const std = @import("std"); -const regex = @import("regex"); -const dns = @import("dig"); const pg = @import("pg"); const Allocator = std.mem.Allocator; const Pool = pg.Pool; -const ArrayList = std.ArrayList; -const Regex = regex.Regex; pub fn initPool(allocator: Allocator) !*pg.Pool { const info = try parsePostgresConnStr(allocator); - //std.debug.print("Connection: {s}:{s}@{s}:{d}/{s}\n", .{ info.username, info.password, info.hostname, info.port, info.database }); const pg_pool = try Pool.init(allocator, .{ .size = 28, @@ -22,8 +17,8 @@ pub fn initPool(allocator: Allocator) !*pg.Pool { .username = info.username, .database = info.database, .password = info.password, + .timeout = 10_000, }, - .timeout = 10_000, }); return pg_pool; @@ -37,30 +32,8 @@ pub const ConnectionInfo = struct { database: []const u8, }; -fn addressAsString(address: std.net.Address) ![]const u8 { - const bytes = @as(*const [4]u8, @ptrCast(&address.in.sa.addr)); - - var buffer: [256]u8 = undefined; - var source = std.io.StreamSource{ .buffer = std.io.fixedBufferStream(&buffer) }; - var writer = source.writer(); - - //try writer.writeAll("Hello, World!"); - - try writer.print("{}.{}.{}.{}", .{ - bytes[0], - bytes[1], - bytes[2], - bytes[3], - }); - - const output = source.buffer.getWritten(); - - return output; -} - fn parsePostgresConnStr(allocator: Allocator) !ConnectionInfo { const pg_port = try getEnvVar(allocator, "PG_PORT", "5432"); - // std.debug.print("tfb port {s}\n", .{pg_port}); var port = try std.fmt.parseInt(u16, pg_port, 0); if (port == 0) { diff --git a/frameworks/Zig/zap/zap.dockerfile b/frameworks/Zig/zap/zap.dockerfile index 71123f4f3e7..2a7c31572d0 100644 --- a/frameworks/Zig/zap/zap.dockerfile +++ b/frameworks/Zig/zap/zap.dockerfile @@ -1,4 +1,4 @@ -FROM fedora:40 AS build +FROM fedora:42 WORKDIR /zap @@ -11,20 +11,28 @@ ENV PG_PORT=5432 COPY src src COPY build.zig.zon build.zig.zon COPY build.zig build.zig + +RUN dnf install -y tar xz git wget nginx + +ARG ZIG_VER=0.15.2 + +RUN wget https://ziglang.org/download/${ZIG_VER}/zig-$(uname -m)-linux-${ZIG_VER}.tar.xz + +RUN tar -xvf zig-$(uname -m)-linux-${ZIG_VER}.tar.xz + +RUN mv zig-$(uname -m)-linux-${ZIG_VER} /usr/local/zig + +ENV PATH="/usr/local/zig:$PATH" COPY start-servers.sh start-servers.sh COPY build-nginx-conf.sh build-nginx-conf.sh COPY nginx.conf nginx.conf -RUN chmod +x start-servers.sh -RUN chmod +x build-nginx-conf.sh - -RUN ./build-nginx-conf.sh +RUN chmod +x start-servers.sh build-nginx-conf.sh && ./build-nginx-conf.sh -RUN dnf install -y zig nginx RUN zig version -RUN zig build -Doptimize=ReleaseFast +RUN zig build -Doptimize=ReleaseFast RUN cp /zap/zig-out/bin/zap /usr/local/bin EXPOSE 8080 -CMD ./start-servers.sh && nginx -c /zap/nginx.conf -g "daemon off;" \ No newline at end of file +CMD ./start-servers.sh && nginx -c /zap/nginx.conf -g "daemon off;"