zig library for atproto applications
at main 1.9 kB view raw
1const std = @import("std"); 2const builtin = @import("builtin"); 3const ourio = @import("ourio"); 4const atproto = @import("atproto"); 5const stda = @import("stda"); 6 7const dns = stda.net.dns; 8 9pub fn main() !void { 10 var debug_allocator: std.heap.DebugAllocator(.{}) = .init; 11 const gpa, const is_debug = gpa: { 12 break :gpa switch (builtin.mode) { 13 .Debug, .ReleaseSafe => .{ debug_allocator.allocator(), true }, 14 .ReleaseFast, .ReleaseSmall => .{ std.heap.smp_allocator, false }, 15 }; 16 }; 17 defer if (is_debug) { 18 _ = debug_allocator.deinit(); 19 }; 20 21 var io: ourio.Ring = try .init(gpa, 64); 22 defer io.deinit(); 23 24 var bundle: std.crypto.Certificate.Bundle = .{}; 25 try bundle.rescan(gpa); 26 defer bundle.deinit(gpa); 27 28 var resolver: dns.Resolver = undefined; 29 try resolver.init(gpa, &io, .{}); 30 try io.run(.until_done); 31 32 var counter: Counter = .{}; 33 34 var jetstream: atproto.Jetstream = undefined; 35 try jetstream.init(gpa, &io, bundle, .{}, &resolver, .{ .cb = print, .msg = 1, .ptr = &counter }); 36 37 _ = try io.timer(.{ .sec = 1 }, .{ .cb = print, .msg = 2, .ptr = &counter }); 38 39 try io.run(.until_done); 40} 41 42const Counter = struct { 43 tick: u8 = 0, 44 count: usize = 0, 45}; 46 47fn print(io: *ourio.Ring, task: ourio.Task) anyerror!void { 48 const counter = task.userdataCast(Counter); 49 50 switch (task.msg) { 51 1 => { 52 const result = task.result.?; 53 _ = try result.userptr; 54 counter.count += 1; 55 }, 56 2 => { 57 const writer = std.io.getStdOut().writer(); 58 try writer.writeAll("\r\x1b[2K"); 59 try writer.print("{d} events per second", .{counter.count}); 60 counter.count = 0; 61 _ = try io.timer(.{ .sec = 1 }, .{ .cb = print, .msg = 2, .ptr = counter }); 62 }, 63 else => {}, 64 } 65}