Skip to content

Commit 238234b

Browse files
authored
Update httpz to Zig 0.15.2 (#10299)
* update httpz to zig 0.15.2 * Update Zig installation method in Dockerfile Replace curl with wget for downloading Zig and adjust installation paths. * Replace curl with wget in Dockerfile * Fix tar extraction and move commands in Dockerfile * Fix URL format for Zig download in Dockerfile * fix compile error
1 parent a020916 commit 238234b

File tree

6 files changed

+59
-55
lines changed

6 files changed

+59
-55
lines changed

frameworks/Zig/httpz/build.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ pub fn build(b: *std.Build) !void {
66

77
const dep_opts = .{ .target = target, .optimize = optimize };
88

9-
const exe = b.addExecutable(.{
10-
.name = "httpz",
11-
.root_source_file = b.path("src/main.zig"),
9+
const root_module = b.addModule("root_mod", .{
1210
.target = target,
1311
.optimize = optimize,
12+
.root_source_file = b.path("src/main.zig"),
1413
});
1514

1615
const httpz_module = b.dependency("httpz", dep_opts).module("httpz");
1716
const pg_module = b.dependency("pg", dep_opts).module("pg");
18-
const datetimez_module = b.dependency("datetimez", dep_opts).module("zig-datetime");
17+
const datetimez_module = b.dependency("datetimez", dep_opts).module("datetime");
18+
19+
const exe = b.addExecutable(.{
20+
.name = "httpz",
21+
.root_module = root_module,
22+
});
1923

2024
exe.root_module.addImport("httpz", httpz_module);
2125
exe.root_module.addImport("pg", pg_module);

frameworks/Zig/httpz/build.zig.zon

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
.{ .name = "Zap testing", .version = "0.1.1", .paths = .{
2-
"build.zig",
3-
"build.zig.zon",
4-
"src",
5-
}, .dependencies = .{
6-
.pg = .{ .url = "https://github.com/karlseguin/pg.zig/archive/239a4468163a49d8c0d03285632eabe96003e9e2.tar.gz", .hash = "1220a1d7e51e2fa45e547c76a9e099c09d06e14b0b9bfc6baa89367f56f1ded399a0" },
7-
.httpz = .{
8-
.url = "git+https://github.com/karlseguin/http.zig?ref=zig-0.13#7d2ddae87af9b110783085c0ea6b03985faa4584",
9-
.hash = "12208c1f2c5f730c4c03aabeb0632ade7e21914af03e6510311b449458198d0835d6",
1+
.{
2+
.name = .httpz_testing,
3+
.fingerprint = 0x586e7c8a23a7e7c2,
4+
.version = "0.1.1",
5+
.paths = .{
6+
"build.zig",
7+
"build.zig.zon",
8+
"src",
109
},
11-
.datetimez = .{
12-
.url = "git+https://github.com/frmdstryr/zig-datetime#70aebf28fb3e137cd84123a9349d157a74708721",
13-
.hash = "122077215ce36e125a490e59ec1748ffd4f6ba00d4d14f7308978e5360711d72d77f",
10+
.dependencies = .{
11+
.pg = .{
12+
.url = "git+https://github.com/karlseguin/pg.zig.git#f8d4892387fbad2abdf775783e101e50a7114335",
13+
.hash = "pg-0.0.0-Wp_7gag6BgD_QAZrPhNNEGpnUZR_LEkKT40Ura3p-4yX",
14+
},
15+
.httpz = .{
16+
.url = "git+https://github.com/karlseguin/http.zig.git#a4477b1dbdc5b820fd4ad301797ca6399f7eb6e4",
17+
.hash = "httpz-0.0.0-PNVzrBgtBwCVkSJyophIX6WHwDR0r8XhBGQr96Kk-1El",
18+
},
19+
.datetimez = .{
20+
.url = "git+https://github.com/frmdstryr/zig-datetime.git#3a39a21e6e34dcb0ade0ff828d0914d40ba535f3",
21+
.hash = "datetime-0.8.0-cJNXzP_YAQBxQ5hkNNP6ScnG5XsqciJmeP5RVV4xwCBA",
22+
},
1423
},
15-
} }
24+
}

frameworks/Zig/httpz/httpz.dockerfile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ COPY src src
1212
COPY build.zig.zon build.zig.zon
1313
COPY build.zig build.zig
1414

15-
ARG ZIG_VER=0.13.0
15+
ARG ZIG_VER=0.15.2
1616

17-
RUN apt-get update && apt-get install -y curl xz-utils ca-certificates
17+
RUN apt-get update && apt-get install -y wget xz-utils ca-certificates
1818

19-
RUN curl https://ziglang.org/download/${ZIG_VER}/zig-linux-$(uname -m)-${ZIG_VER}.tar.xz -o zig-linux.tar.xz && \
20-
tar xf zig-linux.tar.xz && \
21-
mv zig-linux-$(uname -m)-${ZIG_VER}/ /opt/zig
19+
RUN wget https://ziglang.org/download/${ZIG_VER}/zig-$(uname -m)-linux-${ZIG_VER}.tar.xz
2220

23-
RUN /opt/zig/zig build -Doptimize=ReleaseFast
21+
RUN tar -xvf zig-$(uname -m)-linux-${ZIG_VER}.tar.xz
22+
23+
RUN mv zig-$(uname -m)-linux-${ZIG_VER} /usr/local/zig
24+
25+
ENV PATH="/usr/local/zig:$PATH"
26+
RUN zig build -Doptimize=ReleaseFast
2427

2528
EXPOSE 3000
26-
RUN ls
2729

2830
CMD ["zig-out/bin/httpz"]

frameworks/Zig/httpz/src/endpoints.zig

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const httpz = @import("httpz");
33
const pg = @import("pg");
44
const datetimez = @import("datetimez");
55

6-
pub var date_str: []u8 = "";
6+
pub var date_str: [29]u8 = undefined;
77

88
pub const Global = struct {
99
pool: *pg.Pool,
10-
rand: *std.rand.Random,
10+
rand: *std.Random,
1111
};
1212

1313
const World = struct {
@@ -70,17 +70,7 @@ fn getWorld(pool: *pg.Pool, random_number: u32) !World {
7070
fn setHeaders(allocator: std.mem.Allocator, res: *httpz.Response) !void {
7171
res.header("Server", "Httpz");
7272

73-
//const now = datetimez.datetime.Date.now();
74-
//const time = datetimez.datetime.Time.now();
75-
76-
// Wed, 17 Apr 2013 12:00:00 GMT
77-
// Return date in ISO format YYYY-MM-DD
78-
//const TB_DATE_FMT = "{s:0>3}, {d:0>2} {s:0>3} {d:0>4} {d:0>2}:{d:0>2}:{d:0>2} GMT";
79-
//const now_str = try std.fmt.allocPrint(allocator, TB_DATE_FMT, .{ now.weekdayName()[0..3], now.day, now.monthName()[0..3], now.year, time.hour, time.minute, time.second });
80-
81-
//defer allocator.free(now_str);
82-
83-
res.header("Date", try allocator.dupe(u8, date_str));
73+
res.header("Date", try allocator.dupe(u8, &date_str));
8474
}
8575

8676
fn getFortunesHtml(allocator: std.mem.Allocator, pool: *pg.Pool) ![]const u8 {

frameworks/Zig/httpz/src/main.zig

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,40 @@ const pool = @import("pool.zig");
77

88
const endpoints = @import("endpoints.zig");
99

10-
var server: httpz.ServerCtx(*endpoints.Global, *endpoints.Global) = undefined;
10+
var server: httpz.Server(*endpoints.Global) = undefined;
1111

1212
pub fn main() !void {
1313
const cpu_count = try std.Thread.getCpuCount();
1414
var gpa = std.heap.GeneralPurposeAllocator(.{
1515
.thread_safe = true,
1616
}){};
17+
defer {
18+
if (builtin.mode == .Debug) _ = gpa.deinit();
19+
}
1720

18-
const allocator = gpa.allocator();
21+
const allocator = if (builtin.mode == .Debug) gpa.allocator() else std.heap.smp_allocator;
1922

2023
var pg_pool = try pool.initPool(allocator);
2124
defer pg_pool.deinit();
2225

2326
const date_thread = try std.Thread.spawn(.{}, struct {
2427
fn update() !void {
25-
const ally = std.heap.page_allocator;
2628
while (true) {
2729
const now = datetimez.datetime.Date.now();
2830
const time = datetimez.datetime.Time.now();
2931

3032
// Wed, 17 Apr 2013 12:00:00 GMT
3133
// Return date in ISO format YYYY-MM-DD
3234
const TB_DATE_FMT = "{s:0>3}, {d:0>2} {s:0>3} {d:0>4} {d:0>2}:{d:0>2}:{d:0>2} GMT";
33-
endpoints.date_str = try std.fmt.allocPrint(ally, TB_DATE_FMT, .{ now.weekdayName()[0..3], now.day, now.monthName()[0..3], now.year, time.hour, time.minute, time.second });
34-
std.time.sleep(std.time.ns_per_ms * 980);
35+
_ = try std.fmt.bufPrint(&endpoints.date_str, TB_DATE_FMT, .{ now.weekdayName()[0..3], now.day, now.monthName()[0..3], now.year, time.hour, time.minute, time.second });
36+
std.Thread.sleep(std.time.ns_per_ms * 980);
3537
}
3638
}
3739
}.update, .{});
3840

3941
date_thread.detach();
4042

41-
var prng = std.rand.DefaultPrng.init(@as(u64, @bitCast(std.time.milliTimestamp())));
43+
var prng: std.Random.DefaultPrng = .init(@as(u64, @bitCast(std.time.milliTimestamp())));
4244

4345
var rand = prng.random();
4446

@@ -50,7 +52,7 @@ pub fn main() !void {
5052
const port: u16 = 3000;
5153
const workers = @as(u16, @intCast(16 * cpu_count));
5254

53-
server = try httpz.ServerApp(*endpoints.Global).init(allocator, .{
55+
server = try httpz.Server(*endpoints.Global).init(allocator, .{
5456
.port = port,
5557
.address = "0.0.0.0",
5658
.workers = .{
@@ -118,24 +120,24 @@ pub fn main() !void {
118120
defer server.deinit();
119121

120122
// now that our server is up, we register our intent to handle SIGINT
121-
try std.posix.sigaction(std.posix.SIG.INT, &.{
123+
std.posix.sigaction(std.posix.SIG.INT, &.{
122124
.handler = .{ .handler = shutdown },
123-
.mask = std.posix.empty_sigset,
125+
.mask = std.posix.sigemptyset(),
124126
.flags = 0,
125127
}, null);
126128

127-
var router = server.router();
128-
router.get("/json", endpoints.json);
129-
router.get("/plaintext", endpoints.plaintext);
130-
router.get("/db", endpoints.db);
131-
router.get("/fortunes", endpoints.fortune);
129+
var router = try server.router(.{});
130+
router.get("/json", endpoints.json, .{});
131+
router.get("/plaintext", endpoints.plaintext, .{});
132+
router.get("/db", endpoints.db, .{});
133+
router.get("/fortunes", endpoints.fortune, .{});
132134

133135
std.debug.print("Httpz using {d} workers listening at 0.0.0.0:{d}\n", .{ workers, port });
134136

135137
try server.listen();
136138
}
137139

138-
fn shutdown(_: c_int) callconv(.C) void {
140+
fn shutdown(_: c_int) callconv(.c) void {
139141
server.stop();
140142
}
141143

frameworks/Zig/httpz/src/pool.zig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
const std = @import("std");
2-
const regex = @import("regex");
32
const pg = @import("pg");
43

54
const Allocator = std.mem.Allocator;
65
const Pool = pg.Pool;
7-
const ArrayList = std.ArrayList;
86

97
pub fn initPool(allocator: Allocator) !*pg.Pool {
108
const info = try parsePostgresConnStr(allocator);
11-
//std.debug.print("Connection: {s}:{s}@{s}:{d}/{s}\n", .{ info.username, info.password, info.hostname, info.port, info.database });
129

1310
const pg_pool = try Pool.init(allocator, .{
1411
.size = 56,

0 commit comments

Comments
 (0)