Compare commits

..

5 Commits

Author SHA1 Message Date
b454f4de8d delete useless comment 2025-07-30 14:12:49 +03:00
c161639766 move lua types converters from utils to sv1 2025-07-30 14:12:10 +03:00
dd336a7d9a move lua types converters from utils to sv1 2025-07-30 14:11:57 +03:00
ab37ecb7f7 add init hooks to nodeApp 2025-07-30 13:47:05 +03:00
bd02f079ab rename sv1.HandleLUA to sv1.handleLUA 2025-07-30 12:50:12 +03:00
4 changed files with 25 additions and 30 deletions

View File

@@ -28,7 +28,11 @@ import (
var nodeApp = app.New()
func Run(cmd *cobra.Command, args []string) {
nodeApp.InitialHooks()
nodeApp.InitialHooks(
Init0Hook, Init1Hook, Init2Hook,
Init3Hook, Init4Hook, Init5Hook,
Init6Hook,
)
nodeApp.Run(RunHook)
}

View File

@@ -24,5 +24,5 @@ func (h *HandlerV1) Handle(r *http.Request, req *rpc.RPCRequest) *rpc.RPCRespons
}
}
return h.HandleLUA(method, req)
return h.handleLUA(method, req)
}

View File

@@ -7,13 +7,14 @@ import (
"path/filepath"
"strconv"
"github.com/akyaiy/GoSally-mvp/internal/core/utils"
"github.com/akyaiy/GoSally-mvp/internal/engine/logs"
"github.com/akyaiy/GoSally-mvp/internal/server/rpc"
lua "github.com/yuin/gopher-lua"
)
func (h *HandlerV1) HandleLUA(path string, req *rpc.RPCRequest) *rpc.RPCResponse {
func (h *HandlerV1) handleLUA(path string, req *rpc.RPCRequest) *rpc.RPCResponse {
L := lua.NewState()
defer L.Close()
@@ -21,7 +22,7 @@ func (h *HandlerV1) HandleLUA(path string, req *rpc.RPCRequest) *rpc.RPCResponse
paramsTable := L.NewTable()
if fetchedParams, ok := req.Params.(map[string]any); ok {
for k, v := range fetchedParams {
L.SetField(paramsTable, k, utils.ConvertGolangTypesToLua(L, v))
L.SetField(paramsTable, k, ConvertGolangTypesToLua(L, v))
}
}
L.SetField(inTable, "Params", paramsTable)
@@ -34,29 +35,20 @@ func (h *HandlerV1) HandleLUA(path string, req *rpc.RPCRequest) *rpc.RPCResponse
logTable := L.NewTable()
L.SetField(logTable, "Info", L.NewFunction(func(L *lua.LState) int {
msg := L.ToString(1)
h.x.SLog.Info(fmt.Sprintf("the script says: %s", msg), slog.String("script", path))
return 0
}))
logFuncs := map[string]func(string, ...any){
"Info": h.x.SLog.Info,
"Debug": h.x.SLog.Debug,
"Error": h.x.SLog.Error,
"Warn": h.x.SLog.Warn,
}
L.SetField(logTable, "Debug", L.NewFunction(func(L *lua.LState) int {
msg := L.ToString(1)
h.x.SLog.Debug(fmt.Sprintf("the script says: %s", msg), slog.String("script", path))
return 0
}))
L.SetField(logTable, "Error", L.NewFunction(func(L *lua.LState) int {
msg := L.ToString(1)
h.x.SLog.Error(fmt.Sprintf("the script says: %s", msg), slog.String("script", path))
return 0
}))
L.SetField(logTable, "Warn", L.NewFunction(func(L *lua.LState) int {
msg := L.ToString(1)
h.x.SLog.Warn(fmt.Sprintf("the script says: %s", msg), slog.String("script", path))
return 0
}))
for name, logFunc := range logFuncs {
L.SetField(logTable, name, L.NewFunction(func(L *lua.LState) int {
msg := L.ToString(1)
logFunc(fmt.Sprintf("the script says: %s", msg), slog.String("script", path))
return 0
}))
}
L.SetField(logTable, "Event", L.NewFunction(func(L *lua.LState) int {
msg := L.ToString(1)
@@ -120,7 +112,7 @@ func (h *HandlerV1) HandleLUA(path string, req *rpc.RPCRequest) *rpc.RPCResponse
out := make(map[string]any)
resultTbl.ForEach(func(key lua.LValue, value lua.LValue) {
out[key.String()] = utils.ConvertLuaTypesToGolang(value)
out[key.String()] = ConvertLuaTypesToGolang(value)
})
out["responsible-node"] = h.cs.UUID32

View File

@@ -1,4 +1,4 @@
package utils
package sv1
import (
"fmt"
@@ -17,7 +17,6 @@ func ConvertLuaTypesToGolang(value lua.LValue) any {
case lua.LTTable:
tbl := value.(*lua.LTable)
// Попробуем как массив
var arr []any
isArray := true
tbl.ForEach(func(key, val lua.LValue) {