summaryrefslogtreecommitdiff
path: root/worker.go
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-08-23 21:14:30 +0600
committerunwox <me@unwox.com>2024-08-23 21:14:30 +0600
commit751e381230762caa6937e9c516de82fd27c59250 (patch)
tree10a4ded95e673133d9e8303524a585469e805786 /worker.go
parent4003417f7b94648a7726995a49ab56c4bc8aad55 (diff)
pass a single response object instead of multiple arguments to lua
Diffstat (limited to 'worker.go')
-rw-r--r--worker.go31
1 files changed, 25 insertions, 6 deletions
diff --git a/worker.go b/worker.go
index f86906f..77aae33 100644
--- a/worker.go
+++ b/worker.go
@@ -93,14 +93,22 @@ func (w *Worker) Listen(queue chan any) {
}
w.lua.PushFromRef(w.routes[r.route])
- w.lua.PushString(r.request.Method)
- w.lua.PushString(r.request.URL.Path)
+ res := make(map[string]any)
+ res["method"] = r.request.Method
+ res["path"] = r.request.URL.Path
- fh := make(map[string]string)
+ fh := make(map[string]any)
for k := range r.request.Header {
fh[k] = r.request.Header.Get(k)
}
- w.lua.PushStringTable(fh)
+ res["headers"] = fh
+
+ flatQr := make(map[string]any)
+ qr := r.request.URL.Query()
+ for k := range qr {
+ flatQr[k] = qr.Get(k)
+ }
+ res["query"] = flatQr
body, err := io.ReadAll(r.request.Body)
if err != nil {
@@ -112,9 +120,20 @@ func (w *Worker) Listen(queue chan any) {
log.Println("could not read a request body:", err)
return
}
- w.lua.PushString(string(body))
+ res["body"] = string(body)
+
+ err = w.lua.PushObject(res)
+ if err != nil {
+ r.result <- &HTTPResponse {
+ Code: 500,
+ Headers: make(map[string]string),
+ Body: "server error",
+ }
+ log.Println("could not form a request to lua:", err)
+ return
+ }
- err = w.lua.PCall(4, 3)
+ err = w.lua.PCall(1, 3)
if err != nil {
r.result <- &HTTPResponse {