-
-
Notifications
You must be signed in to change notification settings - Fork 640
Expand file tree
/
Copy pathmain.go
More file actions
113 lines (96 loc) · 2.92 KB
/
main.go
File metadata and controls
113 lines (96 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Package main provides the entry point for the GPT-Load proxy server
package main
import (
"context"
"embed"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"gpt-load/internal/app"
"gpt-load/internal/commands"
"gpt-load/internal/container"
"gpt-load/internal/types"
"gpt-load/internal/utils"
"github.com/sirupsen/logrus"
)
//go:embed web/dist
var buildFS embed.FS
//go:embed web/dist/index.html
var indexPage []byte
func main() {
if len(os.Args) > 1 {
runCommand()
} else {
runServer()
}
}
// runCommand dispatches to the appropriate command handler
func runCommand() {
command := os.Args[1]
args := os.Args[2:]
switch command {
case "migrate-keys":
commands.RunMigrateKeys(args)
case "help", "-h", "--help":
printHelp()
default:
fmt.Printf("Unknown command: %s\n", command)
fmt.Println("Run 'gpt-load help' for usage.")
os.Exit(1)
}
}
// printHelp displays the general help information
func printHelp() {
fmt.Println("GPT-Load - Multi-channel AI proxy with intelligent key rotation.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println(" gpt-load Start the proxy server")
fmt.Println(" gpt-load <command> [args] Execute a command")
fmt.Println()
fmt.Println("Available Commands:")
fmt.Println(" migrate-keys Migrate encryption keys")
fmt.Println(" help Display this help message")
fmt.Println()
fmt.Println("Use 'gpt-load <command> --help' for more information about a command.")
}
// runServer run App Server
func runServer() {
// 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)
}
// Initialize global logger
if err := container.Invoke(func(configManager types.ConfigManager) {
utils.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)
}
}