diff options
Diffstat (limited to 'web/controller')
| -rw-r--r-- | web/controller/server.go | 61 | ||||
| -rw-r--r-- | web/controller/xray_setting.go | 8 |
2 files changed, 31 insertions, 38 deletions
diff --git a/web/controller/server.go b/web/controller/server.go index 3b93afd9..169a1ae7 100644 --- a/web/controller/server.go +++ b/web/controller/server.go @@ -21,17 +21,14 @@ type ServerController struct { serverService service.ServerService settingService service.SettingService - lastStatus *service.Status - lastGetStatusTime time.Time + lastStatus *service.Status lastVersions []string - lastGetVersionsTime time.Time + lastGetVersionsTime int64 // unix seconds } func NewServerController(g *gin.RouterGroup) *ServerController { - a := &ServerController{ - lastGetStatusTime: time.Now(), - } + a := &ServerController{} a.initRouter(g) a.startTask() return a @@ -40,7 +37,7 @@ func NewServerController(g *gin.RouterGroup) *ServerController { func (a *ServerController) initRouter(g *gin.RouterGroup) { g.GET("/status", a.status) - g.GET("/cpuHistory", a.getCpuHistory) + g.GET("/cpuHistory/:bucket", a.getCpuHistoryBucket) g.GET("/getXrayVersion", a.getXrayVersion) g.GET("/getConfigJson", a.getConfigJson) g.GET("/getDb", a.getDb) @@ -79,35 +76,34 @@ func (a *ServerController) startTask() { }) } -func (a *ServerController) status(c *gin.Context) { - a.lastGetStatusTime = time.Now() - - jsonObj(c, a.lastStatus, nil) -} +func (a *ServerController) status(c *gin.Context) { jsonObj(c, a.lastStatus, nil) } -// getCpuHistory returns recent CPU utilization points. -// Query param q=minutes (int). Bounds: 1..360 (6 hours). Defaults to 60. -func (a *ServerController) getCpuHistory(c *gin.Context) { - minsStr := c.Query("q") - mins := 60 - if minsStr != "" { - if v, err := strconv.Atoi(minsStr); err == nil { - mins = v - } +func (a *ServerController) getCpuHistoryBucket(c *gin.Context) { + bucketStr := c.Param("bucket") + bucket, err := strconv.Atoi(bucketStr) + if err != nil || bucket <= 0 { + jsonMsg(c, "invalid bucket", fmt.Errorf("bad bucket")) + return } - if mins < 1 { - mins = 1 + allowed := map[int]bool{ + 2: true, // Real-time view + 30: true, // 30s intervals + 60: true, // 1m intervals + 120: true, // 2m intervals + 180: true, // 3m intervals + 300: true, // 5m intervals } - if mins > 360 { - mins = 360 + if !allowed[bucket] { + jsonMsg(c, "invalid bucket", fmt.Errorf("unsupported bucket")) + return } - res := a.serverService.GetCpuHistory(mins) - jsonObj(c, res, nil) + points := a.serverService.AggregateCpuHistory(bucket, 60) + jsonObj(c, points, nil) } func (a *ServerController) getXrayVersion(c *gin.Context) { - now := time.Now() - if now.Sub(a.lastGetVersionsTime) <= time.Minute { + now := time.Now().Unix() + if now-a.lastGetVersionsTime <= 60 { // 1 minute cache jsonObj(c, a.lastVersions, nil) return } @@ -119,7 +115,7 @@ func (a *ServerController) getXrayVersion(c *gin.Context) { } a.lastVersions = versions - a.lastGetVersionsTime = time.Now() + a.lastGetVersionsTime = now jsonObj(c, versions, nil) } @@ -137,7 +133,6 @@ func (a *ServerController) updateGeofile(c *gin.Context) { } func (a *ServerController) stopXrayService(c *gin.Context) { - a.lastGetStatusTime = time.Now() err := a.serverService.StopXrayService() if err != nil { jsonMsg(c, I18nWeb(c, "pages.xray.stopError"), err) @@ -253,9 +248,7 @@ func (a *ServerController) importDB(c *gin.Context) { defer file.Close() // Always restart Xray before return defer a.serverService.RestartXrayService() - defer func() { - a.lastGetStatusTime = time.Now() - }() + // lastGetStatusTime removed; no longer needed // Import it err = a.serverService.ImportDB(file) if err != nil { diff --git a/web/controller/xray_setting.go b/web/controller/xray_setting.go index 5b2d1036..2b5e0db1 100644 --- a/web/controller/xray_setting.go +++ b/web/controller/xray_setting.go @@ -23,13 +23,13 @@ func NewXraySettingController(g *gin.RouterGroup) *XraySettingController { func (a *XraySettingController) initRouter(g *gin.RouterGroup) { g = g.Group("/xray") + g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig) + g.GET("/getOutboundsTraffic", a.getOutboundsTraffic) + g.GET("/getXrayResult", a.getXrayResult) g.POST("/", a.getXraySetting) - g.POST("/update", a.updateSetting) - g.GET("/getXrayResult", a.getXrayResult) - g.GET("/getDefaultJsonConfig", a.getDefaultXrayConfig) g.POST("/warp/:action", a.warp) - g.GET("/getOutboundsTraffic", a.getOutboundsTraffic) + g.POST("/update", a.updateSetting) g.POST("/resetOutboundsTraffic", a.resetOutboundsTraffic) } |
