fix redundant middleware, clean up pipeline
This commit is contained in:
parent
1fbcfaf9df
commit
d3633dfdbc
@ -8,6 +8,7 @@ _G._http_routes = _G._http_routes or {}
|
|||||||
_G._http_middleware = _G._http_middleware or {}
|
_G._http_middleware = _G._http_middleware or {}
|
||||||
_G._http_session_config = _G._http_session_config or {
|
_G._http_session_config = _G._http_session_config or {
|
||||||
store_name = "sessions",
|
store_name = "sessions",
|
||||||
|
filename = "sessions.json",
|
||||||
cookie_name = "session_id",
|
cookie_name = "session_id",
|
||||||
cookie_options = {
|
cookie_options = {
|
||||||
path = "/",
|
path = "/",
|
||||||
@ -329,12 +330,19 @@ function Request:is_json()
|
|||||||
return string.contains(content_type, "application/json")
|
return string.contains(content_type, "application/json")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Session shortcuts
|
-- Static flash methods (no dynamic assignment)
|
||||||
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:flash(key, message) return self.session:flash(key, message) end
|
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: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: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:is_authenticated() return self.session:get("authenticated", false) end
|
||||||
function Request:current_user() return self.session:get("user") end
|
function Request:current_user() return self.session:get("user") end
|
||||||
|
|
||||||
@ -430,6 +438,21 @@ function Response:redirect(url, status)
|
|||||||
return self
|
return self
|
||||||
end
|
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)
|
function Response:cookie(name, value, options)
|
||||||
if self._sent then error("Cannot set cookies after response has been sent") end
|
if self._sent then error("Cannot set cookies after response has been sent") end
|
||||||
options = options or {}
|
options = options or {}
|
||||||
@ -463,19 +486,6 @@ function Response:cookie(name, value, options)
|
|||||||
return self
|
return self
|
||||||
end
|
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
|
-- ROUTING BASE CLASS
|
||||||
-- ======================================================================
|
-- ======================================================================
|
||||||
@ -682,6 +692,20 @@ function Server:close()
|
|||||||
return _G.__IS_WORKER or moonshark.http_close_server()
|
return _G.__IS_WORKER or moonshark.http_close_server()
|
||||||
end
|
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
|
-- ROUTING ENGINE
|
||||||
-- ======================================================================
|
-- ======================================================================
|
||||||
@ -828,22 +852,6 @@ function http.cors(options)
|
|||||||
end
|
end
|
||||||
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)
|
function http.json_parser(options)
|
||||||
options = options or {}
|
options = options or {}
|
||||||
local strict = options.strict ~= false
|
local strict = options.strict ~= false
|
||||||
@ -924,29 +932,6 @@ function http.csrf_protection(options)
|
|||||||
end
|
end
|
||||||
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)
|
function http.session_middleware(options)
|
||||||
return function(req, res, next)
|
return function(req, res, next)
|
||||||
if options and not _G._session_configured then
|
if options and not _G._session_configured then
|
||||||
@ -998,24 +983,6 @@ function http.require_guest(redirect_url)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function http.template_helpers()
|
|
||||||
return function(req, res, next)
|
|
||||||
res.locals.csrf_token = req:csrf_token()
|
|
||||||
res.locals.csrf_token_tag = '<input type="hidden" name="_csrf_token" value="' .. req:csrf_token() .. '">'
|
|
||||||
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 '<div class="' .. css_class .. '">' .. tostring(messages[flash_type]) .. '</div>' or ""
|
|
||||||
end
|
|
||||||
|
|
||||||
next()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function http.create_server(callback)
|
function http.create_server(callback)
|
||||||
local app = http.server()
|
local app = http.server()
|
||||||
if callback then callback(app) end
|
if callback then callback(app) end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user