diff --git a/server/go.mod b/server/go.mod index 256c8c58..f52df2b2 100644 --- a/server/go.mod +++ b/server/go.mod @@ -9,8 +9,6 @@ require ( github.com/gogf/gf v1.16.9 github.com/google/uuid v1.6.0 github.com/joho/godotenv v1.5.1 - github.com/tidwall/gjson v1.17.1 - github.com/tidwall/sjson v1.2.5 ) require ( @@ -34,8 +32,6 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect - github.com/tidwall/match v1.1.1 // indirect - github.com/tidwall/pretty v1.2.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect go.opentelemetry.io/otel v1.14.0 // indirect diff --git a/server/go.sum b/server/go.sum index ba727db0..e7389abf 100644 --- a/server/go.sum +++ b/server/go.sum @@ -101,15 +101,6 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= -github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U= -github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= -github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= -github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= -github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= -github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= -github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= -github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= diff --git a/server/internal/http_server.go b/server/internal/http_server.go index da39d05b..8d6fed69 100644 --- a/server/internal/http_server.go +++ b/server/internal/http_server.go @@ -23,8 +23,6 @@ import ( "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/gogf/gf/crypto/gmd5" - "github.com/tidwall/gjson" - "github.com/tidwall/sjson" ) type HttpServer struct { @@ -388,7 +386,13 @@ func (s *HttpServer) processProperty(req *StartReq) (propertyJsonFile string, lo return } - propertyJson := string(content) + // Unmarshal the JSON content into a map + var propertyJson map[string]interface{} + err = json.Unmarshal(content, &propertyJson) + if err != nil { + slog.Error("handlerStart unmarshal property.json failed", "err", err, "requestId", req.RequestId, logTag) + return + } // Get graph name graphName := req.GraphName @@ -407,15 +411,24 @@ func (s *HttpServer) processProperty(req *StartReq) (propertyJsonFile string, lo } } - graph := fmt.Sprintf(`_ten.predefined_graphs.#(name=="%s")`, graphName) + // Locate the predefined graphs array + tenSection, ok := propertyJson["_ten"].(map[string]interface{}) + if !ok { + slog.Error("Invalid format: _ten section missing", "requestId", req.RequestId, logTag) + return + } - // Get the array of graphs - graphs := gjson.Get(propertyJson, "_ten.predefined_graphs").Array() + predefinedGraphs, ok := tenSection["predefined_graphs"].([]interface{}) + if !ok { + slog.Error("Invalid format: predefined_graphs missing or not an array", "requestId", req.RequestId, logTag) + return + } - // Create a new array for graphs that match the name - var newGraphs []gjson.Result - for _, graph := range graphs { - if graph.Get("name").String() == graphName { + // Filter the graph with the matching name + var newGraphs []interface{} + for _, graph := range predefinedGraphs { + graphMap, ok := graph.(map[string]interface{}) + if ok && graphMap["name"] == graphName { newGraphs = append(newGraphs, graph) } } @@ -426,27 +439,30 @@ func (s *HttpServer) processProperty(req *StartReq) (propertyJsonFile string, lo return } - // Set the array of graphs directly using sjson.Set - graphData := make([]interface{}, len(newGraphs)) - for i, graph := range newGraphs { - graphData[i] = graph.Value() // Convert gjson.Result to interface{} - } - // Replace the predefined_graphs array with the filtered array - propertyJson, _ = sjson.Set(propertyJson, "_ten.predefined_graphs", graphData) + tenSection["predefined_graphs"] = newGraphs // Automatically start on launch - propertyJson, _ = sjson.Set(propertyJson, fmt.Sprintf(`%s.auto_start`, graph), true) + for _, graph := range newGraphs { + graphMap, _ := graph.(map[string]interface{}) + graphMap["auto_start"] = true + } // Set additional properties to property.json for extensionName, props := range req.Properties { - if extKey := extensionName; extKey != "" { + if extensionName != "" { for prop, val := range props { - // Construct the path - path := fmt.Sprintf(`%s.nodes.#(name=="%s").property.%s`, graph, extKey, prop) - propertyJson, err = sjson.Set(propertyJson, path, val) - if err != nil { - slog.Error("handlerStart set property failed", "err", err, "graph", graphName, "extensionName", extensionName, "prop", prop, "val", val, "requestId", req.RequestId, logTag) + // Construct the path in the nested graph structure + for _, graph := range newGraphs { + graphMap, _ := graph.(map[string]interface{}) + nodes, _ := graphMap["nodes"].([]interface{}) + for _, node := range nodes { + nodeMap, _ := node.(map[string]interface{}) + if nodeMap["name"] == extensionName { + properties := nodeMap["property"].(map[string]interface{}) + properties[prop] = val + } + } } } } @@ -454,17 +470,36 @@ func (s *HttpServer) processProperty(req *StartReq) (propertyJsonFile string, lo // Set start parameters to property.json for key, props := range startPropMap { - if val := getFieldValue(req, key); val != "" { + val := getFieldValue(req, key) + if val != "" { for _, prop := range props { - propertyJson, _ = sjson.Set(propertyJson, fmt.Sprintf(`%s.nodes.#(name=="%s").property.%s`, graph, prop.ExtensionName, prop.Property), val) + // Set each start parameter to the appropriate graph and property + for _, graph := range newGraphs { + graphMap, _ := graph.(map[string]interface{}) + nodes, _ := graphMap["nodes"].([]interface{}) + for _, node := range nodes { + nodeMap, _ := node.(map[string]interface{}) + if nodeMap["name"] == prop.ExtensionName { + properties := nodeMap["property"].(map[string]interface{}) + properties[prop.Property] = val + } + } + } } } } + // Marshal the modified JSON back to a string + modifiedPropertyJson, err := json.MarshalIndent(propertyJson, "", " ") + if err != nil { + slog.Error("handlerStart marshal modified JSON failed", "err", err, "requestId", req.RequestId, logTag) + return + } + ts := time.Now().Format("20060102_150405_000") propertyJsonFile = fmt.Sprintf("%s/property-%s-%s.json", s.config.LogPath, url.QueryEscape(req.ChannelName), ts) logFile = fmt.Sprintf("%s/app-%s-%s.log", s.config.LogPath, url.QueryEscape(req.ChannelName), ts) - os.WriteFile(propertyJsonFile, []byte(propertyJson), 0644) + os.WriteFile(propertyJsonFile, []byte(modifiedPropertyJson), 0644) return }