srctree

Gregory Mullen parent 5a871f1c f89e7e68
add auth requirement options to stats

inlinesplit
examples/stats.zig added: 44, removed: 10, total 34
@@ -9,7 +9,9 @@ pub fn main() !void {
std.heap.page_allocator,
.{
.mode = .{ .http = .{ .port = 8088 } },
.stats = true,
.stats = .{
.auth_mode = .open,
},
},
) catch |err| {
std.log.err("Unable to serve stats err: [{}]", .{err});
 
src/auth/provider.zig added: 44, removed: 10, total 34
@@ -21,7 +21,7 @@ pub const VTable = struct {
pub const CreateSessionFn = *const fn (*anyopaque, *User) Error!void;
pub const GetCookieFn = *const fn (*anyopaque, User) Error!?RequestCookie;
 
pub const Empty: VTable = .{
pub const empty: VTable = .{
.authenticate = null,
.lookup_user = null,
.valid = null,
@@ -77,7 +77,7 @@ test "Provider" {
const std = @import("std");
const p = Provider{
.ctx = undefined,
.vtable = VTable.Empty,
.vtable = .empty,
};
 
try std.testing.expectError(error.NotProvided, p.authenticate(undefined));
 
src/server.zig added: 44, removed: 10, total 34
@@ -28,17 +28,20 @@ pub const Options = struct {
mode: RunMode = .{ .http = .{} },
auth: Auth.Provider = .invalid,
threads: ?u16 = null,
stats: bool = false,
stats: ?stats_.Options = null,
};
 
pub fn init(a: Allocator, router: Router, opts: Options) !Server {
if (opts.stats) |so| {
stats_.options = so;
}
return .{
.interface = switch (opts.mode) {
.zwsgi => |z| .{ .zwsgi = zWSGI.init(a, router, z, opts) },
.http => |h| .{ .http = try Http.init(a, router, h, opts) },
.other => unreachable,
},
.stats = if (opts.stats) .init(opts.threads != null) else null,
.stats = if (opts.stats) |_| .init(opts.threads != null) else null,
};
}
 
 
src/stats.zig added: 44, removed: 10, total 34
@@ -1,3 +1,19 @@
pub const Options = struct {
auth_mode: AuthMode,
 
pub const AuthMode = enum {
auth_required,
sensitive,
open,
};
 
pub const default: Options = .{
.auth_mode = .sensitive,
};
};
 
pub var options: Options = .default;
 
pub const Stats = struct {
mutex: ?std.Thread.Mutex,
start_time: i64,
@@ -105,6 +121,19 @@ pub const Endpoint = struct {
pub const stats = index;
 
pub fn index(f: *Frame) Router.Error!void {
var include_ip: bool = false;
switch (options.auth_mode) {
.auth_required => {
if (f.auth_provider.vtable.valid == null) return f.sendDefaultErrorPage(.not_implemented);
},
.sensitive => {
if (f.user) |user| if (f.auth_provider.valid(&user)) {
include_ip = true;
};
},
.open => include_ip = true,
}
 
var data: [60]S.VerseStatsList = @splat(
.{
.ip_address = "",
@@ -145,7 +174,7 @@ pub const Endpoint = struct {
null;
 
data[i] = .{
.ip_address = src.addr.slice(),
.ip_address = if (include_ip) src.addr.slice() else "[redacted]",
.number = src.number,
.size = src.size,
.time = src.time,