feat: Add SysInfoVer endpoint and AppService for version retrieval

This commit is contained in:
lejianwen
2025-04-07 16:38:21 +08:00
parent 605e88e04c
commit ae59b6a733
6 changed files with 71 additions and 7 deletions

View File

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

View File

@@ -56,3 +56,9 @@ func (p *Peer) SysInfo(c *gin.Context) {
//直接响应文本 //直接响应文本
c.String(http.StatusOK, "SYSINFO_UPDATED") 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)
}

View File

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

28
service/app.go Normal file
View File

@@ -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
}

33
service/app_test.go Normal file
View File

@@ -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()
}

View File

@@ -23,6 +23,7 @@ type Service struct {
*ShareRecordService *ShareRecordService
*ServerCmdService *ServerCmdService
*LdapService *LdapService
*AppService
} }
type Dependencies struct { type Dependencies struct {