Skip to content

Commit ba69235

Browse files
authored
Zap upgraded to 0.8 (#9254)
* Zap upgraded to 0.8 * Zap 0.8 fixes
1 parent 4966387 commit ba69235

File tree

8 files changed

+128
-127
lines changed

8 files changed

+128
-127
lines changed

frameworks/Zig/zap/build.zig

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
const std = @import("std");
2+
const ModuleMap = std.StringArrayHashMap(*std.Build.Module);
3+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
4+
const allocator = gpa.allocator();
25

36
// Although this function looks imperative, note that its job is to
47
// declaratively construct a build graph that will be executed by an external
58
// runner.
6-
pub fn build(b: *std.Build) void {
9+
pub fn build(b: *std.Build) !void {
710
// Standard target options allows the person running `zig build` to choose
811
// what target to build for. Here we do not override the defaults, which
912
// means any target is allowed, and the default is native. Other options
@@ -15,41 +18,38 @@ pub fn build(b: *std.Build) void {
1518
// set a preferred release mode, allowing the user to decide how to optimize.
1619
const optimize = b.standardOptimizeOption(.{});
1720

21+
const dep_opts = .{ .target = target, .optimize = optimize };
22+
1823
const exe = b.addExecutable(.{
1924
.name = "zap",
2025
// In this case the main source file is merely a path, however, in more
2126
// complicated build scripts, this could be a generated file.
22-
.root_source_file = .{ .path = "src/main.zig" },
27+
.root_source_file = b.path("src/main.zig"),
2328
.target = target,
2429
.optimize = optimize,
2530
});
2631

27-
//exe.addPackagePath("random", "src/random.zig");
28-
2932
const zap = b.dependency("zap", .{
3033
.target = target,
3134
.optimize = optimize,
3235
.openssl = false, // set to true to enable TLS support
3336
});
34-
exe.addModule("zap", zap.module("zap"));
3537

36-
const pg = b.dependency("pg", .{
37-
.target = target,
38-
.optimize = optimize,
39-
});
40-
exe.addModule("pg", pg.module("pg"));
38+
var modules = ModuleMap.init(allocator);
39+
defer modules.deinit();
4140

42-
const dig = b.dependency("dig", .{
43-
.target = target,
44-
.optimize = optimize,
45-
});
46-
exe.addModule("dns", dig.module("dns"));
41+
const zap_module = b.dependency("zap", dep_opts).module("zap");
42+
const pg_module = b.dependency("pg", dep_opts).module("pg");
43+
const dig_module = b.dependency("dig", dep_opts).module("dns");
44+
45+
try modules.put("zap", zap_module);
46+
try modules.put("pg", pg_module);
47+
try modules.put("dig", dig_module);
4748

48-
// const mustache = b.dependency("mustache", .{
49-
// .target = target,
50-
// .optimize = optimize,
51-
// });
52-
// exe.addModule("mustache", mustache.module("mustache"));
49+
// // Expose this as a module that others can import
50+
exe.root_module.addImport("zap", zap_module);
51+
exe.root_module.addImport("pg", pg_module);
52+
exe.root_module.addImport("dig", dig_module);
5353

5454
exe.linkLibrary(zap.artifact("facil.io"));
5555

@@ -84,7 +84,7 @@ pub fn build(b: *std.Build) void {
8484
// Creates a step for unit testing. This only builds the test executable
8585
// but does not run it.
8686
const unit_tests = b.addTest(.{
87-
.root_source_file = .{ .path = "src/main.zig" },
87+
.root_source_file = b.path("src/main.zig"),
8888
.target = target,
8989
.optimize = optimize,
9090
});

frameworks/Zig/zap/build.zig.zon

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
.{
2-
.name = "Zap testing",
3-
.version = "0.1.0",
4-
5-
.dependencies = .{
6-
// zap v0.5.1
7-
.zap = .{
8-
.url = "https://github.com/zigzap/zap/archive/refs/tags/v0.5.1.tar.gz",
9-
.hash = "1220d4802fb09d4e99c0e7265f90d6f3cfdc3e5e31c1b05f0924ee2dd26d9d6dbbf4",
10-
},
11-
.pg = .{
12-
.url = "https://github.com/karlseguin/pg.zig/archive/f3f4a0b3b9996bfb1bf9bd0bdd0d73b36e915a86.tar.gz",
13-
.hash = "1220337202642ee66408a35f254549f22cf3a096c6fa6c28e6f87a0161d5a6c0f4ab"
14-
},
15-
.dig = .{
16-
.url = "https://github.com/lun-4/zigdig/archive/2ec407ec3c7f347e747717977958e9ba339eb82f.tar.gz",
17-
.hash = "1220dfdb3089dfe9a4e4bc1226fcff08d91d0c0853f287d98d8b81270da251790331"
18-
},
19-
20-
}
21-
}
1+
.{ .name = "Zap testing", .version = "0.1.1", .paths = .{
2+
"build.zig",
3+
"build.zig.zon",
4+
"src",
5+
}, .dependencies = .{
6+
.zap = .{
7+
.url = "https://github.com/zigzap/zap/archive/refs/tags/v0.8.0.tar.gz",
8+
.hash = "12209936c3333b53b53edcf453b1670babb9ae8c2197b1ca627c01e72670e20c1a21",
9+
},
10+
.pg = .{ .url = "https://github.com/karlseguin/pg.zig/archive/239a4468163a49d8c0d03285632eabe96003e9e2.tar.gz",
11+
.hash = "1220a1d7e51e2fa45e547c76a9e099c09d06e14b0b9bfc6baa89367f56f1ded399a0" },
12+
.dig = .{ .url = "https://github.com/lun-4/zigdig/archive/a54c85c26aa83c64ee81e3ee1818890be5cbed0b.tar.gz", .hash = "1220f078ab62d1328339504f9122dc4d241be30ada451628d78b8a3bf5bb9be1dcba" },
13+
} }

frameworks/Zig/zap/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
echo "Waiting for ZAP to start..."
1+
echo "Waiting for ZAP framework to start..."
22

33
zap

frameworks/Zig/zap/src/endpoints.zig

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,27 @@ pub const FortunesEndpoint = struct {
6464
defer conn.release();
6565

6666
var rows = try conn.query("SELECT id, message FROM Fortune", .{});
67-
rows.deinit();
67+
defer rows.deinit();
6868

6969
var fortunes = std.ArrayList(Fortune).init(middleware.SharedAllocator.getAllocator());
7070
defer fortunes.deinit();
7171

7272
while (try rows.next()) |row| {
73-
var fortune = Fortune{ .id = row.get(i32, 0), .message = row.get([]const u8, 1) };
74-
_ = try fortunes.append(fortune);
73+
const fortune = Fortune{ .id = row.get(i32, 0), .message = row.get([]const u8, 1) };
74+
try fortunes.append(fortune);
7575
}
7676

77-
var fortune = Fortune{ .id = 0, .message = "Additional fortune added at request time." };
78-
_ = try fortunes.append(fortune);
77+
const fortune = Fortune{ .id = 0, .message = "Additional fortune added at request time." };
78+
try fortunes.append(fortune);
7979

80-
var fortunes_slice = try fortunes.toOwnedSlice();
80+
const fortunes_slice = try fortunes.toOwnedSlice();
8181
std.mem.sort(Fortune, fortunes_slice, {}, cmpFortuneByMessage);
8282

8383
return fortunes_slice;
8484
}
8585

8686
fn getFortunesHtml(self: *Self, pool: *pg.Pool) ![]const u8 {
87-
var fortunes = try getFortunes(pool);
87+
const fortunes = try getFortunes(pool);
8888

8989
self.mutex.lock();
9090
const ret = self.mustache.build(.{ .fortunes = fortunes });
@@ -95,15 +95,15 @@ pub const FortunesEndpoint = struct {
9595

9696
// std.debug.print("mustache output {s}\n", .{raw});
9797

98-
var html = try deescapeHtml(raw);
98+
const html = try deescapeHtml(raw);
9999

100100
// std.debug.print("html output {s}\n", .{html});
101101

102102
return html;
103103
}
104104

105105
pub fn get(ep: *zap.Endpoint, req: zap.Request) void {
106-
const self = @fieldParentPtr(Self, "ep", ep);
106+
const self: *FortunesEndpoint = @fieldParentPtr("ep", ep);
107107

108108
if (!checkPath(ep, req)) return;
109109

@@ -118,7 +118,7 @@ pub const FortunesEndpoint = struct {
118118
}
119119
}
120120

121-
var fortunes_html = getFortunesHtml(self, pool) catch return;
121+
const fortunes_html = getFortunesHtml(self, pool) catch return;
122122

123123
req.sendBody(fortunes_html) catch return;
124124

@@ -146,7 +146,7 @@ pub const DbEndpoint = struct {
146146
}
147147

148148
pub fn get(ep: *zap.Endpoint, req: zap.Request) void {
149-
const self = @fieldParentPtr(Self, "ep", ep);
149+
const self: *DbEndpoint = @fieldParentPtr("ep", ep);
150150

151151
if (!checkPath(ep, req)) return;
152152

@@ -177,14 +177,13 @@ pub const DbEndpoint = struct {
177177
var conn = pool.acquire() catch return;
178178
defer conn.release();
179179

180-
var row_result = conn.row("SELECT id, randomNumber FROM World WHERE id = $1", .{random_number}) catch |err| {
180+
const row_result = conn.row("SELECT id, randomNumber FROM World WHERE id = $1", .{random_number}) catch |err| {
181181
std.debug.print("Error querying database: {}\n", .{err});
182182
return;
183183
};
184184
var row = row_result.?;
185-
defer row.deinit();
186185

187-
var world = World{ .id = row.get(i32, 0), .randomNumber = row.get(i32, 1) };
186+
const world = World{ .id = row.get(i32, 0), .randomNumber = row.get(i32, 1) };
188187

189188
var buf: [100]u8 = undefined;
190189
var json_to_send: []const u8 = undefined;
@@ -218,7 +217,7 @@ pub const PlaintextEndpoint = struct {
218217
}
219218

220219
pub fn get(ep: *zap.Endpoint, req: zap.Request) void {
221-
const self = @fieldParentPtr(Self, "ep", ep);
220+
const self: *PlaintextEndpoint = @fieldParentPtr("ep", ep);
222221
_ = self;
223222

224223
if (!checkPath(ep, req)) return;
@@ -248,14 +247,14 @@ pub const JsonEndpoint = struct {
248247
}
249248

250249
pub fn get(ep: *zap.Endpoint, req: zap.Request) void {
251-
const self = @fieldParentPtr(Self, "ep", ep);
250+
const self: *JsonEndpoint = @fieldParentPtr("ep", ep);
252251
_ = self;
253252

254253
if (!checkPath(ep, req)) return;
255254

256255
req.setContentType(.JSON) catch return;
257256

258-
var message = Message{ .message = "Hello, World!" };
257+
const message = Message{ .message = "Hello, World!" };
259258

260259
var buf: [100]u8 = undefined;
261260
var json_to_send: []const u8 = undefined;

frameworks/Zig/zap/src/main.zig

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@ pub fn main() !void {
2121
.child_allocator = gpa.allocator(),
2222
};
2323

24-
var allocator = tsa.allocator();
24+
const allocator = tsa.allocator();
2525

2626
var pg_pool = try pool.initPool(allocator);
2727
defer pg_pool.deinit();
2828

29-
var rnd = std.rand.DefaultPrng.init(blk: {
30-
var seed: u64 = undefined;
31-
try std.os.getrandom(std.mem.asBytes(&seed));
32-
break :blk seed;
33-
});
29+
var prng = std.rand.DefaultPrng.init(@as(u64, @bitCast(std.time.milliTimestamp())));
3430

3531
middleware.SharedAllocator.init(allocator);
3632

@@ -66,7 +62,7 @@ pub fn main() !void {
6662
);
6763

6864
var headerHandler = middleware.HeaderMiddleWare.init(dbEndpointHandler.getHandler());
69-
var prngHandler = middleware.PrngMiddleWare.init(headerHandler.getHandler(), &rnd);
65+
var prngHandler = middleware.RandomMiddleWare.init(headerHandler.getHandler(), &prng);
7066
var pgHandler = middleware.PgMiddleWare.init(prngHandler.getHandler(), pg_pool);
7167

7268
var listener = try zap.Middleware.Listener(middleware.Context).init(
@@ -92,4 +88,3 @@ pub fn main() !void {
9288
.workers = 1,
9389
});
9490
}
95-

frameworks/Zig/zap/src/middleware.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub const SharedAllocator = struct {
2222

2323
// create a combined context struct
2424
pub const Context = struct {
25-
prng: ?PrngMiddleWare.Prng = null,
25+
prng: ?RandomMiddleWare.Prng = null,
2626
pg: ?PgMiddleWare.Pg = null,
2727
};
2828

@@ -47,7 +47,7 @@ pub const HeaderMiddleWare = struct {
4747
// note that the first parameter is of type *Handler, not *Self !!!
4848
pub fn onRequest(handler: *Handler, req: zap.Request, context: *Context) bool {
4949
// this is how we would get our self pointer
50-
var self = @fieldParentPtr(Self, "handler", handler);
50+
const self: *Self = @fieldParentPtr("handler", handler);
5151
_ = self;
5252

5353
req.setHeader("Server", "Zap") catch return false;
@@ -57,7 +57,7 @@ pub const HeaderMiddleWare = struct {
5757
}
5858
};
5959

60-
pub const PrngMiddleWare = struct {
60+
pub const RandomMiddleWare = struct {
6161
handler: Handler,
6262
rnd: *std.rand.DefaultPrng,
6363

@@ -83,7 +83,7 @@ pub const PrngMiddleWare = struct {
8383
pub fn onRequest(handler: *Handler, req: zap.Request, context: *Context) bool {
8484

8585
// this is how we would get our self pointer
86-
var self = @fieldParentPtr(Self, "handler", handler);
86+
const self: *RandomMiddleWare = @fieldParentPtr("handler", handler);
8787

8888
context.prng = Prng{ .rnd = self.rnd };
8989

@@ -118,7 +118,7 @@ pub const PgMiddleWare = struct {
118118
pub fn onRequest(handler: *Handler, req: zap.Request, context: *Context) bool {
119119

120120
// this is how we would get our self pointer
121-
var self = @fieldParentPtr(Self, "handler", handler);
121+
const self: *Self = @fieldParentPtr("handler", handler);
122122

123123
// do our work: fill in the user field of the context
124124
context.pg = Pg{ .pool = self.pool };

0 commit comments

Comments
 (0)