srctree

Gregory Mullen parent c7d67d60 446985fc
simplfy server init

examples/basic.zig added: 19, removed: 16, total 3
@@ -12,7 +12,7 @@ pub fn main() !void {
defer _ = gpa.deinit();
const alloc = gpa.allocator();
 
var server = try Verse.Server.init(alloc, .http, .{ .routefn = route }, .{ .http = .{ .port = 8080 } });
var server = try Verse.Server.init(alloc, .{ .http = .{ .port = 8080 } }, .{ .routefn = route });
 
server.serve() catch |err| {
std.debug.print("error: {any}", .{err});
 
src/server.zig added: 19, removed: 16, total 3
@@ -11,29 +11,32 @@ pub const Http = @import("http.zig");
 
alloc: Allocator,
router: Router,
interface: union(RunMode) {
unix: zWSGI,
http: Http,
other: void,
},
interface: Interface,
 
pub const RunMode = enum {
unix,
zwsgi,
http,
other,
};
 
pub const Options = struct {
zwsgi: zWSGI.Options = .{},
http: Http.Options = .{},
pub const Interface = union(RunMode) {
zwsgi: zWSGI,
http: Http,
other: void,
};
 
pub fn init(a: Allocator, runmode: RunMode, router: Router, opts: Options) !Server {
pub const Options = union(RunMode) {
zwsgi: zWSGI.Options,
http: Http.Options,
other: void,
};
 
pub fn init(a: Allocator, opts: Options, router: Router) !Server {
return .{
.alloc = a,
.router = router,
.interface = switch (runmode) {
.unix => .{ .unix = zWSGI.init(a, opts.zwsgi, router) },
.interface = switch (opts) {
.zwsgi => .{ .zwsgi = zWSGI.init(a, opts.zwsgi, router) },
.http => .{ .http = try Http.init(a, opts.http, router) },
.other => unreachable,
},
@@ -42,7 +45,7 @@ pub fn init(a: Allocator, runmode: RunMode, router: Router, opts: Options) !Serv
 
pub fn serve(srv: *Server) !void {
switch (srv.interface) {
.unix => |*zw| try zw.serve(),
.zwsgi => |*zw| try zw.serve(),
.http => |*ht| try ht.serve(),
else => {},
}