1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
package main
import (
"errors"
"io"
"log"
"net/http"
)
type WorkerRequest struct {
Request *http.Request
Route string
result chan *WorkerResponse
}
type WorkerResponse struct {
Code int
Headers map[string]string
Body string
}
type Worker struct {
read chan *WorkerRequest
lua *Lua
api LuaRef
routes map[string]LuaRef
started bool
}
func NewWorker(read chan *WorkerRequest) *Worker {
return &Worker {
read: read,
routes: make(map[string]LuaRef),
lua: &Lua{},
}
}
func (w *Worker) Start (filename string) error {
if w.started {
return errors.New("already started")
}
w.lua.Start()
api, err := w.lua.Require(filename)
if err != nil {
return err
}
w.api = api
err = w.initRoutes()
if err != nil {
return err
}
return nil
}
func (w *Worker) Listen () {
for {
r := <- w.read
handlerRef := w.routes[r.Route]
w.lua.PushFromRef(handlerRef)
w.lua.PushString(r.Request.Method)
w.lua.PushString(r.Request.URL.Path)
fh := make(map[string]string)
for k := range r.Request.Header {
fh[k] = r.Request.Header.Get(k)
}
w.lua.PushTable(fh)
body, err := io.ReadAll(r.Request.Body)
if err != nil {
w.lua.Pop(4);
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))
w.lua.PCall(4, 3)
code := w.lua.ToInt(-3)
rbody := w.lua.ToString(-1)
// parse headers
headers := make(map[string]string)
w.lua.Pop(1)
w.lua.PushNil()
for w.lua.Next() {
if !w.lua.IsString(-2) || !w.lua.IsString(-2) {
w.lua.Pop(1)
continue
}
v := w.lua.ToString(-1)
w.lua.Pop(1)
// we must not pop the item key from the stack because
// otherwise C.lua_next won't work properly
k := w.lua.ToString(-1)
headers[k] = v
}
w.lua.Pop(2)
r.result <- &WorkerResponse {
Code: int(code),
Headers: headers,
Body: rbody,
}
}
}
func (w *Worker) Request (route string, r *http.Request) chan *WorkerResponse {
res := make(chan *WorkerResponse)
w.read <- &WorkerRequest{
Request: r,
Route: route,
result: res,
}
return res
}
func (w *Worker) ListRoutes() []string {
res := []string{}
for route, _ := range w.routes {
res = append(res, route)
}
return res
}
func (w *Worker) Stop () {
w.lua.Close()
}
func (w *Worker) initRoutes() error {
w.lua.PushFromRef(w.api)
w.lua.PushTableItem("routes")
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) {
w.lua.Pop(1)
continue
}
handlerRef := w.lua.PopToRef()
// we must not pop the item key from the stack because
// otherwise C.lua_next won't work properly
route := w.lua.ToString(-1)
w.routes[route] = handlerRef
}
w.lua.Pop(2)
return nil
}
|