From 90f03cec94e92d92246d9bbd05cc1319ec2fa2b3 Mon Sep 17 00:00:00 2001 From: robin Date: Thu, 3 Sep 2026 22:34:35 +0200 Subject: [PATCH] maivi: implement redirects as a plugin --- examples/plugins/redirects.lua | 12 ++++++++++ src/main.zig | 2 -- src/root.zig | 43 ---------------------------------- 3 files changed, 12 insertions(+), 45 deletions(-) create mode 100644 examples/plugins/redirects.lua diff --git a/examples/plugins/redirects.lua b/examples/plugins/redirects.lua new file mode 100644 index 0000000..8685d98 --- /dev/null +++ b/examples/plugins/redirects.lua @@ -0,0 +1,12 @@ +local paths = { + ["^/wiwiwi"] = "/wawawa", +} + +maivi.hook("handle", {}, function(ctx) + for k, v in pairs(paths) do + local str, n = string.gsub(ctx.path, k, v, 1) + if n == 1 then + return ctx:redirect(str, nil) + end + end +end) diff --git a/src/main.zig b/src/main.zig index 2497ccc..7132b8c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -51,8 +51,6 @@ pub fn main(init: process.Init) !void { if (maivi.build) return maivi.gen.generate(); - try maivi.add_redirects(); - try maivi.server(); try maivi.listen(); } diff --git a/src/root.zig b/src/root.zig index 7f5bb60..7305cc8 100644 --- a/src/root.zig +++ b/src/root.zig @@ -29,8 +29,6 @@ var closed = false; pub var router: Router = undefined; -var redirects: std.StringHashMap([]const u8) = undefined; - // allocator ================================================================== pub var allocator: Allocator = undefined; @@ -52,8 +50,6 @@ pub fn init(init_io: Io, init_allocator: Allocator, cfg: *const Config) !void { router = .init(); - redirects = .init(allocator); - if (luaruntime) { data.init(); errdefer data.deinit(); @@ -70,8 +66,6 @@ pub fn deinit() void { router.deinit(); - redirects.deinit(); - if (luaruntime) { data.deinit(); lua.deinit(); @@ -201,40 +195,3 @@ fn handle_fs(conn: *Connection) !void { return error.FileNotFound; } - -// redirects ================================================================== - -pub fn add_redirects() !void { - const root = try std.Io.Dir.openDirAbsolute(io, config.root, .{}); - const contents = helpers.read_file(allocator, root, "redirect.txt") catch |err| switch (err) { - error.FileNotFound => return, - else => return err, - }; - defer allocator.free(contents); - - var it = std.mem.splitScalar(u8, contents, '\n'); - while (it.next()) |x| { - var from: ?[]const u8 = null; - var to: ?[]const u8 = null; - - var x_it = std.mem.splitScalar(u8, x, ' '); - while (x_it.next()) |s| { - if (s.len == 0) continue; - if (from == null) from = s else if (to == null) to = s; - } - - if (from == null or to == null) continue; - - const v = try redirects.allocator.dupe(u8, to.?); - try redirects.put(from.?, v); - try router.add(from.?, handleredirect, .{}); - } -} - -fn handleredirect(conn: *Connection) !void { - const request = conn.request; - - const redirect_url = redirects.get(conn.path) orelse - return error.FileNotFound; - _ = try helpers.redirect(conn.arena, request, redirect_url, null); -} -- 2.51.2