71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
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",
|
|
},
|
|
})
|
|
}
|