Files
3x-ui-agent/internal/web/controller/probe_notify.go
T

69 lines
2.1 KiB
Go

package controller
import (
"encoding/json"
"net/http"
"github.com/mhsanaei/3x-ui/v3/internal/web/entity"
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
"github.com/mhsanaei/3x-ui/v3/internal/web/service/probenotify"
"github.com/gin-gonic/gin"
)
// Item 7 B6: the notification settings surface — the notify_* block
// (offline grace, traffic step, expiry window, login alerts, the
// webhook channel and the two message templates), a test send, and
// the cf-probe retirement coverage the operator checks before
// standing the old probe fleet down. Like the rest of the probe
// surfaces these endpoints answer in the panel's Msg envelope and
// take effect immediately; validation refusals are operator-facing
// text, shown verbatim.
type ProbeNotifyController struct{}
func NewProbeNotifyController(g *gin.RouterGroup) *ProbeNotifyController {
pc := &ProbeNotifyController{}
g.GET("/probe/notify", pc.get)
g.PUT("/probe/notify", pc.put)
g.POST("/probe/notify/test", pc.test)
return pc
}
type probeNotifyPayload struct {
Settings probenotify.SettingsView `json:"settings"`
Coverage []service.ProbeCoverageRow `json:"coverage"`
}
func (pc *ProbeNotifyController) get(c *gin.Context) {
jsonObj(c, probeNotifyPayload{
Settings: probenotify.GetSettings(),
Coverage: service.ProbeAgentCoverage(),
}, nil)
}
func (pc *ProbeNotifyController) put(c *gin.Context) {
var body map[string]json.RawMessage
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusOK, entity.Msg{Success: false, Msg: "请求格式不对"})
return
}
if err := probenotify.SaveSettings(body); err != nil {
// SaveSettings validates everything before writing anything;
// its errors are the operator's answer (a range, a malformed
// header block), not machine faults.
c.JSON(http.StatusOK, entity.Msg{Success: false, Msg: err.Error()})
return
}
jsonMsg(c, "", nil)
}
func (pc *ProbeNotifyController) test(c *gin.Context) {
sent, err := probenotify.Test()
if err != nil {
c.JSON(http.StatusOK, entity.Msg{Success: false, Msg: err.Error()})
return
}
jsonObj(c, gin.H{"sent": sent}, nil)
}