mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(api): add namespaces, batch ops, and admin logs
This commit is contained in:
52
internal/middleware/operation_log.go
Normal file
52
internal/middleware/operation_log.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OperationLogMiddleware records admin mutating operations.
|
||||
func OperationLogMiddleware(db *gorm.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
c.Next()
|
||||
|
||||
if db == nil {
|
||||
return
|
||||
}
|
||||
switch c.Request.Method {
|
||||
case http.MethodGet, http.MethodHead, http.MethodOptions:
|
||||
return
|
||||
}
|
||||
|
||||
errMsg := ""
|
||||
if len(c.Errors) > 0 {
|
||||
errMsg = c.Errors.String()
|
||||
}
|
||||
reqID := ""
|
||||
if v, ok := c.Get("request_id"); ok {
|
||||
if s, ok := v.(string); ok {
|
||||
reqID = s
|
||||
}
|
||||
}
|
||||
|
||||
log := model.OperationLog{
|
||||
Actor: "admin",
|
||||
Method: c.Request.Method,
|
||||
Path: c.Request.URL.Path,
|
||||
Query: strings.TrimSpace(c.Request.URL.RawQuery),
|
||||
StatusCode: c.Writer.Status(),
|
||||
LatencyMs: time.Since(start).Milliseconds(),
|
||||
ClientIP: c.ClientIP(),
|
||||
RequestID: reqID,
|
||||
UserAgent: c.GetHeader("User-Agent"),
|
||||
ErrorMessage: errMsg,
|
||||
}
|
||||
_ = db.Create(&log).Error
|
||||
}
|
||||
}
|
||||
46
internal/middleware/operation_log_test.go
Normal file
46
internal/middleware/operation_log_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestOperationLogMiddleware_WritesLog(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("file:oplog?mode=memory&cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.OperationLog{}); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
|
||||
r := gin.New()
|
||||
r.Use(OperationLogMiddleware(db))
|
||||
r.POST("/admin/test", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/test?x=1", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", rr.Code)
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := db.Model(&model.OperationLog{}).Count(&count).Error; err != nil {
|
||||
t.Fatalf("count: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("expected 1 log, got %d", count)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user