diff --git a/cmd/dashboard/controller/api_v1.go b/cmd/dashboard/controller/api_v1.go index 335e345..999fd92 100644 --- a/cmd/dashboard/controller/api_v1.go +++ b/cmd/dashboard/controller/api_v1.go @@ -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()) } diff --git a/service/singleton/api.go b/service/singleton/api.go index 1335f31..060ab02 100644 --- a/service/singleton/api.go +++ b/service/singleton/api.go @@ -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)