Repository navigation
Expand file tree
/
Copy pathexample_stdlib.zig
More file actions
497 lines (419 loc) · 19.4 KB
/
Copy pathexample_stdlib.zig
File metadata and controls
497 lines (419 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// example_stdlib.zig — kitchen-sink Datastar example, stdlib only.
//
// Build: zig build stdlib
// Run: ./zig-out/bin/example_stdlib
const std = @import("std");
const datastar = @import("datastar");
const Io = std.Io;
const PORT = 8081;
pub const std_options = std.Options{ .log_level = .debug };
var update_count: std.atomic.Value(usize) = .init(1);
var prng: std.Random.DefaultPrng = .init(0);
var shared_io: Io = undefined;
fn getCountAndIncrement() usize {
return update_count.fetchAdd(1, .acq_rel);
}
var hotreload_id: u64 = 0;
fn setHotReload(io: Io) void {
const ts = Io.Clock.now(.real, io);
const seed_u96: u96 = @bitCast(ts.nanoseconds);
prng.seed(@truncate(seed_u96));
hotreload_id = prng.random().int(u64);
std.log.debug("Hotreload ID {}", .{hotreload_id});
}
pub fn main(init: std.process.Init) !void {
shared_io = init.io;
setHotReload(init.io);
const io = init.io;
const allocator = init.gpa;
const address = try Io.net.IpAddress.parseIp6("::", PORT);
var listener = try address.listen(io, .{ .reuse_address = true });
defer listener.deinit(io);
var group: Io.Group = .init;
defer group.cancel(io);
std.log.info("Datastar SDK stdlib example → http://localhost:{}/", .{PORT});
while (true) {
const conn = try listener.accept(io);
group.concurrent(io, handleConnection, .{ io, allocator, conn }) catch |err| {
std.log.err("spawn handler error: {}", .{err});
conn.close(io);
continue;
};
}
}
fn handleConnection(io: Io, allocator: std.mem.Allocator, conn: Io.net.Stream) Io.Cancelable!void {
defer conn.close(io);
var read_buffer: [4096]u8 = undefined;
var write_buffer: [4096]u8 = undefined;
var reader = conn.reader(io, &read_buffer);
var writer = conn.writer(io, &write_buffer);
var arena: std.heap.ArenaAllocator = .init(allocator);
defer arena.deinit();
while (true) {
defer _ = arena.reset(.retain_capacity);
var server = std.http.Server.init(&reader.interface, &writer.interface);
var request = server.receiveHead() catch break;
handleRequest(arena.allocator(), &request) catch |err| {
std.log.err("handler error: {}", .{err});
_ = request.respond("internal error", .{ .status = .internal_server_error }) catch {};
break;
};
}
}
// ----- Manual router -----
fn handleRequest(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const target = request.head.target;
const path = if (std.mem.indexOfScalar(u8, target, '?')) |i| target[0..i] else target;
const method = request.head.method;
// Split into segments: skip leading '/', collect parts.
var segments: [8][]const u8 = undefined;
var seg_count: usize = 0;
var it = std.mem.splitScalar(u8, path[1..], '/');
while (it.next()) |seg| : (seg_count += 1) {
if (seg_count < segments.len) segments[seg_count] = seg;
}
if (seg_count == 0 or std.mem.eql(u8, segments[0], "")) {
return serveHtml(arena, request, @embedFile("index.html"), .{
.hotreload_id = hotreload_id,
.web_server = "Zig stdlib Server",
});
}
if (std.mem.eql(u8, segments[0], "style.css") and seg_count == 1) {
return request.respond(@embedFile("style.css"), .{
.extra_headers = &.{.{ .name = "content-type", .value = "text/css; charset=UTF-8" }},
});
}
if (std.mem.eql(u8, segments[0], "text-html") and seg_count == 1) {
return textHtml(arena, request);
}
if (std.mem.eql(u8, segments[0], "patch") and seg_count == 1) {
return patchElements(arena, request);
}
if (seg_count >= 2 and std.mem.eql(u8, segments[0], "patch")) {
if (std.mem.eql(u8, segments[1], "opts")) {
if (seg_count == 2) return patchElementsOpts(arena, request);
if (seg_count == 3 and std.mem.eql(u8, segments[2], "reset")) return patchElementsOptsReset(arena, request);
}
if (std.mem.eql(u8, segments[1], "json") and seg_count == 2) return jsonSignals(arena, request);
if (std.mem.eql(u8, segments[1], "signals")) {
if (seg_count == 2) return patchSignals(arena, request);
if (seg_count == 3 and std.mem.eql(u8, segments[2], "onlymissing")) return patchSignalsOnlyIfMissing(arena, request);
if (seg_count == 4 and std.mem.eql(u8, segments[2], "remove")) return patchSignalsRemove(arena, request, segments[3]);
}
}
if (seg_count == 2 and std.mem.eql(u8, segments[0], "executescript")) {
const sample = std.fmt.parseInt(u8, segments[1], 10) catch 0;
return executeScript(arena, request, sample);
}
if (std.mem.eql(u8, segments[0], "svg-morph") and seg_count == 1) return svgMorph(arena, request);
if (std.mem.eql(u8, segments[0], "mathml-morph") and seg_count == 1) return mathMorph(arena, request);
if (seg_count == 2 and std.mem.eql(u8, segments[0], "code")) {
const snip = std.fmt.parseInt(u8, segments[1], 10) catch 1;
return code(arena, request, snip);
}
if (seg_count == 2 and std.mem.eql(u8, segments[0], "mime")) return mimeTest(arena, request, segments[1]);
if (seg_count == 2 and std.mem.eql(u8, segments[0], "hotreload")) {
const id = std.fmt.parseInt(u64, segments[1], 10) catch 0;
return hotreload(request, id);
}
_ = method;
try request.respond("not found", .{ .status = .not_found });
}
// ----- Helpers -----
fn serveHtml(arena: std.mem.Allocator, request: *std.http.Server.Request, comptime tmpl: []const u8, args: anytype) !void {
const body = try std.fmt.allocPrint(arena, tmpl, args);
try request.respond(body, .{
.extra_headers = &.{.{ .name = "content-type", .value = "text/html; charset=UTF-8" }},
});
}
fn respondSse(arena: std.mem.Allocator, request: *std.http.Server.Request, body: []const u8) !void {
_ = arena; // autofix
try request.respond(body, .{
.extra_headers = &.{
.{ .name = "content-type", .value = "text/event-stream; charset=UTF-8" },
.{ .name = "cache-control", .value = "no-cache" },
},
});
}
fn respondJson(arena: std.mem.Allocator, request: *std.http.Server.Request, value: anytype) !void {
_ = arena; // autofix
var buf: [4096]u8 = undefined;
var body = try request.respondStreaming(&buf, .{
.respond_options = .{
.extra_headers = &.{
.{ .name = "content-type", .value = "application/json" },
},
},
});
const jf = std.json.fmt(value, .{});
try jf.format(&body.writer);
try body.end();
}
// ----- Handlers -----
fn textHtml(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const body = try std.fmt.allocPrint(arena,
\\<p id="text-html">This is update number {d}</p>
, .{getCountAndIncrement()});
try request.respond(body, .{
.extra_headers = &.{.{ .name = "content-type", .value = "text/html; charset=UTF-8" }},
});
}
fn patchElements(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const block = try datastar.patchElementsFmt(arena,
\\<p id="mf-patch">This is update number {d}</p>
, .{getCountAndIncrement()}, .{});
try respondSse(arena, request, block);
}
fn patchElementsOpts(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const signals = try datastar.readSignals(struct { morph: []const u8 }, arena, request);
if (signals.morph.len < 1) return;
var patch_mode: datastar.PatchMode = .outer;
inline for (std.meta.fields(datastar.PatchMode)) |f| {
if (std.mem.eql(u8, f.name, signals.morph)) {
patch_mode = @enumFromInt(f.value);
break;
}
}
if (patch_mode == .outer or patch_mode == .inner) return;
const opts: datastar.PatchElementsOptions = .{ .selector = "#mf-patch-opts", .mode = patch_mode };
const body = switch (patch_mode) {
.replace => try datastar.patchElements(arena,
\\<p id="mf-patch-opts" class="border-4 border-error">Complete Replacement of the OUTER HTML</p>
, opts),
else => try datastar.patchElementsFmt(arena,
\\<p>This is update number {d}</p>
, .{getCountAndIncrement()}, opts),
};
try respondSse(arena, request, body);
}
fn patchElementsOptsReset(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const body = try datastar.patchElements(arena, @embedFile("index_opts.html"), .{});
try respondSse(arena, request, body);
}
fn jsonSignals(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const foo = prng.random().intRangeAtMost(u8, 0, 255);
const bar = prng.random().intRangeAtMost(u8, 0, 255);
try respondJson(arena, request, .{ .fooj = foo, .barj = bar });
}
fn patchSignals(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const foo = prng.random().intRangeAtMost(u8, 0, 255);
const bar = prng.random().intRangeAtMost(u8, 0, 255);
const body = try datastar.patchSignals(arena, .{ .foo = foo, .bar = bar }, .{});
try respondSse(arena, request, body);
}
fn patchSignalsOnlyIfMissing(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const foo = prng.random().intRangeAtMost(u8, 1, 100);
const bar = prng.random().intRangeAtMost(u8, 1, 100);
const signals_block = try datastar.patchSignals(arena, .{ .newfoo = foo, .newbar = bar }, .{ .only_if_missing = true });
const script_block = try datastar.executeScript(arena, "console.log('Patched newfoo and newbar, but only if missing');", .{});
const body = try std.mem.concat(arena, u8, &.{ signals_block, script_block });
try respondSse(arena, request, body);
}
fn patchSignalsRemove(arena: std.mem.Allocator, request: *std.http.Server.Request, names: []const u8) !void {
var json_buf: Io.Writer.Allocating = .init(arena);
try json_buf.writer.writeAll("{");
var it = std.mem.splitScalar(u8, names, ',');
var first = true;
while (it.next()) |name| {
if (!first) try json_buf.writer.writeAll(",");
try json_buf.writer.print("\"{s}\":null", .{name});
first = false;
}
try json_buf.writer.writeAll("}");
const parsed = try std.json.parseFromSliceLeaky(
std.json.Value,
arena,
json_buf.written(),
.{},
);
const body = try datastar.patchSignals(arena, parsed, .{});
try respondSse(arena, request, body);
}
fn executeScript(arena: std.mem.Allocator, request: *std.http.Server.Request, sample: u8) !void {
var attribs: datastar.ScriptAttributes = .empty;
try attribs.put(arena, "type", "text/javascript");
try attribs.put(arena, "trace", "true");
try attribs.put(arena, "aardvark", "should appear last, not first");
const body = switch (sample) {
1 => try datastar.executeScript(
arena,
"console.log('Running from executeScript() directly');",
.{},
),
2 => try datastar.executeScript(arena,
\\console.log('Multiline Script, using executeScript with a built-up payload');
\\parent = document.querySelector('#execute-script-page');
\\console.log(parent.outerHTML);
, .{ .attributes = attribs }),
3 => try datastar.executeScriptFmt(
arena,
"console.log('Using formatted print {d}');",
.{sample},
.{},
),
else => try datastar.executeScriptFmt(
arena,
"console.log('Unknown SampleID {d}');",
.{sample},
.{},
),
};
try respondSse(arena, request, body);
}
// ----- Long-lived streaming SSE -----
fn beginStream(buffer: []u8, request: *std.http.Server.Request) !std.http.BodyWriter {
var body = try request.respondStreaming(buffer, .{
.respond_options = .{
.extra_headers = &.{
.{ .name = "content-type", .value = "text/event-stream; charset=UTF-8" },
.{ .name = "cache-control", .value = "no-cache" },
},
},
});
try body.flush();
return body;
}
fn svgMorph(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const opt = try datastar.readSignals(struct { svgMorph: usize = 1 }, arena, request);
var body_buffer: [4096]u8 = undefined;
var body = try beginStream(&body_buffer, request);
var frame_buf: [4096]u8 = undefined;
var fba: std.heap.FixedBufferAllocator = .init(&frame_buf);
for (0..opt.svgMorph) |_| {
try emitSvgFrame(&body, &fba,
\\<circle id="svg-circle" cx="{}" cy="{}" r="{}" class="fill-red-500 transition-all duration-500" />
, .{ prng.random().intRangeAtMost(u8, 10, 100), prng.random().intRangeAtMost(u8, 10, 100), prng.random().intRangeAtMost(u8, 10, 80) });
try shared_io.sleep(.fromMilliseconds(100), .real);
try emitSvgFrame(&body, &fba,
\\<rect id="svg-square" x="{}" y="{}" width="{}" height="80" class="fill-green-500 transition-all duration-500" />
, .{ prng.random().intRangeAtMost(u8, 10, 100), prng.random().intRangeAtMost(u8, 10, 100), prng.random().intRangeAtMost(u8, 10, 80) });
try shared_io.sleep(.fromMilliseconds(100), .real);
try emitSvgFrame(&body, &fba,
\\<polygon id="svg-triangle" points="{},{} {},{} {},{}" class="fill-blue-500 transition-all duration-500" />
, .{
prng.random().intRangeAtMost(u16, 50, 300), prng.random().intRangeAtMost(u16, 50, 300),
prng.random().intRangeAtMost(u16, 50, 300), prng.random().intRangeAtMost(u16, 50, 300),
prng.random().intRangeAtMost(u16, 50, 300), prng.random().intRangeAtMost(u16, 50, 300),
});
try shared_io.sleep(.fromMilliseconds(100), .real);
}
try body.end();
}
fn emitSvgFrame(body: *std.http.BodyWriter, fba: *std.heap.FixedBufferAllocator, comptime fmt: []const u8, args: anytype) !void {
fba.reset();
const block = try datastar.patchElementsFmt(fba.allocator(), fmt, args, .{ .namespace = .svg });
try body.writer.writeAll(block);
try body.writer.flush();
try body.flush();
}
const mathMLs = [_][]const u8{
@embedFile("snippets/math1.html"), @embedFile("snippets/math2.html"),
@embedFile("snippets/math3.html"), @embedFile("snippets/math4.html"),
@embedFile("snippets/math5.html"), @embedFile("snippets/math6.html"),
@embedFile("snippets/math7.html"), @embedFile("snippets/math8.html"),
@embedFile("snippets/math9.html"), @embedFile("snippets/math10.html"),
@embedFile("snippets/math11.html"),
};
fn mathMorph(arena: std.mem.Allocator, request: *std.http.Server.Request) !void {
const opt = try datastar.readSignals(struct { mathmlMorph: usize = 1 }, arena, request);
if (opt.mathmlMorph == 1) {
const a = try datastar.patchElementsFmt(arena,
\\<mn id="math-factor" class="text-red-500 font-bold">{}</mn>
, .{prng.random().intRangeAtMost(u16, 2, 22)}, .{ .namespace = .mathml, .view_transition = true });
const b = try datastar.patchSignals(arena, .{ .mathmlMorph = 1 }, .{});
const body = try std.mem.concat(arena, u8, &.{ a, b });
try respondSse(arena, request, body);
return;
}
var stream_buffer: [4096]u8 = undefined;
var stream = try beginStream(&stream_buffer, request);
var frame_buf: [4096]u8 = undefined;
var fba: std.heap.FixedBufferAllocator = .init(&frame_buf);
const delay_ms: u64 = switch (mathMLs.len - 3) {
1, 2 => 2000,
3 => 1600,
4 => 1200,
else => 200,
};
for (0..opt.mathmlMorph) |_| {
fba.reset();
const r = prng.random().intRangeAtMost(u8, 1, mathMLs.len);
const block = try datastar.patchElements(fba.allocator(), mathMLs[r - 1], .{ .namespace = .mathml });
try stream.writer.writeAll(block);
try stream.writer.flush();
try stream.flush();
try shared_io.sleep(.fromMilliseconds(delay_ms), .real);
}
fba.reset();
const reset_block = try datastar.patchSignals(fba.allocator(), .{ .mathmlMorph = 1 }, .{});
try stream.writer.writeAll(reset_block);
try stream.end();
}
const snippets = [_][]const u8{
@embedFile("snippets/code1.zig"), @embedFile("snippets/code2.zig"),
@embedFile("snippets/code3.zig"), @embedFile("snippets/code4.zig"),
@embedFile("snippets/code5.zig"), @embedFile("snippets/code6.zig"),
@embedFile("snippets/code7.zig"), @embedFile("snippets/code8.zig"),
@embedFile("snippets/code9.zig"), @embedFile("snippets/code10.zig"),
@embedFile("snippets/code11.zig"),
};
fn code(arena: std.mem.Allocator, request: *std.http.Server.Request, snip: u8) !void {
if (snip < 1 or snip > snippets.len) return error.InvalidCodeSnippet;
const data = snippets[snip - 1];
var html: Io.Writer.Allocating = .init(arena);
try html.writer.writeAll("<pre><code>");
var line_it = std.mem.splitAny(u8, data, "\n");
while (line_it.next()) |line| {
try html.writer.writeAll(" ");
for (line) |c| {
switch (c) {
'<' => try html.writer.writeAll("<"),
'>' => try html.writer.writeAll(">"),
' ' => try html.writer.writeAll(" "),
else => try html.writer.writeByte(c),
}
}
try html.writer.writeAll("\n");
}
try html.writer.writeAll("</code></pre>\n");
const selector = try std.fmt.allocPrint(arena, "#code-{}", .{snip});
const body = try datastar.patchElements(arena, html.written(), .{ .selector = selector, .mode = .append });
try respondSse(arena, request, body);
}
fn mimeTest(arena: std.mem.Allocator, request: *std.http.Server.Request, filename: []const u8) !void {
const path = try std.fmt.allocPrint(arena, "examples/assets/mime-tests/{s}", .{filename});
const body = try Io.Dir.cwd().readFileAlloc(shared_io, path, arena, .limited(8 * 1024 * 1024));
const ext = std.fs.path.extension(filename);
const mime: []const u8 = if (std.mem.eql(u8, ext, ".css")) "text/css; charset=UTF-8" else if (std.mem.eql(u8, ext, ".js")) "application/javascript" else if (std.mem.eql(u8, ext, ".html") or std.mem.eql(u8, ext, ".htm")) "text/html; charset=UTF-8" else if (std.mem.eql(u8, ext, ".json")) "application/json" else "application/octet-stream";
try request.respond(body, .{
.extra_headers = &.{.{ .name = "content-type", .value = mime }},
});
}
fn hotreload(request: *std.http.Server.Request, id: u64) !void {
var stream_buffer: [4096]u8 = undefined;
var stream = try beginStream(&stream_buffer, request);
var frame_buf: [1024]u8 = undefined;
var fba: std.heap.FixedBufferAllocator = .init(&frame_buf);
if (id != hotreload_id) {
std.log.warn("Client is stale {} != {} - reload them", .{ id, hotreload_id });
const block = try datastar.executeScript(fba.allocator(), "window.location.reload()", .{});
try stream.writer.writeAll(block);
try stream.writer.flush();
try stream.flush();
return;
}
var seconds: u64 = 0;
while (true) {
try shared_io.sleep(.fromMilliseconds(60_000), .real);
seconds += 60;
fba.reset();
const ping = try std.fmt.allocPrint(fba.allocator(),
\\<keepalive data-time="{}" />
, .{seconds});
const block = try datastar.patchElements(fba.allocator(), ping, .{});
try stream.writer.writeAll(block);
try stream.writer.flush();
try stream.flush();
}
try stream.end();
}