refactor: 重构types

This commit is contained in:
tbphp
2025-06-29 20:14:13 +08:00
parent 2ba378f7ac
commit ab95af0bbe
7 changed files with 7 additions and 7 deletions

View File

@@ -10,7 +10,7 @@ import (
"sync/atomic"
"gpt-load/internal/errors"
"gpt-load/pkg/types"
"gpt-load/internal/types"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"

View File

@@ -6,7 +6,7 @@ import (
"runtime"
"time"
"gpt-load/pkg/types"
"gpt-load/internal/types"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

View File

@@ -12,7 +12,7 @@ import (
"time"
"gpt-load/internal/errors"
"gpt-load/pkg/types"
"gpt-load/internal/types"
"github.com/sirupsen/logrus"
)

View File

@@ -7,7 +7,7 @@ import (
"time"
"gpt-load/internal/errors"
"gpt-load/pkg/types"
"gpt-load/internal/types"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

View File

@@ -16,7 +16,7 @@ import (
"time"
"gpt-load/internal/errors"
"gpt-load/pkg/types"
"gpt-load/internal/types"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

141
internal/types/types.go Normal file
View File

@@ -0,0 +1,141 @@
// Package types defines common interfaces and types used across the application
package types
import (
"time"
"github.com/gin-gonic/gin"
)
// ConfigManager defines the interface for configuration management
type ConfigManager interface {
GetServerConfig() ServerConfig
GetKeysConfig() KeysConfig
GetOpenAIConfig() OpenAIConfig
GetAuthConfig() AuthConfig
GetCORSConfig() CORSConfig
GetPerformanceConfig() PerformanceConfig
GetLogConfig() LogConfig
Validate() error
DisplayConfig()
}
// KeyManager defines the interface for API key management
type KeyManager interface {
LoadKeys() error
GetNextKey() (*KeyInfo, error)
RecordSuccess(key string)
RecordFailure(key string, err error)
GetStats() Stats
ResetBlacklist()
GetBlacklist() []BlacklistEntry
Close()
}
// ProxyServer defines the interface for proxy server
type ProxyServer interface {
HandleProxy(c *gin.Context)
Close()
}
// ServerConfig represents server configuration
type ServerConfig struct {
Port int `json:"port"`
Host string `json:"host"`
ReadTimeout int `json:"readTimeout"`
WriteTimeout int `json:"writeTimeout"`
IdleTimeout int `json:"idleTimeout"`
GracefulShutdownTimeout int `json:"gracefulShutdownTimeout"`
}
// KeysConfig represents keys configuration
type KeysConfig struct {
FilePath string `json:"filePath"`
StartIndex int `json:"startIndex"`
BlacklistThreshold int `json:"blacklistThreshold"`
MaxRetries int `json:"maxRetries"`
}
// OpenAIConfig represents OpenAI API configuration
type OpenAIConfig struct {
BaseURL string `json:"baseUrl"`
BaseURLs []string `json:"baseUrls"`
RequestTimeout int `json:"requestTimeout"`
ResponseTimeout int `json:"responseTimeout"`
IdleConnTimeout int `json:"idleConnTimeout"`
}
// AuthConfig represents authentication configuration
type AuthConfig struct {
Key string `json:"key"`
Enabled bool `json:"enabled"`
}
// CORSConfig represents CORS configuration
type CORSConfig struct {
Enabled bool `json:"enabled"`
AllowedOrigins []string `json:"allowedOrigins"`
AllowedMethods []string `json:"allowedMethods"`
AllowedHeaders []string `json:"allowedHeaders"`
AllowCredentials bool `json:"allowCredentials"`
}
// PerformanceConfig represents performance configuration
type PerformanceConfig struct {
MaxConcurrentRequests int `json:"maxConcurrentRequests"`
EnableGzip bool `json:"enableGzip"`
}
// LogConfig represents logging configuration
type LogConfig struct {
Level string `json:"level"`
Format string `json:"format"`
EnableFile bool `json:"enableFile"`
FilePath string `json:"filePath"`
EnableRequest bool `json:"enableRequest"`
}
// KeyInfo represents API key information
type KeyInfo struct {
Key string `json:"key"`
Index int `json:"index"`
Preview string `json:"preview"`
}
// Stats represents system statistics
type Stats struct {
CurrentIndex int64 `json:"currentIndex"`
TotalKeys int `json:"totalKeys"`
HealthyKeys int `json:"healthyKeys"`
BlacklistedKeys int `json:"blacklistedKeys"`
SuccessCount int64 `json:"successCount"`
FailureCount int64 `json:"failureCount"`
MemoryUsage MemoryUsage `json:"memoryUsage"`
}
// MemoryUsage represents memory usage statistics
type MemoryUsage struct {
Alloc uint64 `json:"alloc"`
TotalAlloc uint64 `json:"totalAlloc"`
Sys uint64 `json:"sys"`
NumGC uint32 `json:"numGC"`
LastGCTime string `json:"lastGCTime"`
NextGCTarget uint64 `json:"nextGCTarget"`
}
// BlacklistEntry represents a blacklisted key entry
type BlacklistEntry struct {
Key string `json:"key"`
Preview string `json:"preview"`
Reason string `json:"reason"`
BlacklistAt time.Time `json:"blacklistAt"`
FailCount int `json:"failCount"`
}
// RetryError represents retry error information
type RetryError struct {
StatusCode int `json:"statusCode"`
ErrorMessage string `json:"errorMessage"`
KeyIndex int `json:"keyIndex"`
Attempt int `json:"attempt"`
}