From e11eae509f944c2f0387d2dbf862dcd5763b84da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=9B=86=E5=B7=9D?= <jichuan1@huawei.com>
Date: Fri, 6 Sep 2024 16:35:20 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20golang=E6=9C=8D=E5=8A=A1=E6=8F=90?=
 =?UTF-8?q?=E4=BE=9Bad=E6=8E=A5=E5=8F=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: 集川 <jichuan1@huawei.com>
---
 ide/server/main.go            | 75 ++++++++++++++++++++++++++++++++---
 ide/server/server-config.json |  4 ++
 ide/server/server-config.txt  |  1 -
 ide/webpack.config.js         |  4 +-
 4 files changed, 75 insertions(+), 9 deletions(-)
 create mode 100644 ide/server/server-config.json
 delete mode 100644 ide/server/server-config.txt

diff --git a/ide/server/main.go b/ide/server/main.go
index 80a242fb..5de296ef 100644
--- a/ide/server/main.go
+++ b/ide/server/main.go
@@ -45,6 +45,7 @@ import (
 	"runtime"
 	"strconv"
 	"strings"
+	"sync"
 	"time"
 )
 
@@ -52,6 +53,7 @@ const HttpPort = 9000
 
 var exPath string
 var serveInfo string
+var msgPublishData MsgPublishData
 var hdcPublicKey string
 var hdcPrivateKey *rsa.PrivateKey
 
@@ -178,6 +180,7 @@ func main() {
 		mux.HandleFunc("/application/hdcPublicKey", getHdcPublicKey)
 		mux.HandleFunc("/application/encryptHdcMsg", encryptHdcMsg)
 		mux.HandleFunc("/application/signatureHdcMsg", signatureHdcMsg)
+		mux.HandleFunc("/application/messagePublish", getMsgPublish)
 		fs := http.FileServer(http.Dir(exPath + "/"))
 		mux.Handle("/application/", http.StripPrefix("/application/", cors(fs, version)))
 		go func() {
@@ -299,14 +302,74 @@ func signatureHdcMsg(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
-func readReqServerConfig() string {
-	readServerConfig, serverConfigErr := os.ReadFile(exPath + "/server-config.txt")
-	if serverConfigErr != nil {
-		serveInfo = ""
+func parseMsgPublishFile() {
+	msgPublishData.Mux.RLock()
+	defer msgPublishData.Mux.RUnlock()
+	exist, err := PathExists(msgPublishData.FilePath)
+	if err != nil || !exist {
+		return
+	}
+	buf, err := os.ReadFile(msgPublishData.FilePath)
+	if err != nil {
+		fmt.Println("read fail", err)
+		return
+	}
+	msgPublishData.Msg = string(buf)
+}
+
+func getMsgPublish(w http.ResponseWriter, r *http.Request) {
+	w.Header().Set("Access-Control-Allow-Origin", "*")
+	w.Header().Set("Content-Type", "text/json")
+	msgPublishData.Mux.Lock()
+	data := msgPublishData.Msg
+	msgPublishData.Mux.Unlock()
+	if len(data) == 0 {
+		resp(&w)(false, -1, "msg failed", nil)
 	} else {
-		serveInfo = string(readServerConfig)
+		resp(&w)(true, 0, "success", map[string]interface{}{
+			"data": data,
+		})
+	}
+}
+
+type ServerConfig struct {
+	ServeInfo      string `json:"ServeInfo"`
+	MsgPublishFile string `json:"MsgPublishFile"`
+}
+
+type MsgPublishData struct {
+	FilePath string
+	Msg      string
+	Mux      sync.RWMutex
+}
+
+func loopUpdateMsgPublishData() {
+	loopTime := 30 * time.Second
+	timer := time.NewTimer(loopTime)
+	for {
+		select {
+		case <-timer.C:
+			parseMsgPublishFile()
+		}
+		timer.Reset(loopTime)
+	}
+}
+
+func readReqServerConfig() {
+	serverConfigBuffer, err := os.ReadFile(exPath + "/server-config.json")
+	if err != nil {
+		return
+	}
+	var sc ServerConfig
+	err = json.Unmarshal(serverConfigBuffer, &sc)
+	if err != nil {
+		return
 	}
-	return serveInfo
+	serveInfo = sc.ServeInfo
+	msgPublishData.Mux.RLock()
+	msgPublishData.FilePath = sc.MsgPublishFile
+	msgPublishData.Mux.RUnlock()
+	go loopUpdateMsgPublishData()
 }
 
 func mapToJson(m map[string]interface{}) (string, error) {
diff --git a/ide/server/server-config.json b/ide/server/server-config.json
new file mode 100644
index 00000000..637b4034
--- /dev/null
+++ b/ide/server/server-config.json
@@ -0,0 +1,4 @@
+{
+    "ServeInfo": "127.0.0.1:9100/statistics",
+    "MsgPublishFile": ""
+}
\ No newline at end of file
diff --git a/ide/server/server-config.txt b/ide/server/server-config.txt
deleted file mode 100644
index fde29681..00000000
--- a/ide/server/server-config.txt
+++ /dev/null
@@ -1 +0,0 @@
-127.0.0.1:9100/statistics
\ No newline at end of file
diff --git a/ide/webpack.config.js b/ide/webpack.config.js
index f7702c1c..55397917 100644
--- a/ide/webpack.config.js
+++ b/ide/webpack.config.js
@@ -168,8 +168,8 @@ const config = {
           to: 'wasm.json',
         },
         {
-          from: './server/server-config.txt',
-          to: 'server-config.txt',
+          from: './server/server-config.json',
+          to: 'server-config.json',
         },
       ],
     }),
-- 
2.34.1

