summaryrefslogtreecommitdiff
path: root/worker.go
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-06-12 20:35:23 +0600
committerunwox <me@unwox.com>2024-06-12 20:35:50 +0600
commitd00ee9196d08cf824c9ec55f2987399c751df966 (patch)
tree015a1fed765be0c9b1e54b2d562f66d9451a4852 /worker.go
parent89eb422c173e1dd0c1b6004dc920358ca935dec3 (diff)
improve worker lua integration
Diffstat (limited to 'worker.go')
-rw-r--r--worker.go26
1 files changed, 19 insertions, 7 deletions
diff --git a/worker.go b/worker.go
index d125f17..3c64d68 100644
--- a/worker.go
+++ b/worker.go
@@ -3,6 +3,7 @@ package main
import (
"errors"
"io"
+ "log"
"net/http"
)
@@ -45,7 +46,10 @@ func (w *Worker) Start (filename string) error {
return err
}
w.api = api
- w.initRoutes()
+ err = w.initRoutes()
+ if err != nil {
+ return err
+ }
return nil
}
@@ -67,8 +71,13 @@ func (w *Worker) Listen () {
body, err := io.ReadAll(r.Request.Body)
if err != nil {
w.lua.Pop(4);
- // TODO: 500
- return
+ r.result <- &WorkerResponse {
+ Code: 500,
+ Headers: make(map[string]string),
+ Body: "server error",
+ }
+ log.Println("could not read a request body")
+ continue
}
w.lua.PushString(string(body))
@@ -102,7 +111,7 @@ func (w *Worker) Listen () {
}
}
-func (w *Worker) Request (route string, r *http.Request,) chan *WorkerResponse {
+func (w *Worker) Request (route string, r *http.Request) chan *WorkerResponse {
res := make(chan *WorkerResponse)
w.read <- &WorkerRequest{
Request: r,
@@ -124,10 +133,12 @@ func (w *Worker) Stop () {
w.lua.Close()
}
-func (w *Worker) initRoutes() {
+func (w *Worker) initRoutes() error {
w.lua.PushFromRef(w.api)
w.lua.PushTableItem("routes")
- // FIXME: istable?
+ if !w.lua.IsTable(-1) {
+ return errors.New("\"routes\" must be a table")
+ }
w.lua.PushNil()
for w.lua.Next() {
if !w.lua.IsString(-2) || !w.lua.IsFunction(-1) {
@@ -140,5 +151,6 @@ func (w *Worker) initRoutes() {
route := w.lua.ToString(-1)
w.routes[route] = handlerRef
}
- // FIXME: pop?
+ w.lua.Pop(2)
+ return nil
}