mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-20 11:54:25 +00:00
- Add package documentation mapping handler files to domains (services/monitoring/deployments/plugins/automation/config/chaos/billing) - Remove gorm.io/gorm, models, monitoring, and services package imports - Remove ServicesHandler struct and all methods (List/Get/Restart/Start/Stop/Scale/Logs/Health/PodStatus/Events) - Remove MonitoringHandler struct and all methods (Metrics/Alerts/Health/Logs)
75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/nutcas3/telecom-platform/apps/api-server/internal/services"
|
|
)
|
|
|
|
// ChaosHandler exposes chaos-engineering experiment endpoints.
|
|
type ChaosHandler struct {
|
|
svc *services.ChaosService
|
|
}
|
|
|
|
// NewChaosHandler constructs a ChaosHandler.
|
|
func NewChaosHandler(svc *services.ChaosService) *ChaosHandler {
|
|
return &ChaosHandler{svc: svc}
|
|
}
|
|
|
|
type runExperimentRequest struct {
|
|
Type services.ExperimentType `json:"type" binding:"required"`
|
|
Target string `json:"target" binding:"required"`
|
|
DurationMS int64 `json:"duration_ms"`
|
|
Probability float64 `json:"probability"`
|
|
Amount int `json:"amount"`
|
|
}
|
|
|
|
func (h *ChaosHandler) Run(c *gin.Context) {
|
|
var req runExperimentRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
badRequest(c, err)
|
|
return
|
|
}
|
|
cfg := services.ExperimentConfig{
|
|
Duration: millisToDuration(req.DurationMS),
|
|
Probability: req.Probability,
|
|
Amount: req.Amount,
|
|
Target: req.Target,
|
|
}
|
|
exp, err := h.svc.RunExperiment(c.Request.Context(), req.Type, cfg)
|
|
if err != nil {
|
|
serverError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, exp)
|
|
}
|
|
|
|
func (h *ChaosHandler) List(c *gin.Context) {
|
|
active := h.svc.GetActiveExperiments()
|
|
history, _ := h.svc.GetExperimentHistory()
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"active": active,
|
|
"history": history,
|
|
})
|
|
}
|
|
|
|
func (h *ChaosHandler) Status(c *gin.Context) {
|
|
id := c.Query("id")
|
|
if id == "" {
|
|
active := h.svc.GetActiveExperiments()
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"active_count": len(active),
|
|
"active": active,
|
|
})
|
|
return
|
|
}
|
|
exp, ok := h.svc.GetExperimentStatus(id)
|
|
if !ok {
|
|
c.JSON(http.StatusNotFound, ErrorResponse{Error: "experiment not found", Code: "NOT_FOUND"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, exp)
|
|
}
|