prefect server in zig
1const std = @import("std");
2const zap = @import("zap");
3const mem = std.mem;
4
5fn sendJson(r: zap.Request, body: []const u8) void {
6 r.setHeader("content-type", "application/json") catch {};
7 r.setHeader("access-control-allow-origin", "*") catch {};
8 r.setHeader("access-control-allow-methods", "GET, POST, PATCH, DELETE, OPTIONS") catch {};
9 r.setHeader("access-control-allow-headers", "content-type, x-prefect-api-version") catch {};
10 r.sendBody(body) catch {};
11}
12
13pub fn health(r: zap.Request) !void {
14 // match python prefect server response
15 r.setHeader("content-type", "text/plain") catch {};
16 r.setHeader("access-control-allow-origin", "*") catch {};
17 r.sendBody("ok") catch {};
18}
19
20pub fn csrfToken(r: zap.Request) !void {
21 // prefect client expects a csrf token with client id and expiration
22 // extract client from query string: /api/csrf-token?client=...
23 const target = r.path orelse "/";
24 var client_id: []const u8 = "unknown";
25
26 if (mem.indexOf(u8, target, "client=")) |start| {
27 const rest = target[start + 7 ..];
28 if (mem.indexOf(u8, rest, "&")) |end| {
29 client_id = rest[0..end];
30 } else {
31 client_id = rest;
32 }
33 }
34
35 var buf: [512]u8 = undefined;
36 const response = std.fmt.bufPrint(&buf,
37 \\{{"token":"zig-csrf-token","client":"{s}","expiration":"2099-01-01T00:00:00Z"}}
38 , .{client_id}) catch {
39 sendJson(r, "{\"token\":\"zig-csrf-token\",\"client\":\"unknown\",\"expiration\":\"2099-01-01T00:00:00Z\"}");
40 return;
41 };
42 sendJson(r, response);
43}
44
45pub fn version(r: zap.Request) !void {
46 // return version as JSON string (FastAPI behavior)
47 // must return 3.x to match Python client major version
48 sendJson(r, "\"3.0.0\"");
49}