chore: sync local updates
This commit is contained in:
100
mengyamonitor-backend-server/internal/handler/admin.go
Normal file
100
mengyamonitor-backend-server/internal/handler/admin.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"mengyamonitor-backend-server/internal/config"
|
||||
"mengyamonitor-backend-server/internal/model"
|
||||
"mengyamonitor-backend-server/internal/store"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Admin struct {
|
||||
Store *store.Store
|
||||
Cfg *config.Config
|
||||
}
|
||||
|
||||
func (h *Admin) Auth(c *gin.Context) {
|
||||
var in model.AuthInput
|
||||
if err := c.ShouldBindJSON(&in); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
||||
return
|
||||
}
|
||||
if subtle.ConstantTimeCompare([]byte(in.Token), []byte(h.Cfg.AdminToken)) != 1 {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
func (h *Admin) ListServers(c *gin.Context) {
|
||||
list, err := h.Store.ListAll(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": list})
|
||||
}
|
||||
|
||||
func (h *Admin) CreateServer(c *gin.Context) {
|
||||
var in model.CreateServerInput
|
||||
if err := c.ShouldBindJSON(&in); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
||||
return
|
||||
}
|
||||
s, err := h.Store.Create(c.Request.Context(), in)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{"data": s})
|
||||
}
|
||||
|
||||
func (h *Admin) UpdateServer(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
var in model.UpdateServerInput
|
||||
if err := c.ShouldBindJSON(&in); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
||||
return
|
||||
}
|
||||
s, err := h.Store.Update(c.Request.Context(), id, in)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": s})
|
||||
}
|
||||
|
||||
func (h *Admin) DeleteServer(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if err := h.Store.Delete(c.Request.Context(), id); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
func (h *Admin) Reorder(c *gin.Context) {
|
||||
var in model.ReorderInput
|
||||
if err := c.ShouldBindJSON(&in); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
||||
return
|
||||
}
|
||||
if err := h.Store.Reorder(c.Request.Context(), in.IDs); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
70
mengyamonitor-backend-server/internal/handler/public.go
Normal file
70
mengyamonitor-backend-server/internal/handler/public.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"mengyamonitor-backend-server/internal/model"
|
||||
"mengyamonitor-backend-server/internal/store"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Public struct {
|
||||
Store *store.Store
|
||||
}
|
||||
|
||||
// publicServer hides agent_key from unauthenticated API consumers.
|
||||
type publicServer struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
Enabled bool `json:"enabled"`
|
||||
SortOrder int `json:"sortOrder"`
|
||||
CreatedAt time.Time `json:"createdAt,omitempty"`
|
||||
}
|
||||
|
||||
func toPublicServers(in []model.MonitoredServer) []publicServer {
|
||||
out := make([]publicServer, 0, len(in))
|
||||
for _, s := range in {
|
||||
out = append(out, publicServer{
|
||||
ID: s.ID, Name: s.Name, URL: s.URL, Enabled: s.Enabled,
|
||||
SortOrder: s.SortOrder, CreatedAt: s.CreatedAt,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (h *Public) Health(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
|
||||
func (h *Public) ListServers(c *gin.Context) {
|
||||
list, err := h.Store.ListEnabled(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": toPublicServers(list)})
|
||||
}
|
||||
|
||||
func (h *Public) Root(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"service": "萌芽监控 — 中心服务",
|
||||
"version": "1.0.0",
|
||||
"public": []string{
|
||||
"GET /api/health",
|
||||
"GET /api/servers",
|
||||
"GET /api/ws/monitor — WebSocket 指标快照",
|
||||
"gRPC AgentIngest — 见环境变量 GRPC_PORT",
|
||||
},
|
||||
"admin": []string{
|
||||
"POST /api/admin/auth",
|
||||
"GET /api/admin/servers",
|
||||
"POST /api/admin/servers",
|
||||
"PUT /api/admin/servers/:id",
|
||||
"DELETE /api/admin/servers/:id",
|
||||
"PUT /api/admin/servers/reorder",
|
||||
},
|
||||
})
|
||||
}
|
||||
50
mengyamonitor-backend-server/internal/handler/ws.go
Normal file
50
mengyamonitor-backend-server/internal/handler/ws.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"mengyamonitor-backend-server/internal/hub"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var wsUpgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
// MonitorWebSocket upgrades to WebSocket and streams hub snapshots.
|
||||
func MonitorWebSocket(h *hub.Hub) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
conn, err := wsUpgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
log.Printf("ws: upgrade: %v", err)
|
||||
return
|
||||
}
|
||||
defer h.Unregister(conn)
|
||||
snap, err := h.BuildSnapshotMessage()
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return
|
||||
}
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(15 * time.Second))
|
||||
if err := conn.WriteMessage(websocket.TextMessage, snap); err != nil {
|
||||
_ = conn.Close()
|
||||
return
|
||||
}
|
||||
h.AddSubscriber(conn)
|
||||
for {
|
||||
_ = conn.SetReadDeadline(time.Now().Add(120 * time.Second))
|
||||
_, _, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user