mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-06 16:01:07 +00:00
199 lines
8.9 KiB
Go
199 lines
8.9 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
_ "github.com/chenyme/grok2api/backend/docs"
|
|
accountapp "github.com/chenyme/grok2api/backend/internal/application/account"
|
|
accountsyncapp "github.com/chenyme/grok2api/backend/internal/application/accountsync"
|
|
adminauthapp "github.com/chenyme/grok2api/backend/internal/application/adminauth"
|
|
auditapp "github.com/chenyme/grok2api/backend/internal/application/audit"
|
|
clientkeyapp "github.com/chenyme/grok2api/backend/internal/application/clientkey"
|
|
dashboardapp "github.com/chenyme/grok2api/backend/internal/application/dashboard"
|
|
egressapp "github.com/chenyme/grok2api/backend/internal/application/egress"
|
|
"github.com/chenyme/grok2api/backend/internal/application/gateway"
|
|
mediaapp "github.com/chenyme/grok2api/backend/internal/application/media"
|
|
modelapp "github.com/chenyme/grok2api/backend/internal/application/model"
|
|
settingsapp "github.com/chenyme/grok2api/backend/internal/application/settings"
|
|
updatecheckapp "github.com/chenyme/grok2api/backend/internal/application/updatecheck"
|
|
accounthttp "github.com/chenyme/grok2api/backend/internal/transport/http/account"
|
|
adminauthhttp "github.com/chenyme/grok2api/backend/internal/transport/http/adminauth"
|
|
audithttp "github.com/chenyme/grok2api/backend/internal/transport/http/audit"
|
|
clientkeyhttp "github.com/chenyme/grok2api/backend/internal/transport/http/clientkey"
|
|
dashboardhttp "github.com/chenyme/grok2api/backend/internal/transport/http/dashboard"
|
|
egresshttp "github.com/chenyme/grok2api/backend/internal/transport/http/egress"
|
|
"github.com/chenyme/grok2api/backend/internal/transport/http/inference"
|
|
mediahttp "github.com/chenyme/grok2api/backend/internal/transport/http/media"
|
|
"github.com/chenyme/grok2api/backend/internal/transport/http/middleware"
|
|
modelhttp "github.com/chenyme/grok2api/backend/internal/transport/http/model"
|
|
settingshttp "github.com/chenyme/grok2api/backend/internal/transport/http/settings"
|
|
systemhttp "github.com/chenyme/grok2api/backend/internal/transport/http/system"
|
|
"github.com/gin-gonic/gin"
|
|
swaggerFiles "github.com/swaggo/files"
|
|
ginSwagger "github.com/swaggo/gin-swagger"
|
|
)
|
|
|
|
type Dependencies struct {
|
|
Logger *slog.Logger
|
|
RequestTimeout time.Duration
|
|
MaxBodyBytes int64
|
|
TrustedProxies []string
|
|
ConcurrencyGate *middleware.ConcurrencyGate
|
|
SecureCookies bool
|
|
SwaggerEnabled bool
|
|
PublicAPIBaseURL string
|
|
FrontendStaticPath string
|
|
// Readiness 返回可观测的分层就绪状态。Ready 仅为旧调用方保留。
|
|
Readiness func(context.Context) ReadinessSnapshot
|
|
Ready func(context.Context) bool
|
|
TrafficReady func() bool
|
|
AdminAuth *adminauthapp.Service
|
|
Accounts *accountapp.Service
|
|
AccountSync *accountsyncapp.Service
|
|
Models *modelapp.Service
|
|
ClientKeys *clientkeyapp.Service
|
|
Audits *auditapp.Service
|
|
Dashboard *dashboardapp.Service
|
|
Gateway *gateway.Service
|
|
Media *mediaapp.Service
|
|
Settings *settingsapp.Service
|
|
Egress *egressapp.Service
|
|
QualityGuardStatePath string
|
|
QualityGuardConfigPath string
|
|
QualityGuardToken string
|
|
QualityGuardProbe egressapp.QualityProbeInput
|
|
Updates *updatecheckapp.Service
|
|
}
|
|
|
|
type ReadinessComponent struct {
|
|
State string `json:"state"`
|
|
Detail string `json:"detail,omitempty"`
|
|
}
|
|
|
|
// ReadinessCredentialReport 表示可公开的启动凭据恢复统计。
|
|
type ReadinessCredentialReport struct {
|
|
SchedulesBackfilled int `json:"schedulesBackfilled"`
|
|
CriticalFound int `json:"criticalFound"`
|
|
Refreshed int `json:"refreshed"`
|
|
Failed int `json:"failed"`
|
|
}
|
|
|
|
// ReadinessStartupReport 表示可公开的启动恢复统计,不包含内部错误文本。
|
|
type ReadinessStartupReport struct {
|
|
StartedAt time.Time `json:"startedAt"`
|
|
CompletedAt *time.Time `json:"completedAt,omitempty"`
|
|
Credentials ReadinessCredentialReport `json:"credentials"`
|
|
CooldownsRestored int `json:"cooldownsRestored"`
|
|
QuotaRecoveriesRestored int `json:"quotaRecoveriesRestored"`
|
|
DueWebQuotasQueued int `json:"dueWebQuotasQueued"`
|
|
StatsigKeysWarmed int `json:"statsigKeysWarmed"`
|
|
StaleWebQuotasFound int `json:"staleWebQuotasFound"`
|
|
StaleWebQuotasSynced int `json:"staleWebQuotasSynced"`
|
|
StaleModelCatalogsFound int `json:"staleModelCatalogsFound"`
|
|
StaleModelCatalogsSynced int `json:"staleModelCatalogsSynced"`
|
|
ErrorCount int `json:"errorCount"`
|
|
}
|
|
|
|
// ReadinessSnapshot 表示公开就绪端点的稳定响应契约。
|
|
type ReadinessSnapshot struct {
|
|
Ready bool `json:"ready"`
|
|
State string `json:"state"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
Components map[string]ReadinessComponent `json:"components,omitempty"`
|
|
Startup *ReadinessStartupReport `json:"startup,omitempty"`
|
|
}
|
|
|
|
// New 创建完整 HTTP 路由并明确区分公共、管理员和客户端鉴权边界。
|
|
func New(deps Dependencies) *gin.Engine {
|
|
if deps.ConcurrencyGate == nil {
|
|
panic("httpserver: ConcurrencyGate 不能为空")
|
|
}
|
|
gin.SetMode(gin.ReleaseMode)
|
|
if deps.Logger == nil {
|
|
deps.Logger = slog.Default()
|
|
}
|
|
router := gin.New()
|
|
if err := router.SetTrustedProxies(deps.TrustedProxies); err != nil {
|
|
panic("httpserver: trustedProxies 配置无效: " + err.Error())
|
|
}
|
|
router.Use(gin.Recovery(), middleware.RequestID(), middleware.ClientIP(), middleware.SecurityHeaders(), middleware.MaxBodyBytes(deps.MaxBodyBytes), middleware.Timeout(deps.RequestTimeout), middleware.AccessLog(deps.Logger))
|
|
router.GET("/healthz", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"ok": true}) })
|
|
router.GET("/readyz", func(c *gin.Context) {
|
|
if deps.Readiness != nil {
|
|
snapshot := deps.Readiness(c.Request.Context())
|
|
status := http.StatusServiceUnavailable
|
|
if snapshot.Ready {
|
|
status = http.StatusOK
|
|
}
|
|
c.JSON(status, snapshot)
|
|
return
|
|
}
|
|
if deps.Ready != nil && deps.Ready(c.Request.Context()) {
|
|
c.JSON(http.StatusOK, gin.H{"ready": true, "state": "ready"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusServiceUnavailable, gin.H{"ready": false, "state": "not_ready"})
|
|
})
|
|
if deps.SwaggerEnabled {
|
|
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
|
}
|
|
mediaHandler := mediahttp.NewHandler(deps.Media)
|
|
mediaHandler.RegisterPublic(router)
|
|
|
|
adminRoot := router.Group("/api/admin/v1")
|
|
authHandler := adminauthhttp.NewHandler(deps.AdminAuth, deps.SecureCookies)
|
|
authHandler.RegisterPublic(adminRoot)
|
|
adminProtected := adminRoot.Group("")
|
|
adminProtected.Use(middleware.AdminAuth(deps.AdminAuth))
|
|
authHandler.RegisterAuthenticated(adminProtected)
|
|
accounthttp.NewHandler(deps.Accounts, deps.AccountSync).Register(adminProtected)
|
|
modelhttp.NewHandler(deps.Models).Register(adminProtected)
|
|
clientkeyhttp.NewHandler(deps.ClientKeys).Register(adminProtected)
|
|
auditHandler := audithttp.NewHandler(deps.Audits)
|
|
auditHandler.Register(adminProtected)
|
|
dashboardhttp.NewHandler(deps.Dashboard).Register(adminProtected)
|
|
mediaHandler.RegisterAdmin(adminProtected)
|
|
settingshttp.NewHandler(deps.Settings).Register(adminProtected)
|
|
egressHandler := egresshttp.NewHandler(deps.Egress, deps.QualityGuardStatePath, deps.QualityGuardConfigPath).WithQualityGuardProbe(deps.QualityGuardProbe)
|
|
egressHandler.Register(adminProtected)
|
|
systemhttp.NewHandler(func() string {
|
|
if deps.Settings != nil {
|
|
return deps.Settings.PublicAPIBaseURL()
|
|
}
|
|
return deps.PublicAPIBaseURL
|
|
}, deps.Updates).Register(adminProtected)
|
|
|
|
if deps.QualityGuardToken != "" {
|
|
qualityGuardInternal := router.Group("/api/internal/v1/quality-guard")
|
|
qualityGuardInternal.Use(middleware.QualityGuardAuth(deps.QualityGuardToken))
|
|
audithttp.NewQualityGuardHandler(deps.Audits, deps.QualityGuardProbe.ClientKeyID).RegisterQualityGuard(qualityGuardInternal)
|
|
egressHandler.RegisterQualityGuard(qualityGuardInternal)
|
|
}
|
|
|
|
v1 := router.Group("/v1")
|
|
v1.Use(deps.ConcurrencyGate.Middleware())
|
|
v1.Use(middleware.ObserveBodyMemory())
|
|
if deps.TrafficReady != nil {
|
|
v1.Use(func(c *gin.Context) {
|
|
if deps.TrafficReady() {
|
|
c.Next()
|
|
return
|
|
}
|
|
c.AbortWithStatusJSON(http.StatusServiceUnavailable, gin.H{"error": gin.H{
|
|
"code": "service_reconciling", "message": "服务正在完成启动恢复,请稍后重试", "param": nil, "type": "server_error",
|
|
}})
|
|
})
|
|
}
|
|
v1.Use(middleware.ClientAuth(deps.ClientKeys))
|
|
inferenceHandler := inference.NewHandler(deps.Gateway, deps.Models, deps.MaxBodyBytes, deps.PublicAPIBaseURL)
|
|
if deps.Settings != nil {
|
|
inferenceHandler.SetPublicAPIBaseURLResolver(deps.Settings.PublicAPIBaseURL)
|
|
}
|
|
inferenceHandler.Register(v1)
|
|
registerFrontend(router, deps.FrontendStaticPath)
|
|
return router
|
|
}
|