From d4413c433fed7607d6d9c1ba47e5515e2a7a718f Mon Sep 17 00:00:00 2001 From: Alexey Date: Sun, 2 Nov 2025 11:04:12 +0200 Subject: [PATCH] fmt --- src/cmd/root.go | 4 +- src/hooks/initial.go | 10 +-- src/hooks/run.go | 12 ++- src/internal/engine/lua/pool.go | 4 +- src/internal/engine/lua/types.go | 5 +- src/internal/server/sv1/db_sqlite.go | 122 +++++++++++++-------------- src/internal/server/sv2/handle.go | 12 +++ src/internal/server/sv2/server.go | 43 ++++++++++ 8 files changed, 137 insertions(+), 75 deletions(-) create mode 100644 src/internal/server/sv2/handle.go create mode 100644 src/internal/server/sv2/server.go diff --git a/src/cmd/root.go b/src/cmd/root.go index 5c7bf49..030904b 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -1,4 +1,4 @@ -// The cmd package is the main package where all the main hooks and methods are called. +// The cmd package is the main package where all the main hooks and methods are called. // GoSally uses spf13/cobra to organize all the calls. package cmd @@ -24,7 +24,7 @@ scripts in a given directory. For more information, visit: https://gosally.oblat }, } -// Execute prepares global log, loads cmdline args +// Execute prepares global log, loads cmdline args // and executes rootCmd.Execute() func Execute() { log.SetOutput(os.Stdout) diff --git a/src/hooks/initial.go b/src/hooks/initial.go index d2b9309..bd81d93 100644 --- a/src/hooks/initial.go +++ b/src/hooks/initial.go @@ -85,7 +85,7 @@ func InitUUIDHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { corestate.NODE_UUID = uuid32 } -// The hook is responsible for checking the initialization stage +// The hook is responsible for checking the initialization stage // and restarting in some cases func InitRuntimeHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { if *x.Config.Env.ParentStagePID != os.Getpid() { @@ -138,7 +138,7 @@ func InitRuntimeHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { } // post-init stage -// The hook creates a run.lock file, which contains information +// The hook creates a run.lock file, which contains information // about the process and the node, in the runtime directory. func InitRunlockHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { NodeApp.Fallback(func(ctx context.Context, cs *corestate.CoreState, x *app.AppX) { @@ -192,7 +192,7 @@ func InitRunlockHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { } } -// The hook reads the configuration and replaces special expressions +// The hook reads the configuration and replaces special expressions // (%tmp% and so on) in string fields with the required data. func InitConfigReplHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { if !slices.Contains(*x.Config.Conf.DisableWarnings, "--WNonStdTmpDir") && os.TempDir() != "/tmp" { @@ -218,7 +218,7 @@ func InitConfigReplHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) } } -// The hook is responsible for outputting the +// The hook is responsible for outputting the // final config and asking for confirmation. func InitConfigPrintHook(ctx context.Context, cs *corestate.CoreState, x *app.AppX) { if *x.Config.Conf.Node.ShowConfig { @@ -254,7 +254,7 @@ func InitSLogHook(_ context.Context, cs *corestate.CoreState, x *app.AppX) { *x.SLog = *newSlog } -// The method goes through the entire config structure through +// The method goes through the entire config structure through // reflection and replaces string fields with the required ones. func processConfig(conf any, replacements map[string]any) error { val := reflect.ValueOf(conf) diff --git a/src/hooks/run.go b/src/hooks/run.go index 6245e19..ce4f240 100644 --- a/src/hooks/run.go +++ b/src/hooks/run.go @@ -11,8 +11,8 @@ import ( "regexp" "time" - "github.com/akyaiy/GoSally-mvp/src/internal/core/corestate" "github.com/akyaiy/GoSally-mvp/src/internal/colors" + "github.com/akyaiy/GoSally-mvp/src/internal/core/corestate" "github.com/akyaiy/GoSally-mvp/src/internal/core/run_manager" "github.com/akyaiy/GoSally-mvp/src/internal/core/update" "github.com/akyaiy/GoSally-mvp/src/internal/core/utils" @@ -22,6 +22,7 @@ import ( "github.com/akyaiy/GoSally-mvp/src/internal/server/gateway" "github.com/akyaiy/GoSally-mvp/src/internal/server/session" "github.com/akyaiy/GoSally-mvp/src/internal/server/sv1" + "github.com/akyaiy/GoSally-mvp/src/internal/server/sv2" "github.com/go-chi/chi/v5" "github.com/go-chi/cors" "github.com/spf13/cobra" @@ -65,13 +66,20 @@ func RunHook(ctx context.Context, cs *corestate.CoreState, x *app.AppX) error { Ver: "v1", }) + sv2 := sv2.InitServer(&sv2.HandlerInitStruct{ + X: x, + CS: cs, + AllowedCmd: regexp.MustCompile(AllowedCmdPattern), + Ver: "v2", + }) + session_manager := session.New(*x.Config.Conf.HTTPServer.SessionTTL) s := gateway.InitGateway(&gateway.GatewayServerInit{ SM: session_manager, CS: cs, X: x, - }, serverv1) + }, serverv1, sv2) r := chi.NewRouter() r.Use(cors.Handler(cors.Options{ diff --git a/src/internal/engine/lua/pool.go b/src/internal/engine/lua/pool.go index d1e009d..18bf202 100644 --- a/src/internal/engine/lua/pool.go +++ b/src/internal/engine/lua/pool.go @@ -15,7 +15,7 @@ func NewLuaPool() *LuaPool { pool: sync.Pool{ New: func() any { L := lua.NewState() - + return L }, }, @@ -30,6 +30,6 @@ func (lp *LuaPool) Put(L *lua.LState) { L.Close() newL := lua.NewState() - + lp.pool.Put(newL) } diff --git a/src/internal/engine/lua/types.go b/src/internal/engine/lua/types.go index 625b6f0..3ce3aed 100644 --- a/src/internal/engine/lua/types.go +++ b/src/internal/engine/lua/types.go @@ -11,13 +11,12 @@ import ( type LuaEngineDeps struct { HttpRequest *http.Request JSONRPCRequest *rpc.RPCRequest - SessionUUID string - ScriptPath string + SessionUUID string + ScriptPath string } type LuaEngineContract interface { Handle(deps *LuaEngineDeps) *rpc.RPCResponse - } type LuaEngine struct { diff --git a/src/internal/server/sv1/db_sqlite.go b/src/internal/server/sv1/db_sqlite.go index fa7cd68..3ebb86c 100644 --- a/src/internal/server/sv1/db_sqlite.go +++ b/src/internal/server/sv1/db_sqlite.go @@ -80,10 +80,10 @@ func loadDBMod(llog *slog.Logger, sid string) func(*lua.LState) int { mt := L.NewTypeMetatable("gosally_db") L.SetField(mt, "__index", L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{ - "exec": dbExec, - "query": dbQuery, + "exec": dbExec, + "query": dbQuery, "query_row": dbQueryRow, - "close": dbClose, + "close": dbClose, })) L.SetField(dbMod, "__seed", lua.LString(sid)) @@ -215,43 +215,43 @@ func dbExec(L *lua.LState) int { } func dbQueryRow(L *lua.LState) int { - ud := L.CheckUserData(1) - conn, ok := ud.Value.(*DBConnection) - if !ok { - L.Push(lua.LNil) - L.Push(lua.LString("invalid database connection")) - return 2 - } + ud := L.CheckUserData(1) + conn, ok := ud.Value.(*DBConnection) + if !ok { + L.Push(lua.LNil) + L.Push(lua.LString("invalid database connection")) + return 2 + } - query := L.CheckString(2) + query := L.CheckString(2) - var args []any - if L.GetTop() >= 3 { - params := L.CheckTable(3) - params.ForEach(func(k lua.LValue, v lua.LValue) { - args = append(args, ConvertLuaTypesToGolang(v)) - }) - } + var args []any + if L.GetTop() >= 3 { + params := L.CheckTable(3) + params.ForEach(func(k lua.LValue, v lua.LValue) { + args = append(args, ConvertLuaTypesToGolang(v)) + }) + } - if conn.log { - conn.logger.Info("DB QueryRow", - slog.String("query", query), - slog.Any("params", args)) - } + if conn.log { + conn.logger.Info("DB QueryRow", + slog.String("query", query), + slog.Any("params", args)) + } - mtx := getDBMutex(conn.dbPath) - mtx.RLock() - defer mtx.RUnlock() + mtx := getDBMutex(conn.dbPath) + mtx.RLock() + defer mtx.RUnlock() - db, err := sql.Open("sqlite", conn.dbPath+"?_busy_timeout=5000&_journal_mode=WAL&_sync=NORMAL&_cache_size=-10000") - if err != nil { - L.Push(lua.LNil) - L.Push(lua.LString(err.Error())) - return 2 - } - defer db.Close() + db, err := sql.Open("sqlite", conn.dbPath+"?_busy_timeout=5000&_journal_mode=WAL&_sync=NORMAL&_cache_size=-10000") + if err != nil { + L.Push(lua.LNil) + L.Push(lua.LString(err.Error())) + return 2 + } + defer db.Close() - row := db.QueryRow(query, args...) + row := db.QueryRow(query, args...) columns := []string{} stmt, err := db.Prepare(query) @@ -278,36 +278,36 @@ func dbQueryRow(L *lua.LState) int { columns = append(columns, c) } - colCount := len(columns) - values := make([]any, colCount) - valuePtrs := make([]any, colCount) - for i := range columns { - valuePtrs[i] = &values[i] - } + colCount := len(columns) + values := make([]any, colCount) + valuePtrs := make([]any, colCount) + for i := range columns { + valuePtrs[i] = &values[i] + } - err = row.Scan(valuePtrs...) - if err != nil { - if err == sql.ErrNoRows { - L.Push(lua.LNil) - return 1 - } - L.Push(lua.LNil) - L.Push(lua.LString(fmt.Sprintf("scan failed: %v", err))) - return 2 - } + err = row.Scan(valuePtrs...) + if err != nil { + if err == sql.ErrNoRows { + L.Push(lua.LNil) + return 1 + } + L.Push(lua.LNil) + L.Push(lua.LString(fmt.Sprintf("scan failed: %v", err))) + return 2 + } - rowTable := L.NewTable() - for i, col := range columns { - val := values[i] - if val == nil { - L.SetField(rowTable, col, lua.LNil) - } else { - L.SetField(rowTable, col, ConvertGolangTypesToLua(L, val)) - } - } + rowTable := L.NewTable() + for i, col := range columns { + val := values[i] + if val == nil { + L.SetField(rowTable, col, lua.LNil) + } else { + L.SetField(rowTable, col, ConvertGolangTypesToLua(L, val)) + } + } - L.Push(rowTable) - return 1 + L.Push(rowTable) + return 1 } func dbQuery(L *lua.LState) int { diff --git a/src/internal/server/sv2/handle.go b/src/internal/server/sv2/handle.go new file mode 100644 index 0000000..17685c8 --- /dev/null +++ b/src/internal/server/sv2/handle.go @@ -0,0 +1,12 @@ +package sv2 + +import ( + "context" + "net/http" + + "github.com/akyaiy/GoSally-mvp/src/internal/server/rpc" +) + +func (h *Handler) Handle(_ context.Context, sid string, r *http.Request, req *rpc.RPCRequest) *rpc.RPCResponse { + return nil +} diff --git a/src/internal/server/sv2/server.go b/src/internal/server/sv2/server.go new file mode 100644 index 0000000..b1957c1 --- /dev/null +++ b/src/internal/server/sv2/server.go @@ -0,0 +1,43 @@ +// SV2 works with binaries, scripts, and anything else that has access to stdin/stdout. +// Modules run in a separate process and communicate via I/O. +package sv2 + +import ( + "regexp" + + "github.com/akyaiy/GoSally-mvp/src/internal/core/corestate" + "github.com/akyaiy/GoSally-mvp/src/internal/engine/app" +) + +// HandlerV2InitStruct structure is only for initialization +type HandlerInitStruct struct { + Ver string + CS *corestate.CoreState + X *app.AppX + AllowedCmd *regexp.Regexp +} + +type Handler struct { + cs *corestate.CoreState + x *app.AppX + + // allowedCmd and listAllowedCmd are regular expressions used to validate command names. + allowedCmd *regexp.Regexp + + ver string +} + +func InitServer(o *HandlerInitStruct) *Handler { + return &Handler{ + cs: o.CS, + x: o.X, + allowedCmd: o.AllowedCmd, + ver: o.Ver, + } +} + +// GetVersion returns the API version of the HandlerV1, which is set during initialization. +// This version is used to identify the API version in the request routing. +func (h *Handler) GetVersion() string { + return h.ver +}