start sessions.... again

This commit is contained in:
Sky Johnson 2025-04-10 10:44:52 -05:00
parent 7b7876e864
commit 195800082a
3 changed files with 13 additions and 17 deletions

View File

@ -135,7 +135,7 @@ func (s *Server) processRequest(ctx *fasthttp.RequestCtx) {
return return
} else if found { } else if found {
logger.Debug("Found Lua route match for %s %s with %d params", method, path, params.Count) logger.Debug("Found Lua route match for %s %s with %d params", method, path, params.Count)
s.handleLuaRoute(ctx, bytecode, scriptPath, params) s.handleLuaRoute(ctx, bytecode, scriptPath, params, method, path)
return return
} }
@ -152,13 +152,11 @@ func (s *Server) processRequest(ctx *fasthttp.RequestCtx) {
} }
// handleLuaRoute executes a Lua route // handleLuaRoute executes a Lua route
func (s *Server) handleLuaRoute(ctx *fasthttp.RequestCtx, bytecode []byte, scriptPath string, params *routers.Params) { func (s *Server) handleLuaRoute(ctx *fasthttp.RequestCtx, bytecode []byte, scriptPath string, params *routers.Params, method string, path string) {
// Create context for Lua execution // Create context for Lua execution
luaCtx := runner.NewHTTPContext(ctx) luaCtx := runner.NewHTTPContext(ctx)
defer luaCtx.Release() defer luaCtx.Release()
method := string(ctx.Method())
path := string(ctx.Path())
host := string(ctx.Host()) host := string(ctx.Host())
// Set up additional context values // Set up additional context values
@ -166,6 +164,13 @@ func (s *Server) handleLuaRoute(ctx *fasthttp.RequestCtx, bytecode []byte, scrip
luaCtx.Set("path", path) luaCtx.Set("path", path)
luaCtx.Set("host", host) luaCtx.Set("host", host)
// Add session data
session := s.sessionManager.GetSessionFromRequest(ctx)
luaCtx.Set("session", map[string]any{
"id": session.ID,
"data": session.Data,
})
// URL parameters // URL parameters
if params.Count > 0 { if params.Count > 0 {
paramMap := make(map[string]any, params.Count) paramMap := make(map[string]any, params.Count)

View File

@ -139,19 +139,12 @@ func (s *Sandbox) Execute(state *luajit.State, bytecode []byte, ctx *Context) (*
// extractResponseData pulls response info from the Lua state // extractResponseData pulls response info from the Lua state
func extractHTTPResponseData(state *luajit.State, response *Response) { func extractHTTPResponseData(state *luajit.State, response *Response) {
state.GetGlobal("__http_responses") state.GetGlobal("__http_response")
if !state.IsTable(-1) { if !state.IsTable(-1) {
state.Pop(1) state.Pop(1)
return return
} }
state.PushNumber(1)
state.GetTable(-2)
if !state.IsTable(-1) {
state.Pop(2)
return
}
// Extract status // Extract status
state.GetField(-1, "status") state.GetField(-1, "status")
if state.IsNumber(-1) { if state.IsNumber(-1) {
@ -200,8 +193,7 @@ func extractHTTPResponseData(state *luajit.State, response *Response) {
} }
state.Pop(1) state.Pop(1)
// Clean up state.Pop(1) // Pop __http_response
state.Pop(2)
} }
// extractCookie pulls cookie data from the current table on the stack // extractCookie pulls cookie data from the current table on the stack

View File

@ -1,6 +1,7 @@
package sessions package sessions
import ( import (
"maps"
"sync" "sync"
"time" "time"
) )
@ -66,9 +67,7 @@ func (s *Session) GetAll() map[string]any {
defer s.mu.RUnlock() defer s.mu.RUnlock()
copy := make(map[string]any, len(s.Data)) copy := make(map[string]any, len(s.Data))
for k, v := range s.Data { maps.Copy(copy, s.Data)
copy[k] = v
}
return copy return copy
} }