refactor: 调整主文件位置
This commit is contained in:
77
main.go
Normal file
77
main.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// Package main provides the entry point for the GPT-Load proxy server
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gpt-load/internal/app"
|
||||
"gpt-load/internal/config"
|
||||
"gpt-load/internal/container"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/types"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
//go:embed web/dist
|
||||
var buildFS embed.FS
|
||||
|
||||
//go:embed web/dist/index.html
|
||||
var indexPage []byte
|
||||
|
||||
func main() {
|
||||
// Build the dependency injection container
|
||||
container, err := container.BuildContainer()
|
||||
if err != nil {
|
||||
logrus.Fatalf("Failed to build container: %v", err)
|
||||
}
|
||||
|
||||
// Provide UI assets to the container
|
||||
if err := container.Provide(func() embed.FS { return buildFS }); err != nil {
|
||||
logrus.Fatalf("Failed to provide buildFS: %v", err)
|
||||
}
|
||||
if err := container.Provide(func() []byte { return indexPage }); err != nil {
|
||||
logrus.Fatalf("Failed to provide indexPage: %v", err)
|
||||
}
|
||||
|
||||
// Provide the request log channel as a value
|
||||
requestLogChan := make(chan models.RequestLog, 1000)
|
||||
if err := container.Provide(func() chan models.RequestLog { return requestLogChan }); err != nil {
|
||||
logrus.Fatalf("Failed to provide request log channel: %v", err)
|
||||
}
|
||||
|
||||
// Initialzie global logger
|
||||
if err := container.Invoke(func(configManager types.ConfigManager) {
|
||||
config.SetupLogger(configManager)
|
||||
}); err != nil {
|
||||
logrus.Fatalf("Failed to setup logger: %v", err)
|
||||
}
|
||||
|
||||
// Create and run the application
|
||||
if err := container.Invoke(func(application *app.App, configManager types.ConfigManager) {
|
||||
if err := application.Start(); err != nil {
|
||||
logrus.Fatalf("Failed to start application: %v", err)
|
||||
}
|
||||
|
||||
// Wait for interrupt signal for graceful shutdown
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
|
||||
// Create a context with timeout for shutdown
|
||||
serverConfig := configManager.GetEffectiveServerConfig()
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), time.Duration(serverConfig.GracefulShutdownTimeout)*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Perform graceful shutdown
|
||||
application.Stop(shutdownCtx)
|
||||
|
||||
}); err != nil {
|
||||
logrus.Fatalf("Failed to run application: %v", err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user