mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(api): add log webhook notification service
Implement webhook notifications for log error threshold alerts with configurable thresholds, time windows, and cooldown periods. - Add LogWebhookService with Redis-backed configuration storage - Add admin endpoints for webhook config management (GET/PUT) - Trigger webhook notifications when error count exceeds threshold - Support status code threshold and error message detection - Include sample log record data in webhook payload
This commit is contained in:
112
internal/api/log_webhook_handler_test.go
Normal file
112
internal/api/log_webhook_handler_test.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/ez-api/ez-api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func newTestHandlerWithWebhook(t *testing.T) (*Handler, *miniredis.Miniredis) {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.LogRecord{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
|
||||
mr := miniredis.RunT(t)
|
||||
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
sync := service.NewSyncService(rdb)
|
||||
return NewHandler(db, sync, nil, rdb), mr
|
||||
}
|
||||
|
||||
func TestLogWebhookConfigCRUD(t *testing.T) {
|
||||
h, _ := newTestHandlerWithWebhook(t)
|
||||
|
||||
r := gin.New()
|
||||
r.GET("/admin/logs/webhook", h.GetLogWebhookConfig)
|
||||
r.PUT("/admin/logs/webhook", h.UpdateLogWebhookConfig)
|
||||
|
||||
reqBody := service.LogWebhookConfig{
|
||||
Enabled: true,
|
||||
URL: "https://example.com/webhook",
|
||||
Secret: "s1",
|
||||
Threshold: 3,
|
||||
WindowSeconds: 60,
|
||||
CooldownSeconds: 120,
|
||||
StatusCodeThreshold: 500,
|
||||
}
|
||||
payload, _ := json.Marshal(reqBody)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPut, "/admin/logs/webhook", bytes.NewReader(payload))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
getReq := httptest.NewRequest(http.MethodGet, "/admin/logs/webhook", nil)
|
||||
getRR := httptest.NewRecorder()
|
||||
r.ServeHTTP(getRR, getReq)
|
||||
if getRR.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", getRR.Code, getRR.Body.String())
|
||||
}
|
||||
|
||||
var got service.LogWebhookConfig
|
||||
if err := json.Unmarshal(getRR.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if !got.Enabled || got.URL == "" || got.Threshold != 3 {
|
||||
t.Fatalf("unexpected webhook config: %+v", got)
|
||||
}
|
||||
if got.Secret != "s1" {
|
||||
t.Fatalf("expected secret s1, got %q", got.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogWebhookConfigDisableClears(t *testing.T) {
|
||||
h, mr := newTestHandlerWithWebhook(t)
|
||||
|
||||
r := gin.New()
|
||||
r.PUT("/admin/logs/webhook", h.UpdateLogWebhookConfig)
|
||||
|
||||
seed := service.LogWebhookConfig{Enabled: true, URL: "https://example.com"}
|
||||
payload, _ := json.Marshal(seed)
|
||||
req := httptest.NewRequest(http.MethodPut, "/admin/logs/webhook", bytes.NewReader(payload))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
clear := service.LogWebhookConfig{Enabled: false}
|
||||
clearPayload, _ := json.Marshal(clear)
|
||||
clearReq := httptest.NewRequest(http.MethodPut, "/admin/logs/webhook", bytes.NewReader(clearPayload))
|
||||
clearReq.Header.Set("Content-Type", "application/json")
|
||||
clearRR := httptest.NewRecorder()
|
||||
r.ServeHTTP(clearRR, clearReq)
|
||||
if clearRR.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", clearRR.Code, clearRR.Body.String())
|
||||
}
|
||||
if mr.Exists("meta:log:webhook") {
|
||||
t.Fatalf("expected webhook config to be cleared")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user