chore: initial import
This commit is contained in:
84
internal/handler/rule_handler.go
Normal file
84
internal/handler/rule_handler.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/company/ai-ops/internal/domain/model"
|
||||
"github.com/company/ai-ops/internal/service"
|
||||
"github.com/company/ai-ops/pkg/errors"
|
||||
"github.com/company/ai-ops/pkg/response"
|
||||
)
|
||||
|
||||
// RuleHandler 是告警规则 HTTP 处理器
|
||||
type RuleHandler struct {
|
||||
service *service.RuleService
|
||||
}
|
||||
|
||||
func NewRuleHandler(s *service.RuleService) *RuleHandler {
|
||||
return &RuleHandler{service: s}
|
||||
}
|
||||
|
||||
func (h *RuleHandler) RegisterRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/v1/ai-ops/rules", h.ListRules)
|
||||
mux.HandleFunc("GET /api/v1/ai-ops/rules/{id}", h.GetRule)
|
||||
mux.HandleFunc("POST /api/v1/ai-ops/rules", h.CreateRule)
|
||||
mux.HandleFunc("PUT /api/v1/ai-ops/rules/{id}", h.UpdateRule)
|
||||
mux.HandleFunc("DELETE /api/v1/ai-ops/rules/{id}", h.DeleteRule)
|
||||
}
|
||||
|
||||
func (h *RuleHandler) ListRules(w http.ResponseWriter, r *http.Request) {
|
||||
rules, err := h.service.ListRules(r.Context())
|
||||
if err != nil {
|
||||
response.Error(w, errors.Wrap(err, errors.ErrInternal))
|
||||
return
|
||||
}
|
||||
response.Success(w, rules)
|
||||
}
|
||||
|
||||
func (h *RuleHandler) GetRule(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
rule, err := h.service.GetRule(r.Context(), id)
|
||||
if err != nil {
|
||||
response.Error(w, errors.Wrap(err, errors.ErrNotFound))
|
||||
return
|
||||
}
|
||||
response.Success(w, rule)
|
||||
}
|
||||
|
||||
func (h *RuleHandler) CreateRule(w http.ResponseWriter, r *http.Request) {
|
||||
var rule model.AlertRule
|
||||
if err := decodeJSON(r, &rule); err != nil {
|
||||
response.Error(w, errors.ErrBadRequest.WithDetail(map[string]any{"error": err.Error()}))
|
||||
return
|
||||
}
|
||||
if err := h.service.CreateRule(r.Context(), &rule); err != nil {
|
||||
response.Error(w, errors.Wrap(err, errors.ErrBadRequest))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
response.Success(w, rule)
|
||||
}
|
||||
|
||||
func (h *RuleHandler) UpdateRule(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
var rule model.AlertRule
|
||||
if err := decodeJSON(r, &rule); err != nil {
|
||||
response.Error(w, errors.ErrBadRequest.WithDetail(map[string]any{"error": err.Error()}))
|
||||
return
|
||||
}
|
||||
rule.ID = id
|
||||
if err := h.service.UpdateRule(r.Context(), &rule); err != nil {
|
||||
response.Error(w, errors.Wrap(err, errors.ErrBadRequest))
|
||||
return
|
||||
}
|
||||
response.Success(w, rule)
|
||||
}
|
||||
|
||||
func (h *RuleHandler) DeleteRule(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
if err := h.service.DeleteRule(r.Context(), id); err != nil {
|
||||
response.Error(w, errors.Wrap(err, errors.ErrInternal))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
Reference in New Issue
Block a user