diff --git a/http/controller/api/index.go b/http/controller/api/index.go index c5a4be7..57573d6 100644 --- a/http/controller/api/index.go +++ b/http/controller/api/index.go @@ -7,7 +7,6 @@ import ( "github.com/lejianwen/rustdesk-api/v2/model" "github.com/lejianwen/rustdesk-api/v2/service" "net/http" - "os" "time" ) @@ -74,13 +73,9 @@ func (i *Index) Heartbeat(c *gin.Context) { // @Router /version [get] func (i *Index) Version(c *gin.Context) { //读取resources/version文件 - v, err := os.ReadFile("resources/version") - if err != nil { - response.Fail(c, 101, err.Error()) - return - } + v := service.AllService.AppService.GetAppVersion() response.Success( c, - string(v), + v, ) } diff --git a/http/controller/api/peer.go b/http/controller/api/peer.go index d47613b..b2565cf 100644 --- a/http/controller/api/peer.go +++ b/http/controller/api/peer.go @@ -56,3 +56,9 @@ func (p *Peer) SysInfo(c *gin.Context) { //直接响应文本 c.String(http.StatusOK, "SYSINFO_UPDATED") } + +func (p *Peer) SysInfoVer(c *gin.Context) { + //读取resources/version文件 + v := service.AllService.AppService.GetAppVersion() + c.String(http.StatusOK, v) +} diff --git a/http/router/api.go b/http/router/api.go index d023b2b..cddf7ef 100644 --- a/http/router/api.go +++ b/http/router/api.go @@ -53,6 +53,7 @@ func ApiInit(g *gin.Engine) { pe := &api.Peer{} //提交系统信息 frg.POST("/sysinfo", pe.SysInfo) + frg.POST("/sysinfo_ver", pe.SysInfoVer) } if global.Config.App.WebClient == 1 { diff --git a/service/app.go b/service/app.go new file mode 100644 index 0000000..1c77a9c --- /dev/null +++ b/service/app.go @@ -0,0 +1,28 @@ +package service + +import ( + "os" + "sync" +) + +type AppService struct { +} + +var version = "" + +var once = &sync.Once{} + +func (a *AppService) GetAppVersion() string { + if version != "" { + return version + } + once.Do(func() { + v, err := os.ReadFile("resources/version") + if err != nil { + return + } + version = string(v) + + }) + return version +} diff --git a/service/app_test.go b/service/app_test.go new file mode 100644 index 0000000..d014cd4 --- /dev/null +++ b/service/app_test.go @@ -0,0 +1,33 @@ +package service + +import ( + "sync" + "testing" +) + +// TestGetAppVersion +func TestGetAppVersion(t *testing.T) { + s := &AppService{} + v := s.GetAppVersion() + // 打印结果 + t.Logf("App Version: %s", v) +} + +func TestMultipleGetAppVersion(t *testing.T) { + s := &AppService{} + //并发测试 + // 使用 WaitGroup 等待所有 goroutine 完成 + wg := sync.WaitGroup{} + wg.Add(10) // 启动 10 个 goroutine + // 启动 10 个 goroutine + for i := 0; i < 10; i++ { + go func() { + defer wg.Done() // 完成后减少计数 + v := s.GetAppVersion() + // 打印结果 + t.Logf("App Version: %s", v) + }() + } + // 等待所有 goroutine 完成 + wg.Wait() +} diff --git a/service/service.go b/service/service.go index 6657a38..b5fb1bf 100644 --- a/service/service.go +++ b/service/service.go @@ -23,6 +23,7 @@ type Service struct { *ShareRecordService *ServerCmdService *LdapService + *AppService } type Dependencies struct {