From 45e541ac00326dbd8c564fdf83b207994fb8c253 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 31 Jul 2025 21:30:59 +0300 Subject: [PATCH] update Config structure and add node name parameter --- hooks/initial.go | 1 + internal/engine/config/compositor.go | 5 +++-- internal/engine/config/config.go | 9 +++++++-- internal/engine/logs/logger.go | 1 + internal/server/sv1/lua_handler.go | 12 ++++++------ internal/server/sv1/path.go | 2 +- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/hooks/initial.go b/hooks/initial.go index 5526413..84ca4bf 100644 --- a/hooks/initial.go +++ b/hooks/initial.go @@ -185,6 +185,7 @@ func Init6Hook(cs *corestate.CoreState, x *app.AppX) { } x.Config.Conf.Log.Level = &logs.Levels.Fallback } + x.Log.Printf("Starting \"%s\" node", *x.Config.Conf.Node.Name) } func Init7Hook(cs *corestate.CoreState, x *app.AppX) { diff --git a/internal/engine/config/compositor.go b/internal/engine/config/compositor.go index a1f3d63..ba9c188 100644 --- a/internal/engine/config/compositor.go +++ b/internal/engine/config/compositor.go @@ -43,8 +43,9 @@ func (c *Compositor) LoadConf(path string) error { v.SetConfigType("yaml") // defaults - v.SetDefault("mode", "dev") - v.SetDefault("com_dir", "./com/") + v.SetDefault("node.name", "noname") + v.SetDefault("node.mode", "dev") + v.SetDefault("node.com_dir", "./com/") v.SetDefault("http_server.address", "0.0.0.0") v.SetDefault("http_server.port", "8080") v.SetDefault("http_server.timeout", "5s") diff --git a/internal/engine/config/config.go b/internal/engine/config/config.go index 98d445d..3419aa1 100644 --- a/internal/engine/config/config.go +++ b/internal/engine/config/config.go @@ -18,8 +18,7 @@ type Compositor struct { } type Conf struct { - Mode *string `mapstructure:"mode"` - ComDir *string `mapstructure:"com_dir"` + Node *Node `mapstructure:"node"` HTTPServer *HTTPServer `mapstructure:"http_server"` TLS *TLS `mapstructure:"tls"` Updates *Updates `mapstructure:"updates"` @@ -27,6 +26,12 @@ type Conf struct { DisableWarnings *[]string `mapstructure:"disable_warnings"` } +type Node struct { + Mode *string `mapstructure:"mode"` + Name *string `mapstructure:"name"` + ComDir *string `mapstructure:"com_dir"` +} + type HTTPServer struct { Address *string `mapstructure:"address"` Port *string `mapstructure:"port"` diff --git a/internal/engine/logs/logger.go b/internal/engine/logs/logger.go index 5bd8746..23abeea 100644 --- a/internal/engine/logs/logger.go +++ b/internal/engine/logs/logger.go @@ -16,6 +16,7 @@ import ( ) var GlobalLevel slog.Level + type levelsStruct struct { Available []string Fallback string diff --git a/internal/server/sv1/lua_handler.go b/internal/server/sv1/lua_handler.go index d2ed5f2..6e2772d 100644 --- a/internal/server/sv1/lua_handler.go +++ b/internal/server/sv1/lua_handler.go @@ -27,11 +27,11 @@ func addInitiatorHeaders(req *http.Request, headers http.Header) { headers.Set("X-Initiator-Referer", req.Referer()) } -// A small reminder: this code is only at the MVP stage, -// and some parts of the code may cause shock from the -// incompetence of the developer. But, in the end, -// this code is just an idea. If there is a desire to -// contribute to the development of the code, +// A small reminder: this code is only at the MVP stage, +// and some parts of the code may cause shock from the +// incompetence of the developer. But, in the end, +// this code is just an idea. If there is a desire to +// contribute to the development of the code, // I will be only glad. // TODO: make this huge function more harmonious by dividing responsibilities func (h *HandlerV1) handleLUA(r *http.Request, req *rpc.RPCRequest, path string) *rpc.RPCResponse { @@ -213,7 +213,7 @@ func (h *HandlerV1) handleLUA(r *http.Request, req *rpc.RPCRequest, path string) L.SetField(net, "Http", netHttp) L.SetGlobal("Net", net) - prep := filepath.Join(*h.x.Config.Conf.ComDir, "_prepare.lua") + prep := filepath.Join(*h.x.Config.Conf.Node.ComDir, "_prepare.lua") if _, err := os.Stat(prep); err == nil { if err := L.DoFile(prep); err != nil { return rpc.NewError(rpc.ErrInternalError, err.Error(), req.ID) diff --git a/internal/server/sv1/path.go b/internal/server/sv1/path.go index 3f28b42..dc35b6c 100644 --- a/internal/server/sv1/path.go +++ b/internal/server/sv1/path.go @@ -16,7 +16,7 @@ func (h *HandlerV1) resolveMethodPath(method string) (string, error) { parts := strings.Split(method, ">") relPath := filepath.Join(parts...) + ".lua" - fullPath := filepath.Join(*h.x.Config.Conf.ComDir, relPath) + fullPath := filepath.Join(*h.x.Config.Conf.Node.ComDir, relPath) if _, err := os.Stat(fullPath); os.IsNotExist(err) { return "", errors.New(rpc.ErrMethodNotFoundS)