srctree

Gregory Mullen parent 4ee7c385 88f01a5c
flatten out the api for the required options

README.md added: 65, removed: 79, total 0
@@ -22,12 +22,12 @@ fn index(frame: *Frame) !void {
try frame.sendPage(&page);
}
 
const routes = []Match{
const routes = Routes(&[_]Match{
GET("index.html", index),
};
});
 
pub fn main() !void {
var srv = Server.init(default_allocator, .{ .mode = .{ .zwsgi = .{} });
var srv = Server.init(default_allocator, routes, .{ .mode = .{ .zwsgi = .{} });
srv.serve() catch |err| switch (err) {
else => std.debug.print("Server Error: {}\n", .{err});
};
 
examples/auth-cookie.zig added: 65, removed: 79, total 0
@@ -56,9 +56,8 @@ pub fn main() !void {
});
 
// Step 2: Start a normal Verse Server with an authentication Provider
var server = try verse.Server.init(std.heap.page_allocator, .{
var server = try verse.Server.init(std.heap.page_allocator, routes, .{
.mode = .{ .http = .{ .port = 8089 } },
.router = .{ .routefn = route },
.auth = cookie_auth.provider(),
});
 
@@ -118,14 +117,10 @@ fn create(frame: *Frame) Router.Error!void {
try frame.redirect("/", .found);
}
 
const routes = [_]Router.Match{
const routes = Router.Routes(&[_]Router.Match{
Router.GET("", index),
Router.GET("create", create),
};
 
fn route(frame: *verse.Frame) !BuildFn {
return Router.router(frame, &routes);
}
});
 
const std = @import("std");
const verse = @import("verse");
 
examples/basic.zig added: 65, removed: 79, total 0
@@ -1,20 +1,17 @@
const std = @import("std");
const verse = @import("verse");
const Router = verse.Router;
const BuildFn = Router.BuildFn;
//! The quickest way to start a verse server
//!
 
const routes = [_]Router.Match{
const routes = Router.Routes(&[_]Router.Match{
Router.GET("", index),
};
});
 
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
 
var server = try verse.Server.init(alloc, .{
.mode = .{ .http = .{ .port = 8080 } },
.router = .{ .routefn = route },
var server = try verse.Server.init(alloc, routes, .{
.mode = .{ .http = .{} },
});
 
server.serve() catch |err| {
@@ -23,10 +20,11 @@ pub fn main() !void {
};
}
 
fn route(frame: *verse.Frame) !BuildFn {
return Router.router(frame, &routes);
}
 
fn index(frame: *verse.Frame) Router.Error!void {
try frame.sendHTML(.ok, "hello world");
}
 
const std = @import("std");
const verse = @import("verse");
const Router = verse.Router;
const BuildFn = Router.BuildFn;
 
examples/cookies.zig added: 65, removed: 79, total 0
@@ -8,18 +8,17 @@ const Cookie = verse.Cookie;
var Random = std.Random.DefaultPrng.init(1337);
var random = Random.random();
 
const routes = [_]Router.Match{
const routes = Router.Routes(&[_]Router.Match{
Router.GET("", index),
};
});
 
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
 
var server = try verse.Server.init(alloc, .{
var server = try verse.Server.init(alloc, routes, .{
.mode = .{ .http = .{ .port = 8081 } },
.router = .{ .routefn = route },
});
 
server.serve() catch |err| {
@@ -28,10 +27,6 @@ pub fn main() !void {
};
}
 
fn route(frame: *verse.Frame) !BuildFn {
return Router.router(frame, &routes);
}
 
fn index(frame: *verse.Frame) Router.Error!void {
var buffer: [2048]u8 = undefined;
const found = try print(&buffer, "{} cookies found by the server\n", .{frame.request.cookie_jar.cookies.items.len});
 
examples/template.zig added: 65, removed: 79, total 0
@@ -4,18 +4,17 @@ const PageData = verse.template.PageData;
const Router = verse.Router;
const BuildFn = Router.BuildFn;
 
const routes = [_]Router.Match{
const routes = Router.Routes(&[_]Router.Match{
Router.GET("", index),
};
});
 
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
 
var server = try verse.Server.init(alloc, .{
var server = try verse.Server.init(alloc, routes, .{
.mode = .{ .http = .{ .port = 8082 } },
.router = .{ .routefn = route },
});
 
server.serve() catch |err| {
@@ -24,10 +23,6 @@ pub fn main() !void {
};
}
 
fn route(frame: *verse.Frame) !BuildFn {
return Router.router(frame, &routes);
}
 
// This page template is compiled/prepared at comptime.
const ExamplePage = PageData("templates/example.html");
 
 
src/endpoint.zig added: 65, removed: 79, total 0
@@ -34,16 +34,11 @@ pub fn Endpoints(endpoints: anytype) type {
}
 
pub fn serve(self: *Self, options: Options) !void {
var server = try Server.init(self.alloc, .{
var server = try Server.init(self.alloc, Router.Routes(&routes), .{
.mode = options.mode,
.router = .{ .routefn = route },
});
try server.serve();
}
 
pub fn route(frame: *Frame) Router.RoutingError!Router.BuildFn {
return Router.router(frame, &routes);
}
};
}
 
 
src/http.zig added: 65, removed: 79, total 0
@@ -19,10 +19,10 @@ pub const Options = struct {
port: u16 = 8080,
};
 
pub fn init(a: Allocator, opts: Options, sopts: VServer.Options) !HTTP {
pub fn init(a: Allocator, router: Router, opts: Options, sopts: VServer.Options) !HTTP {
return .{
.alloc = a,
.router = sopts.router,
.router = router,
.auth = sopts.auth,
.listen_addr = try std.net.Address.parseIp(opts.host, opts.port),
};
@@ -103,7 +103,8 @@ fn threadFn(server: *HTTP) void {
 
test HTTP {
const a = std.testing.allocator;
var server = try init(a, .{ .port = 9345 }, .{ .router = .{ .routefn = Router.testingRouter } });
 
var server = try init(a, Router.TestingRouter, .{ .port = 9345 }, .{});
var thread = try std.Thread.spawn(.{}, threadFn, .{&server});
 
var client = std.http.Client{ .allocator = a };
 
src/router.zig added: 65, removed: 79, total 0
@@ -1,6 +1,6 @@
routefn: RouteFn,
builderfn: BuilderFn = defaultBuilder,
routerfn: RouterFn = defaultRouter,
builderfn: BuilderFn,
routerfn: RouterFn,
 
/// TODO document
const Router = @This();
@@ -81,8 +81,19 @@ pub const Target = union(enum) {
simple: []const Match,
};
 
/// Translation function to convert a tuple of Match objects into
pub fn ROUTER() void {}
/// Builds a default router given an array of matches.
pub fn Routes(comptime routes: []const Match) Router {
const routefn = struct {
pub fn r(f: *Frame) RoutingError!BuildFn {
return router(f, routes);
}
};
return .{
.routefn = routefn.r,
.builderfn = defaultBuilder,
.routerfn = defaultRouter,
};
}
 
/// Default route building helper.
pub fn ROUTE(comptime name: []const u8, comptime match: anytype) Match {
@@ -377,9 +388,7 @@ fn defaultRouterHtml(frame: *Frame, routefn: RouteFn) Error!void {
return internalServerError;
}
 
pub fn testingRouter(frame: *Frame) RoutingError!BuildFn {
return router(frame, &root);
}
pub const TestingRouter: Router = Routes(&root);
 
test "uri" {
const uri_file = "/root/first/second/third";
 
src/server.zig added: 65, removed: 79, total 0
@@ -24,17 +24,16 @@ pub const Interface = union(RunModes) {
 
pub const Options = struct {
mode: RunMode = .{ .http = .{} },
router: Router,
auth: Auth.Provider = Auth.InvalidAuth.provider(),
};
 
pub fn init(a: Allocator, opts: Options) !Server {
pub fn init(a: Allocator, router: Router, opts: Options) !Server {
return .{
.alloc = a,
.router = opts.router,
.router = router,
.interface = switch (opts.mode) {
.zwsgi => |z| .{ .zwsgi = zWSGI.init(a, z, opts) },
.http => |h| .{ .http = try Http.init(a, h, opts) },
.zwsgi => |z| .{ .zwsgi = zWSGI.init(a, router, z, opts) },
.http => |h| .{ .http = try Http.init(a, router, h, opts) },
.other => unreachable,
},
};
@@ -50,6 +49,9 @@ pub fn serve(srv: *Server) !void {
 
test Server {
std.testing.refAllDecls(@This());
 
const srv = try init(std.testing.allocator, Router.Routes(&.{}), .{});
_ = srv;
}
 
const std = @import("std");
 
src/zwsgi.zig added: 65, removed: 79, total 0
@@ -15,11 +15,11 @@ pub const Options = struct {
chmod: ?std.posix.mode_t = null,
};
 
pub fn init(a: Allocator, opts: Options, sopts: Server.Options) zWSGI {
pub fn init(a: Allocator, router: Router, opts: Options, sopts: Server.Options) zWSGI {
return .{
.alloc = a,
.unix_file = opts.file,
.router = sopts.router,
.router = router,
.auth = sopts.auth,
.options = opts,
};
@@ -256,13 +256,9 @@ fn requestData(a: Allocator, zreq: *zWSGIRequest) !Request.Data {
test init {
const a = std.testing.allocator;
 
const R = struct {
fn route(frame: *Frame) Router.RoutingError!Router.BuildFn {
return Router.router(frame, &.{});
}
};
const router = Router.Routes(&.{});
 
_ = init(a, .{}, .{ .router = .{ .routefn = R.route } });
_ = init(a, router, .{}, .{});
}
 
const std = @import("std");