mirror of
https://git.robbyzambito.me/zits
synced 2026-02-04 03:34:48 +00:00
Port to latest 0.16.0
Use juicy main ;)
This commit is contained in:
@@ -83,9 +83,6 @@ pub fn build(b: *std.Build) void {
|
||||
}),
|
||||
});
|
||||
|
||||
const yazap = b.dependency("yazap", .{});
|
||||
exe.root_module.addImport("yazap", yazap.module("yazap"));
|
||||
|
||||
// This declares intent for the executable to be installed into the
|
||||
// install prefix when running `zig build` (i.e. when executing the default
|
||||
// step). By default the install prefix is `zig-out/` but can be overridden
|
||||
|
||||
@@ -31,12 +31,7 @@
|
||||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||
// Once all dependencies are fetched, `zig build` no longer requires
|
||||
// internet connectivity.
|
||||
.dependencies = .{
|
||||
.yazap = .{
|
||||
.url = "git+https://github.com/prajwalch/yazap#489439cde5ccb1abe47932d8f77a82d1772203a1",
|
||||
.hash = "yazap-0.6.3-Z1t-Eo72AQCnSJmkRYE14etZlTj1gDgPzB2PN5eeWDlw",
|
||||
},
|
||||
},
|
||||
.dependencies = .{},
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
|
||||
103
src/main.zig
103
src/main.zig
@@ -1,82 +1,61 @@
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const zits = @import("zits");
|
||||
const yazap = @import("yazap");
|
||||
|
||||
const Message = zits.MessageParser.Message;
|
||||
const Server = zits.Server;
|
||||
|
||||
const serve = @import("./subcommand/serve.zig").main;
|
||||
|
||||
pub fn main() !void {
|
||||
var dba: std.heap.DebugAllocator(.{}) = .init;
|
||||
defer _ = dba.deinit();
|
||||
const gpa = if (builtin.mode == .Debug or builtin.mode == .ReleaseSafe) dba.allocator() else std.heap.smp_allocator;
|
||||
const help =
|
||||
\\High Performance NATS compatible client and server.
|
||||
\\
|
||||
\\Commands:
|
||||
\\ serve Serve a high performance NATS compatible server
|
||||
\\ publish Publish a message over the NATS protocol
|
||||
\\ help Show this output
|
||||
\\
|
||||
\\Global Flags:
|
||||
\\ -h, --help Show context-sensitive help
|
||||
\\
|
||||
;
|
||||
|
||||
var app = yazap.App.init(gpa, "zits", "High performance NATS compatible client and server.");
|
||||
defer app.deinit();
|
||||
|
||||
var zits_app = app.rootCommand();
|
||||
|
||||
var server_cmd = app.createCommand("serve", "Run a high performance NATS compatible server.");
|
||||
try server_cmd.addArgs(&[_]yazap.Arg{
|
||||
yazap.Arg.singleValueOption(
|
||||
"addr",
|
||||
'a',
|
||||
std.fmt.comptimePrint(
|
||||
"Address to bind to (default: {s})",
|
||||
.{std.meta.fieldInfo(zits.Server.message.Message.ServerInfo, .host).defaultValue().?},
|
||||
),
|
||||
),
|
||||
yazap.Arg.singleValueOption(
|
||||
"port",
|
||||
'p',
|
||||
std.fmt.comptimePrint(
|
||||
"Port to listen on (default: {d})",
|
||||
.{std.meta.fieldInfo(zits.Server.message.Message.ServerInfo, .port).defaultValue().?},
|
||||
),
|
||||
),
|
||||
yazap.Arg.singleValueOption(
|
||||
"name",
|
||||
'n',
|
||||
"Server name (default: auto)",
|
||||
),
|
||||
});
|
||||
try zits_app.addSubcommand(server_cmd);
|
||||
|
||||
const pub_cmd = app.createCommand("pub", "Publish a message.");
|
||||
try zits_app.addSubcommand(pub_cmd);
|
||||
|
||||
var io_impl: std.Io.Threaded = .init_single_threaded;
|
||||
defer io_impl.deinit();
|
||||
const io = io_impl.io();
|
||||
|
||||
const matches = try app.parseProcess(io);
|
||||
|
||||
if (matches.subcommandMatches("serve")) |serve_matches| {
|
||||
var info: zits.Server.message.Message.ServerInfo = .{
|
||||
.server_id = zits.Server.default_id,
|
||||
.server_name = zits.Server.default_name,
|
||||
.version = "zits-master",
|
||||
.max_payload = 1048576,
|
||||
.headers = true,
|
||||
const Subcommand = enum {
|
||||
serve,
|
||||
publish,
|
||||
help,
|
||||
};
|
||||
if (serve_matches.getSingleValue("port")) |port| {
|
||||
info.port = std.fmt.parseUnsigned(@TypeOf(info.port), port, 10) catch |err| std.process.fatal("Could not parse port ({s}): {}\n", .{ port, err });
|
||||
}
|
||||
|
||||
if (serve_matches.getSingleValue("name")) |name| {
|
||||
info.server_name = name;
|
||||
}
|
||||
const to_subcommand: std.StaticStringMap(Subcommand) = .initComptime(.{
|
||||
.{ @tagName(.serve), .serve },
|
||||
.{ "srv", .serve },
|
||||
.{ @tagName(.publish), .publish },
|
||||
.{ "pub", .publish },
|
||||
.{ @tagName(.help), .help },
|
||||
});
|
||||
|
||||
try serve(gpa, info);
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
const io = init.io;
|
||||
const args = try init.minimal.args.toSlice(init.arena.allocator());
|
||||
|
||||
if (args.len == 1) {
|
||||
try std.Io.File.stdout().writeStreamingAll(io, help);
|
||||
return;
|
||||
} else if (matches.subcommandMatches("pub")) |_| {
|
||||
}
|
||||
|
||||
switch (to_subcommand.get(args[1]) orelse .help) {
|
||||
.serve => {
|
||||
try serve(init.gpa, io, args[2..]);
|
||||
return;
|
||||
},
|
||||
.publish => {
|
||||
std.debug.print("Unimplemented\n", .{});
|
||||
},
|
||||
else => {
|
||||
try std.Io.File.stdout().writeStreamingAll(io, help);
|
||||
return;
|
||||
},
|
||||
}
|
||||
|
||||
try app.displayHelp(io);
|
||||
}
|
||||
|
||||
pub const std_options: std.Options = .{
|
||||
|
||||
@@ -24,16 +24,39 @@ fn handleSigInt(sig: std.os.linux.SIG) callconv(.c) void {
|
||||
exit_lock.unlock(io);
|
||||
}
|
||||
|
||||
pub fn main(outer_alloc: Allocator, server_config: ServerInfo) !void {
|
||||
{
|
||||
var dba: DebugAllocator(.{}) = .init;
|
||||
dba.backing_allocator = outer_alloc;
|
||||
defer _ = dba.deinit();
|
||||
const alloc = if (safe_build) dba.allocator() else outer_alloc;
|
||||
pub fn main(alloc: Allocator, outer_io: Io, args: []const [:0]const u8) !void {
|
||||
io = outer_io;
|
||||
var server_config: ServerInfo = .{
|
||||
.server_id = Server.default_id,
|
||||
.server_name = Server.default_name,
|
||||
.version = "zits-master",
|
||||
.max_payload = 1048576,
|
||||
.headers = true,
|
||||
};
|
||||
|
||||
var threaded: Threaded = .init(alloc, .{});
|
||||
defer threaded.deinit();
|
||||
io = threaded.io();
|
||||
{
|
||||
var i: usize = 0;
|
||||
while (i < args.len) : (i += 1) {
|
||||
switch (to_flag.get(args[i]) orelse .help) {
|
||||
.help => {
|
||||
try std.Io.File.stdout().writeStreamingAll(io, help);
|
||||
return;
|
||||
},
|
||||
.port => {
|
||||
i += 1;
|
||||
if (args.len > i) {
|
||||
server_config.port = std.fmt.parseUnsigned(u16, args[i], 10) catch {
|
||||
std.log.err("Could not parse port: {s}", .{args[i]});
|
||||
return;
|
||||
};
|
||||
} else {
|
||||
std.log.err("Must specify port with {s}", .{args[i - 1]});
|
||||
return;
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try exit_lock.lock(io);
|
||||
|
||||
@@ -59,7 +82,24 @@ pub fn main(outer_alloc: Allocator, server_config: ServerInfo) !void {
|
||||
try exit_lock.lock(io);
|
||||
std.debug.print("\n", .{});
|
||||
std.log.info("Shutting down...", .{});
|
||||
server_task.cancel(io) catch {};
|
||||
}
|
||||
server_task.cancel(io) catch |err| switch (err) {
|
||||
error.Canceled => {},
|
||||
else => |e| std.log.err("Error shutting down: {t}", .{e}),
|
||||
};
|
||||
|
||||
std.log.info("Goodbye", .{});
|
||||
}
|
||||
|
||||
const help = "serve help\n";
|
||||
|
||||
const to_flag: std.StaticStringMap(Flag) = .initComptime(.{
|
||||
.{ "-p", .port },
|
||||
.{ "--port", .port },
|
||||
.{ "-h", .help },
|
||||
.{ "--help", .help },
|
||||
});
|
||||
|
||||
const Flag = enum {
|
||||
port,
|
||||
help,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user