初始化
This commit is contained in:
126
server/controller/monitor_controller.go
Normal file
126
server/controller/monitor_controller.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"seeyon-filesystem/middleware"
|
||||
"seeyon-filesystem/repository"
|
||||
"seeyon-filesystem/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// MonitorController 监控控制器
|
||||
type MonitorController struct {
|
||||
fileRepo *repository.FileRepository
|
||||
}
|
||||
|
||||
func NewMonitorController() *MonitorController {
|
||||
return &MonitorController{
|
||||
fileRepo: repository.NewFileRepository(),
|
||||
}
|
||||
}
|
||||
|
||||
// GetTrafficStats 获取流量统计
|
||||
// GET /api/admin/monitor/traffic
|
||||
func (c *MonitorController) GetTrafficStats(ctx *gin.Context) {
|
||||
info := middleware.GetTrafficInfo()
|
||||
utils.Success(ctx, info)
|
||||
}
|
||||
|
||||
// GetRecentRequests 获取最近请求记录
|
||||
// GET /api/admin/monitor/requests?limit=50
|
||||
func (c *MonitorController) GetRecentRequests(ctx *gin.Context) {
|
||||
limit := 50
|
||||
if l, err := parseIntQuery(ctx, "limit"); err == nil && l > 0 {
|
||||
limit = l
|
||||
}
|
||||
if limit > 500 {
|
||||
limit = 500
|
||||
}
|
||||
|
||||
requests := middleware.GetRecentRequests(limit)
|
||||
utils.Success(ctx, requests)
|
||||
}
|
||||
|
||||
// GetStorageStats 获取存储统计(按策略分组)
|
||||
// GET /api/admin/monitor/storage
|
||||
func (c *MonitorController) GetStorageStats(ctx *gin.Context) {
|
||||
stats, err := c.fileRepo.GetPolicyStats()
|
||||
if err != nil {
|
||||
utils.ServerError(ctx, "查询失败")
|
||||
return
|
||||
}
|
||||
|
||||
global, _ := c.fileRepo.GetGlobalStats()
|
||||
utils.Success(ctx, gin.H{
|
||||
"policies": stats,
|
||||
"global": global,
|
||||
})
|
||||
}
|
||||
|
||||
// ========== IP 黑名单管理 ==========
|
||||
|
||||
// GetBlacklist 获取IP黑名单
|
||||
// GET /api/admin/blacklist
|
||||
func (c *MonitorController) GetBlacklist(ctx *gin.Context) {
|
||||
ips := middleware.IPBlacklist.GetBlockedIPs()
|
||||
utils.Success(ctx, gin.H{
|
||||
"enabled": middleware.IPBlacklist.IsEnabled(),
|
||||
"ips": ips,
|
||||
})
|
||||
}
|
||||
|
||||
// SetBlacklist 设置IP黑名单(全量替换)
|
||||
// PUT /api/admin/blacklist
|
||||
func (c *MonitorController) SetBlacklist(ctx *gin.Context) {
|
||||
var req struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
IPs []string `json:"ips"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
utils.BadRequest(ctx, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
middleware.IPBlacklist.SetEnabled(req.Enabled)
|
||||
if req.IPs != nil {
|
||||
middleware.IPBlacklist.SetBlockedIPs(req.IPs)
|
||||
}
|
||||
|
||||
utils.SuccessWithMessage(ctx, "黑名单已更新", nil)
|
||||
}
|
||||
|
||||
// AddBlockedIP 添加封禁IP
|
||||
// POST /api/admin/blacklist
|
||||
func (c *MonitorController) AddBlockedIP(ctx *gin.Context) {
|
||||
var req struct {
|
||||
IP string `json:"ip" binding:"required"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
utils.BadRequest(ctx, "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
middleware.IPBlacklist.AddBlockedIP(req.IP)
|
||||
utils.SuccessWithMessage(ctx, "已添加", nil)
|
||||
}
|
||||
|
||||
// RemoveBlockedIP 移除封禁IP
|
||||
// DELETE /api/admin/blacklist/:ip
|
||||
func (c *MonitorController) RemoveBlockedIP(ctx *gin.Context) {
|
||||
ip := ctx.Param("ip")
|
||||
middleware.IPBlacklist.RemoveBlockedIP(ip)
|
||||
utils.SuccessWithMessage(ctx, "已移除", nil)
|
||||
}
|
||||
|
||||
// parseIntQuery 解析查询参数中的整数
|
||||
func parseIntQuery(ctx *gin.Context, key string) (int, error) {
|
||||
val := ctx.Query(key)
|
||||
if val == "" {
|
||||
return 0, nil
|
||||
}
|
||||
var result int
|
||||
_, err := fmt.Sscanf(val, "%d", &result)
|
||||
return result, err
|
||||
}
|
||||
Reference in New Issue
Block a user