mirror of
https://github.com/metabarcoding/obitools4.git
synced 2026-04-30 12:00:39 +00:00
10f49fe64b
// // Registers the http module in Lua state as a global, // aligning with obicontext and BioSequence conventions. The change ensures consistent module exposure across Lua environments.
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package obilua
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
lua "github.com/yuin/gopher-lua"
|
|
)
|
|
|
|
const httpClientTimeout = 30 * time.Second
|
|
|
|
var _httpClient = &http.Client{
|
|
Timeout: httpClientTimeout,
|
|
}
|
|
|
|
// RegisterHTTP registers the http module in the Lua state as a global,
|
|
// consistent with obicontext and BioSequence.
|
|
//
|
|
// Exposes:
|
|
//
|
|
// http.post(url, body) → response string (on success)
|
|
// http.post(url, body) → nil, err string (on error)
|
|
func RegisterHTTP(luaState *lua.LState) {
|
|
table := luaState.NewTable()
|
|
luaState.SetField(table, "post", luaState.NewFunction(luaHTTPPost))
|
|
luaState.SetGlobal("http", table)
|
|
}
|
|
|
|
// luaHTTPPost implements http.post(url, body) for Lua.
|
|
//
|
|
// Lua signature:
|
|
//
|
|
// local response = http.post(url, body)
|
|
// local response, err = http.post(url, body)
|
|
func luaHTTPPost(L *lua.LState) int {
|
|
url := L.CheckString(1)
|
|
body := L.CheckString(2)
|
|
|
|
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(body))
|
|
if err != nil {
|
|
L.Push(lua.LNil)
|
|
L.Push(lua.LString(err.Error()))
|
|
return 2
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := _httpClient.Do(req)
|
|
if err != nil {
|
|
L.Push(lua.LNil)
|
|
L.Push(lua.LString(err.Error()))
|
|
return 2
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
respBytes, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
L.Push(lua.LNil)
|
|
L.Push(lua.LString(err.Error()))
|
|
return 2
|
|
}
|
|
|
|
L.Push(lua.LString(respBytes))
|
|
return 1
|
|
}
|