feat(server): implement management endpoints and redis sync

Enable full resource management via API and support data plane
synchronization.

- Add CRUD handlers for Providers, Models, and Keys using DTOs
- Implement LogWriter service for asynchronous, batched audit logging
- Update SyncService to snapshot full configuration state to Redis
- Register new API routes and initialize background services
- Add configuration options for logging performance tuning
This commit is contained in:
zenfun
2025-12-02 14:26:16 +08:00
parent 2c3a6af7bf
commit 64d71953a6
10 changed files with 479 additions and 40 deletions

View File

@@ -57,13 +57,22 @@ func main() {
log.Println("Connected to PostgreSQL successfully")
// Auto Migrate
if err := db.AutoMigrate(&model.User{}, &model.Provider{}, &model.Key{}, &model.Model{}); err != nil {
if err := db.AutoMigrate(&model.User{}, &model.Provider{}, &model.Key{}, &model.Model{}, &model.LogRecord{}); err != nil {
log.Fatalf("Failed to auto migrate: %v", err)
}
// 4. Setup Services and Handlers
syncService := service.NewSyncService(rdb)
handler := api.NewHandler(db, syncService)
logWriter := service.NewLogWriter(db, cfg.Log.QueueCapacity, cfg.Log.BatchSize, cfg.Log.FlushInterval)
logCtx, cancelLogs := context.WithCancel(context.Background())
defer cancelLogs()
logWriter.Start(logCtx)
handler := api.NewHandler(db, syncService, logWriter)
// 4.1 Prime Redis snapshots so DP can start with data
if err := syncService.SyncAll(db); err != nil {
log.Printf("Initial sync warning: %v", err)
}
// 5. Setup Gin Router
r := gin.Default()
@@ -74,7 +83,12 @@ func main() {
})
// API Routes
r.POST("/providers", handler.CreateProvider)
r.POST("/keys", handler.CreateKey)
r.POST("/models", handler.CreateModel)
r.GET("/models", handler.ListModels)
r.POST("/sync/snapshot", handler.SyncSnapshot)
r.POST("/logs", handler.IngestLog)
srv := &http.Server{
Addr: ":" + cfg.Server.Port,