package handler import ( "gcy_hpc_server/internal/server" "gcy_hpc_server/internal/service" "github.com/gin-gonic/gin" "go.uber.org/zap" ) // ClusterHandler handles HTTP requests for cluster operations (nodes, partitions, diag). type ClusterHandler struct { clusterSvc *service.ClusterService logger *zap.Logger } // NewClusterHandler creates a new ClusterHandler with the given ClusterService. func NewClusterHandler(clusterSvc *service.ClusterService, logger *zap.Logger) *ClusterHandler { return &ClusterHandler{clusterSvc: clusterSvc, logger: logger} } // GetNodes handles GET /api/v1/nodes. func (h *ClusterHandler) GetNodes(c *gin.Context) { nodes, err := h.clusterSvc.GetNodes(c.Request.Context()) if err != nil { h.logger.Error("handler error", zap.String("method", "GetNodes"), zap.Error(err)) server.InternalError(c, err.Error()) return } server.OK(c, nodes) } // GetNode handles GET /api/v1/nodes/:name. func (h *ClusterHandler) GetNode(c *gin.Context) { name := c.Param("name") resp, err := h.clusterSvc.GetNode(c.Request.Context(), name) if err != nil { h.logger.Error("handler error", zap.String("method", "GetNode"), zap.Error(err)) server.InternalError(c, err.Error()) return } if resp == nil { h.logger.Warn("not found", zap.String("method", "GetNode"), zap.String("name", name)) server.NotFound(c, "node not found") return } server.OK(c, resp) } // GetPartitions handles GET /api/v1/partitions. func (h *ClusterHandler) GetPartitions(c *gin.Context) { partitions, err := h.clusterSvc.GetPartitions(c.Request.Context()) if err != nil { h.logger.Error("handler error", zap.String("method", "GetPartitions"), zap.Error(err)) server.InternalError(c, err.Error()) return } server.OK(c, partitions) } // GetPartition handles GET /api/v1/partitions/:name. func (h *ClusterHandler) GetPartition(c *gin.Context) { name := c.Param("name") resp, err := h.clusterSvc.GetPartition(c.Request.Context(), name) if err != nil { h.logger.Error("handler error", zap.String("method", "GetPartition"), zap.Error(err)) server.InternalError(c, err.Error()) return } if resp == nil { h.logger.Warn("not found", zap.String("method", "GetPartition"), zap.String("name", name)) server.NotFound(c, "partition not found") return } server.OK(c, resp) } // GetDiag handles GET /api/v1/diag. func (h *ClusterHandler) GetDiag(c *gin.Context) { resp, err := h.clusterSvc.GetDiag(c.Request.Context()) if err != nil { h.logger.Error("handler error", zap.String("method", "GetDiag"), zap.Error(err)) server.InternalError(c, err.Error()) return } server.OK(c, resp) }