mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
Establish the foundational structure for the ez-api server. Key changes include: - Set up main entry point with graceful shutdown and Gin router - Configure database connections for PostgreSQL (GORM) and Redis - Define core data models (User, Provider, Key, Model) - Implement configuration loading and basic key creation handler - Add Dockerfile for multi-stage builds and .gitignore
43 lines
1010 B
Go
43 lines
1010 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/ez-api/ez-api/internal/model"
|
|
"github.com/ez-api/ez-api/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Handler struct {
|
|
db *gorm.DB
|
|
sync *service.SyncService
|
|
}
|
|
|
|
func NewHandler(db *gorm.DB, sync *service.SyncService) *Handler {
|
|
return &Handler{db: db, sync: sync}
|
|
}
|
|
|
|
func (h *Handler) CreateKey(c *gin.Context) {
|
|
var key model.Key
|
|
if err := c.ShouldBindJSON(&key); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Save to DB
|
|
if err := h.db.Create(&key).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create key", "details": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Sync to Redis
|
|
if err := h.sync.SyncKey(&key); err != nil {
|
|
// Note: In a real system, we might want to rollback DB or retry async
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync key to Redis", "details": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, key)
|
|
}
|