update: 单例模式

This commit is contained in:
Akkia 2022-05-18 23:54:22 +08:00
parent e341960223
commit 725841cfed
No known key found for this signature in database
GPG Key ID: DABE9A4AB2DD7EF3
2 changed files with 13 additions and 13 deletions

View File

@ -33,12 +33,11 @@ func (v *apiV1) serve() {
// query: tag (服务器分组)
func (v *apiV1) serverList(c *gin.Context) {
tag := c.Query("tag")
serverAPI := &singleton.ServerAPI{}
if tag != "" {
c.JSON(200, serverAPI.GetListByTag(tag))
c.JSON(200, singleton.ServerAPI.GetListByTag(tag))
return
}
c.JSON(200, serverAPI.GetAllList())
c.JSON(200, singleton.ServerAPI.GetAllList())
}
// serverDetails 获取服务器信息 不传入Query参数则获取全部
@ -56,14 +55,13 @@ func (v *apiV1) serverDetails(c *gin.Context) {
}
}
tag := c.Query("tag")
serverAPI := &singleton.ServerAPI{}
if tag != "" {
c.JSON(200, serverAPI.GetStatusByTag(tag))
c.JSON(200, singleton.ServerAPI.GetStatusByTag(tag))
return
}
if len(idList) != 0 {
c.JSON(200, serverAPI.GetStatusByIDList(idList))
c.JSON(200, singleton.ServerAPI.GetStatusByIDList(idList))
return
}
c.JSON(200, serverAPI.GetAllStatus())
c.JSON(200, singleton.ServerAPI.GetAllStatus())
}

View File

@ -10,9 +10,11 @@ var (
ApiTokenList = make(map[string]*model.ApiToken)
UserIDToApiTokenList = make(map[uint64][]string)
ApiLock sync.RWMutex
ServerAPI = &ServerAPIService{}
)
type ServerAPI struct{}
type ServerAPIService struct{}
// CommonResponse 常规返回结构 包含状态码 和 状态信息
type CommonResponse struct {
@ -64,7 +66,7 @@ func LoadAPI() {
}
// GetStatusByIDList 获取传入IDList的服务器状态信息
func (s *ServerAPI) GetStatusByIDList(idList []uint64) *ServerStatusResponse {
func (s *ServerAPIService) GetStatusByIDList(idList []uint64) *ServerStatusResponse {
res := &ServerStatusResponse{}
res.Result = make([]*StatusResponse, 0)
@ -99,12 +101,12 @@ func (s *ServerAPI) GetStatusByIDList(idList []uint64) *ServerStatusResponse {
}
// GetStatusByTag 获取传入分组的所有服务器状态信息
func (s *ServerAPI) GetStatusByTag(tag string) *ServerStatusResponse {
func (s *ServerAPIService) GetStatusByTag(tag string) *ServerStatusResponse {
return s.GetStatusByIDList(ServerTagToIDList[tag])
}
// GetAllStatus 获取所有服务器状态信息
func (s *ServerAPI) GetAllStatus() *ServerStatusResponse {
func (s *ServerAPIService) GetAllStatus() *ServerStatusResponse {
res := &ServerStatusResponse{}
res.Result = make([]*StatusResponse, 0)
ServerLock.RLock()
@ -138,7 +140,7 @@ func (s *ServerAPI) GetAllStatus() *ServerStatusResponse {
}
// GetListByTag 获取传入分组的所有服务器信息
func (s *ServerAPI) GetListByTag(tag string) *ServerInfoResponse {
func (s *ServerAPIService) GetListByTag(tag string) *ServerInfoResponse {
res := &ServerInfoResponse{}
res.Result = make([]*CommonServerInfo, 0)
@ -168,7 +170,7 @@ func (s *ServerAPI) GetListByTag(tag string) *ServerInfoResponse {
}
// GetAllList 获取所有服务器信息
func (s *ServerAPI) GetAllList() *ServerInfoResponse {
func (s *ServerAPIService) GetAllList() *ServerInfoResponse {
res := &ServerInfoResponse{}
res.Result = make([]*CommonServerInfo, 0)