Browse Source

feat: Add SysInfoVer endpoint and AppService for version retrieval

lejianwen 10 months ago
parent
commit
ab231b3fed
6 changed files with 71 additions and 7 deletions
  1. 2 7
      http/controller/api/index.go
  2. 6 0
      http/controller/api/peer.go
  3. 1 0
      http/router/api.go
  4. 28 0
      service/app.go
  5. 33 0
      service/app_test.go
  6. 1 0
      service/service.go

+ 2 - 7
http/controller/api/index.go

@@ -7,7 +7,6 @@ import (
7 7
 	"github.com/lejianwen/rustdesk-api/v2/model"
8 8
 	"github.com/lejianwen/rustdesk-api/v2/service"
9 9
 	"net/http"
10
-	"os"
11 10
 	"time"
12 11
 )
13 12
 
@@ -74,13 +73,9 @@ func (i *Index) Heartbeat(c *gin.Context) {
74 73
 // @Router /version [get]
75 74
 func (i *Index) Version(c *gin.Context) {
76 75
 	//读取resources/version文件
77
-	v, err := os.ReadFile("resources/version")
78
-	if err != nil {
79
-		response.Fail(c, 101, err.Error())
80
-		return
81
-	}
76
+	v := service.AllService.AppService.GetAppVersion()
82 77
 	response.Success(
83 78
 		c,
84
-		string(v),
79
+		v,
85 80
 	)
86 81
 }

+ 6 - 0
http/controller/api/peer.go

@@ -56,3 +56,9 @@ func (p *Peer) SysInfo(c *gin.Context) {
56 56
 	//直接响应文本
57 57
 	c.String(http.StatusOK, "SYSINFO_UPDATED")
58 58
 }
59
+
60
+func (p *Peer) SysInfoVer(c *gin.Context) {
61
+	//读取resources/version文件
62
+	v := service.AllService.AppService.GetAppVersion()
63
+	c.String(http.StatusOK, v)
64
+}

+ 1 - 0
http/router/api.go

@@ -53,6 +53,7 @@ func ApiInit(g *gin.Engine) {
53 53
 		pe := &api.Peer{}
54 54
 		//提交系统信息
55 55
 		frg.POST("/sysinfo", pe.SysInfo)
56
+		frg.POST("/sysinfo_ver", pe.SysInfoVer)
56 57
 	}
57 58
 
58 59
 	if global.Config.App.WebClient == 1 {

+ 28 - 0
service/app.go

@@ -0,0 +1,28 @@
1
+package service
2
+
3
+import (
4
+	"os"
5
+	"sync"
6
+)
7
+
8
+type AppService struct {
9
+}
10
+
11
+var version = ""
12
+
13
+var once = &sync.Once{}
14
+
15
+func (a *AppService) GetAppVersion() string {
16
+	if version != "" {
17
+		return version
18
+	}
19
+	once.Do(func() {
20
+		v, err := os.ReadFile("resources/version")
21
+		if err != nil {
22
+			return
23
+		}
24
+		version = string(v)
25
+
26
+	})
27
+	return version
28
+}

+ 33 - 0
service/app_test.go

@@ -0,0 +1,33 @@
1
+package service
2
+
3
+import (
4
+	"sync"
5
+	"testing"
6
+)
7
+
8
+// TestGetAppVersion
9
+func TestGetAppVersion(t *testing.T) {
10
+	s := &AppService{}
11
+	v := s.GetAppVersion()
12
+	// 打印结果
13
+	t.Logf("App Version: %s", v)
14
+}
15
+
16
+func TestMultipleGetAppVersion(t *testing.T) {
17
+	s := &AppService{}
18
+	//并发测试
19
+	// 使用 WaitGroup 等待所有 goroutine 完成
20
+	wg := sync.WaitGroup{}
21
+	wg.Add(10) // 启动 10 个 goroutine
22
+	// 启动 10 个 goroutine
23
+	for i := 0; i < 10; i++ {
24
+		go func() {
25
+			defer wg.Done() // 完成后减少计数
26
+			v := s.GetAppVersion()
27
+			// 打印结果
28
+			t.Logf("App Version: %s", v)
29
+		}()
30
+	}
31
+	// 等待所有 goroutine 完成
32
+	wg.Wait()
33
+}

+ 1 - 0
service/service.go

@@ -23,6 +23,7 @@ type Service struct {
23 23
 	*ShareRecordService
24 24
 	*ServerCmdService
25 25
 	*LdapService
26
+	*AppService
26 27
 }
27 28
 
28 29
 type Dependencies struct {