fix service api (#556)

* fix service api

* update ServiceResponseItem

* fix: ddns lock
This commit is contained in:
UUBulb
2024-12-05 21:00:02 +08:00
committed by GitHub
parent 2234cff5eb
commit 040c8df02e
10 changed files with 124 additions and 53 deletions

View File

@@ -60,7 +60,7 @@ func routers(r *gin.Engine, adminFrontend, userFrontend fs.FS) {
optionalAuth.GET("/ws/server", commonHandler(serverStream))
optionalAuth.GET("/server-group", commonHandler(listServerGroup))
optionalAuth.GET("/service", commonHandler(listService))
optionalAuth.GET("/service", commonHandler(showService))
optionalAuth.GET("/service/:id", commonHandler(listServiceHistory))
optionalAuth.GET("/service/server", commonHandler(listServerWithServices))
@@ -82,6 +82,7 @@ func routers(r *gin.Engine, adminFrontend, userFrontend fs.FS) {
auth.POST("/user", commonHandler(createUser))
auth.POST("/batch-delete/user", commonHandler(batchDeleteUser))
auth.GET("/service/list", commonHandler(listService))
auth.POST("/service", commonHandler(createService))
auth.PATCH("/service/:id", commonHandler(updateService))
auth.POST("/batch-delete/service", commonHandler(batchDeleteService))

View File

@@ -23,8 +23,8 @@ import (
func listDDNS(c *gin.Context) ([]*model.DDNSProfile, error) {
var ddnsProfiles []*model.DDNSProfile
singleton.DDNSCacheLock.RLock()
defer singleton.DDNSCacheLock.RUnlock()
singleton.DDNSListLock.RLock()
defer singleton.DDNSListLock.RUnlock()
if err := copier.Copy(&ddnsProfiles, &singleton.DDNSList); err != nil {
return nil, err

View File

@@ -22,8 +22,8 @@ import (
func listNAT(c *gin.Context) ([]*model.NAT, error) {
var n []*model.NAT
singleton.NATCacheRwLock.RLock()
defer singleton.NATCacheRwLock.RUnlock()
singleton.NATListLock.RLock()
defer singleton.NATListLock.RUnlock()
if err := copier.Copy(&n, &singleton.NATList); err != nil {
return nil, err

View File

@@ -20,8 +20,8 @@ import (
// @Success 200 {object} model.CommonResponse[[]model.Notification]
// @Router /notification [get]
func listNotification(c *gin.Context) ([]*model.Notification, error) {
singleton.NotificationsLock.RLock()
defer singleton.NotificationsLock.RUnlock()
singleton.NotificationSortedLock.RLock()
defer singleton.NotificationSortedLock.RUnlock()
var notifications []*model.Notification
if err := copier.Copy(&notifications, &singleton.NotificationListSorted); err != nil {

View File

@@ -13,35 +13,22 @@ import (
"gorm.io/gorm"
)
// List service
// @Summary List service
// Show service
// @Summary Show service
// @Security BearerAuth
// @Schemes
// @Description List service
// @Description Show service
// @Tags common
// @Produce json
// @Success 200 {object} model.CommonResponse[model.ServiceResponse]
// @Router /service [get]
func listService(c *gin.Context) (*model.ServiceResponse, error) {
func showService(c *gin.Context) (*model.ServiceResponse, error) {
res, err, _ := requestGroup.Do("list-service", func() (interface{}, error) {
singleton.AlertsLock.RLock()
defer singleton.AlertsLock.RUnlock()
var stats map[uint64]model.ServiceResponseItem
stats := singleton.ServiceSentinelShared.CopyStats()
var cycleTransferStats map[uint64]model.CycleTransferStats
copier.Copy(&stats, singleton.ServiceSentinelShared.LoadStats())
copier.Copy(&cycleTransferStats, singleton.AlertsCycleTransferStatsStore)
_, isMember := c.Get(model.CtxKeyAuthorizedUser)
authorized := isMember // TODO || isViewPasswordVerfied
for k, service := range stats {
if !authorized {
if !service.Service.EnableShowInService {
delete(stats, k)
continue
}
service.Service = &model.Service{Name: service.Service.Name}
stats[k] = service
}
}
return []interface {
}{
stats, cycleTransferStats,
@@ -57,6 +44,27 @@ func listService(c *gin.Context) (*model.ServiceResponse, error) {
}, nil
}
// List service
// @Summary List service
// @Security BearerAuth
// @Schemes
// @Description List service
// @Tags auth required
// @Produce json
// @Success 200 {object} model.CommonResponse[[]model.Service]
// @Router /service [get]
func listService(c *gin.Context) ([]*model.Service, error) {
singleton.ServiceSentinelShared.ServicesLock.RLock()
defer singleton.ServiceSentinelShared.ServicesLock.RUnlock()
var ss []*model.Service
if err := copier.Copy(&ss, singleton.ServiceSentinelShared.ServiceList); err != nil {
return nil, err
}
return ss, nil
}
// List service histories by server id
// @Summary List service histories by server id
// @Security BearerAuth
@@ -218,7 +226,12 @@ func createService(c *gin.Context) (uint64, error) {
return 0, err
}
return m.ID, singleton.ServiceSentinelShared.OnServiceUpdate(m)
if err := singleton.ServiceSentinelShared.OnServiceUpdate(m); err != nil {
return 0, err
}
singleton.ServiceSentinelShared.UpdateServiceList()
return m.ID, nil
}
// Update service
@@ -281,7 +294,12 @@ func updateService(c *gin.Context) (any, error) {
return nil, err
}
return nil, singleton.ServiceSentinelShared.OnServiceUpdate(m)
if err := singleton.ServiceSentinelShared.OnServiceUpdate(m); err != nil {
return nil, err
}
singleton.ServiceSentinelShared.UpdateServiceList()
return nil, nil
}
// Batch delete service
@@ -310,5 +328,6 @@ func batchDeleteService(c *gin.Context) (any, error) {
return nil, err
}
singleton.ServiceSentinelShared.OnServiceDelete(ids)
singleton.ServiceSentinelShared.UpdateServiceList()
return nil, nil
}