diff options
| author | unwox <me@unwox.com> | 2024-09-14 15:51:23 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2024-09-14 15:51:23 +0600 |
| commit | 986522e417406956e83362e08adbd6a18ec904a5 (patch) | |
| tree | 598fd241569c845acd6a604cdf88942e146237de /main.go | |
| parent | a6412f9fc48f410ba4e3b39938d8d3f6a5597066 (diff) | |
better handling of args coming from lua with l.Scan
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 35 |
1 files changed, 14 insertions, 21 deletions
@@ -37,9 +37,14 @@ func main() { // define luna.router module routeModule := make(map[string]any) routeModule["route"] = func (l *Lua) int { - fn := l.PopToRef() - route := l.ToString(-1) - l.Pop(1) + var route string + var fn LuaRef + err := l.Scan(&route, &fn) + if err != nil { + fmt.Println("error:", err) + // FIXME: handle + return 0 + } // find corresponding worker for the lua context var wrk *Worker for _, wrk = range wrks { @@ -84,26 +89,14 @@ func main() { // define luna.http module httpModule := make(map[string]any) httpModule["request"] = func (l *Lua) int { - body := l.ToString(-1) - url := l.ToString(-3) - method := l.ToString(-4) - l.Pop(1) + var method, url, body string headers := make(map[string]string) - l.PushNil() - for l.Next() { - if !l.IsString(-2) || !l.IsString(-2) { - l.Pop(1) - continue - } - v := l.ToString(-1) - l.Pop(1) - // We must not pop the item key from the stack - // because otherwise C.lua_next won't work - // properly. - k := l.ToString(-1) - headers[k] = v + err := l.Scan(&method, &url, &headers, &body) + if err != nil { + // FIXME: handle + fmt.Println("error:", err) + return 0 } - l.Pop(2) req, err := http.NewRequest(method, url, strings.NewReader(body)) if err != nil { |
