From d3633dfdbceba01ce4ebe940dc97bbce04fe39c9 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Fri, 25 Jul 2025 14:43:51 -0500 Subject: [PATCH] fix redundant middleware, clean up pipeline --- modules/http/http.lua | 113 +++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 73 deletions(-) diff --git a/modules/http/http.lua b/modules/http/http.lua index 1761715..70a3b13 100644 --- a/modules/http/http.lua +++ b/modules/http/http.lua @@ -8,6 +8,7 @@ _G._http_routes = _G._http_routes or {} _G._http_middleware = _G._http_middleware or {} _G._http_session_config = _G._http_session_config or { store_name = "sessions", + filename = "sessions.json", cookie_name = "session_id", cookie_options = { path = "/", @@ -329,12 +330,19 @@ function Request:is_json() return string.contains(content_type, "application/json") end --- Session shortcuts -function Request:csrf_token() return self.session:csrf_token() end -function Request:verify_csrf(token) return self.session:verify_csrf_token(token) end +-- Static flash methods (no dynamic assignment) function Request:flash(key, message) return self.session:flash(key, message) end function Request:flash_now(key, message) return self.session:flash_now(key, message) end function Request:get_flash() return self.session:get_all_flash() end +function Request:clear_flash() return self.session:clear_flash() end +function Request:flash_success(msg) return self.session:flash_success(msg) end +function Request:flash_error(msg) return self.session:flash_error(msg) end +function Request:flash_warning(msg) return self.session:flash_warning(msg) end +function Request:flash_info(msg) return self.session:flash_info(msg) end + +-- Session shortcuts +function Request:csrf_token() return self.session:csrf_token() end +function Request:verify_csrf(token) return self.session:verify_csrf_token(token) end function Request:is_authenticated() return self.session:get("authenticated", false) end function Request:current_user() return self.session:get("user") end @@ -430,6 +438,21 @@ function Response:redirect(url, status) return self end +-- Static redirect with flash methods +function Response:redirect_with_flash(url, flash_type, message, status, request) + if not request then error("Request object required for flash redirect") end + request:flash(flash_type, message) + return self:redirect(url, status) +end + +function Response:redirect_with_success(url, message, status, request) + return self:redirect_with_flash(url, "success", message, status, request) +end + +function Response:redirect_with_error(url, message, status, request) + return self:redirect_with_flash(url, "error", message, status, request) +end + function Response:cookie(name, value, options) if self._sent then error("Cannot set cookies after response has been sent") end options = options or {} @@ -463,19 +486,6 @@ function Response:cookie(name, value, options) return self end --- Flash redirect methods (set via middleware) -function Response:redirect_with_flash(url, flash_type, message, status) - error("Use flash_middleware to enable this method") -end - -function Response:redirect_with_success(url, message, status) - error("Use flash_middleware to enable this method") -end - -function Response:redirect_with_error(url, message, status) - error("Use flash_middleware to enable this method") -end - -- ====================================================================== -- ROUTING BASE CLASS -- ====================================================================== @@ -682,6 +692,20 @@ function Server:close() return _G.__IS_WORKER or moonshark.http_close_server() end +function Server:static(root_path, url_prefix) + url_prefix = url_prefix or "/" + if not string.starts_with(url_prefix, "/") then + url_prefix = "/" .. url_prefix + end + + if not _G.__IS_WORKER then + local success, err = moonshark.http_register_static(url_prefix, root_path) + if not success then + error("Failed to register static handler: " .. (err or "unknown error")) + end + end +end + -- ====================================================================== -- ROUTING ENGINE -- ====================================================================== @@ -828,22 +852,6 @@ function http.cors(options) end end -function http.static(root_path, url_prefix) - url_prefix = url_prefix or "/" - if not string.starts_with(url_prefix, "/") then - url_prefix = "/" .. url_prefix - end - - if not _G.__IS_WORKER then - local success, err = moonshark.http_register_static(url_prefix, root_path) - if not success then - error("Failed to register static handler: " .. (err or "unknown error")) - end - end - - return function(req, res, next) next() end -end - function http.json_parser(options) options = options or {} local strict = options.strict ~= false @@ -924,29 +932,6 @@ function http.csrf_protection(options) end end -function http.flash_middleware() - return function(req, res, next) - req.flash = function(key, message) return req.session:flash(key, message) end - req.flash_now = function(key, message) return req.session:flash_now(key, message) end - req.get_flash = function() return req.session:get_all_flash() end - - res.redirect_with_flash = function(self, url, flash_type, message, status) - req.session:flash(flash_type, message) - return self:redirect(url, status) - end - - res.redirect_with_success = function(self, url, message, status) - return self:redirect_with_flash(url, "success", message, status) - end - - res.redirect_with_error = function(self, url, message, status) - return self:redirect_with_flash(url, "error", message, status) - end - - next() - end -end - function http.session_middleware(options) return function(req, res, next) if options and not _G._session_configured then @@ -998,24 +983,6 @@ function http.require_guest(redirect_url) end end -function http.template_helpers() - return function(req, res, next) - res.locals.csrf_token = req:csrf_token() - res.locals.csrf_token_tag = '' - res.locals.flash_messages = req:get_flash() - res.locals.current_user = req:current_user() - res.locals.is_authenticated = req:is_authenticated() - - res.locals.flash_html = function(flash_type, css_class) - css_class = css_class or "alert alert-" .. flash_type - local messages = res.locals.flash_messages - return messages[flash_type] and '
' .. tostring(messages[flash_type]) .. '
' or "" - end - - next() - end -end - function http.create_server(callback) local app = http.server() if callback then callback(app) end