implement sessions

This commit is contained in:
Sky Johnson 2025-04-10 13:01:17 -05:00
parent 195800082a
commit c952242a9c
4 changed files with 64 additions and 24 deletions

View File

@ -210,6 +210,12 @@ func (s *Server) handleLuaRoute(ctx *fasthttp.RequestCtx, bytecode []byte, scrip
return return
} }
// Handle persisting session data
for k, v := range response.SessionData {
session.Set(k, v)
}
s.sessionManager.ApplySessionCookie(ctx, session)
// Apply response to HTTP context // Apply response to HTTP context
runner.ApplyResponse(response, ctx) runner.ApplyResponse(response, ctx)

View File

@ -18,9 +18,7 @@ type Response struct {
Cookies []*fasthttp.Cookie // HTTP cookies Cookies []*fasthttp.Cookie // HTTP cookies
// Session information // Session information
SessionID string // Session ID SessionData map[string]any
SessionData map[string]any // Session data
SessionModified bool // Whether session was modified
} }
// Response pool to reduce allocations // Response pool to reduce allocations
@ -47,30 +45,12 @@ func ReleaseResponse(resp *Response) {
return return
} }
// Reset fields to default values
resp.Body = nil resp.Body = nil
resp.Status = 200 resp.Status = 200
resp.Headers = make(map[string]string, 8)
// Clear maps resp.Metadata = make(map[string]any, 8)
for k := range resp.Headers {
delete(resp.Headers, k)
}
for k := range resp.Metadata {
delete(resp.Metadata, k)
}
for k := range resp.SessionData {
delete(resp.SessionData, k)
}
// Clear cookies
resp.Cookies = resp.Cookies[:0] resp.Cookies = resp.Cookies[:0]
resp.SessionData = make(map[string]any, 8)
// Reset session info
resp.SessionID = ""
resp.SessionModified = false
// Return to pool
responsePool.Put(resp) responsePool.Put(resp)
} }

View File

@ -193,6 +193,16 @@ func extractHTTPResponseData(state *luajit.State, response *Response) {
} }
state.Pop(1) state.Pop(1)
// Extract session data
state.GetField(-1, "session")
if state.IsTable(-1) {
table, err := state.ToTable(-1)
if err == nil {
maps.Copy(response.SessionData, table)
}
}
state.Pop(1)
state.Pop(1) // Pop __http_response state.Pop(1) // Pop __http_response
} }

View File

@ -261,6 +261,49 @@ local cookie = {
end end
} }
-- ======================================================================
-- SESSION MODULE
-- ======================================================================
local session = {
get = function(key)
if type(key) ~= "string" then
error("session.get: key must be a string", 2)
end
local env = getfenv(2)
if env.ctx and env.ctx.session and env.ctx.session.data then
return env.ctx.session.data[key]
end
return nil
end,
set = function(key, value)
if type(key) ~= "string" then
error("session.set: key must be a string", 2)
end
if type(value) == nil then
error("session.set: value cannot be nil", 2)
end
local resp = __ensure_response()
resp.session = resp.session or {}
resp.session[key] = value
end,
id = function()
local env = getfenv(2)
if env.ctx and env.ctx.session then
return env.ctx.session.id
end
return nil
end
}
-- ====================================================================== -- ======================================================================
-- UTIL MODULE -- UTIL MODULE
-- ====================================================================== -- ======================================================================
@ -350,5 +393,6 @@ local util = {
-- ====================================================================== -- ======================================================================
_G.http = http _G.http = http
_G.session = session
_G.cookie = cookie _G.cookie = cookie
_G.util = util _G.util = util