Refactor configuration and key management

- Removed key management configuration from .env.example and related code.
- Updated Makefile to load environment variables for HOST and PORT.
- Modified main.go to handle request logging with a wait group for graceful shutdown.
- Simplified dashboard statistics handler to focus on key counts and request metrics.
- Removed key manager implementation and related interfaces.
- Updated proxy server to use atomic counters for round-robin key selection.
- Cleaned up unused types and configurations in types.go.
- Added package-lock.json for frontend dependencies.
This commit is contained in:
tbphp
2025-06-30 22:09:16 +08:00
parent 5f0990f22b
commit b9a833ceab
10 changed files with 130 additions and 669 deletions

View File

@@ -12,6 +12,7 @@ import (
"path"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
@@ -50,7 +51,9 @@ func main() {
// --- Asynchronous Request Logging Setup ---
requestLogChan := make(chan models.RequestLog, 1000)
go startRequestLogger(database, requestLogChan)
var wg sync.WaitGroup
wg.Add(1)
go startRequestLogger(database, requestLogChan, &wg)
// ---
// Create proxy server
@@ -103,9 +106,15 @@ func main() {
// Attempt graceful shutdown
if err := server.Shutdown(ctx); err != nil {
logrus.Errorf("Server forced to shutdown: %v", err)
} else {
logrus.Info("Server exited gracefully")
}
// Close the request log channel and wait for the logger to finish
logrus.Info("Closing request log channel...")
close(requestLogChan)
wg.Wait()
logrus.Info("All logs have been written.")
logrus.Info("Server exited gracefully")
}
// setupRoutes configures the HTTP routes
@@ -233,7 +242,6 @@ func setupLogger(configManager types.ConfigManager) {
// displayStartupInfo shows startup information
func displayStartupInfo(configManager types.ConfigManager) {
serverConfig := configManager.GetServerConfig()
keysConfig := configManager.GetKeysConfig()
openaiConfig := configManager.GetOpenAIConfig()
authConfig := configManager.GetAuthConfig()
corsConfig := configManager.GetCORSConfig()
@@ -242,10 +250,6 @@ func displayStartupInfo(configManager types.ConfigManager) {
logrus.Info("Current Configuration:")
logrus.Infof(" Server: %s:%d", serverConfig.Host, serverConfig.Port)
logrus.Infof(" Keys file: %s", keysConfig.FilePath)
logrus.Infof(" Start index: %d", keysConfig.StartIndex)
logrus.Infof(" Blacklist threshold: %d errors", keysConfig.BlacklistThreshold)
logrus.Infof(" Max retries: %d", keysConfig.MaxRetries)
logrus.Infof(" Upstream URL: %s", openaiConfig.BaseURL)
logrus.Infof(" Request timeout: %ds", openaiConfig.RequestTimeout)
logrus.Infof(" Response timeout: %ds", openaiConfig.ResponseTimeout)
@@ -278,7 +282,8 @@ func displayStartupInfo(configManager types.ConfigManager) {
}
// startRequestLogger runs a background goroutine to batch-insert request logs.
func startRequestLogger(db *gorm.DB, logChan <-chan models.RequestLog) {
func startRequestLogger(db *gorm.DB, logChan <-chan models.RequestLog, wg *sync.WaitGroup) {
defer wg.Done()
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()