forked from StarryKira/copilot2api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
101 lines (83 loc) · 2.37 KB
/
Copy pathmain.go
File metadata and controls
101 lines (83 loc) · 2.37 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
package main
import (
"flag"
"fmt"
"log"
"sync"
"copilot-go/config"
"copilot-go/handler"
"copilot-go/instance"
"copilot-go/store"
"github.com/gin-gonic/gin"
)
func main() {
webPort := flag.Int("web-port", 3000, "Web console port")
proxyPort := flag.Int("proxy-port", 4141, "Proxy server port")
verbose := flag.Bool("verbose", false, "Enable verbose logging")
autoStart := flag.Bool("auto-start", true, "Auto-start enabled accounts")
flag.Parse()
if !*verbose {
gin.SetMode(gin.ReleaseMode)
}
// Ensure data directories exist
if err := store.EnsurePaths(); err != nil {
log.Fatalf("Failed to initialize data paths: %v", err)
}
// Load proxy config and apply to HTTP clients
if proxyCfg, err := store.GetProxyConfig(); err == nil && proxyCfg.ProxyURL != "" {
config.SetProxyURL(proxyCfg.ProxyURL)
instance.RebuildHTTPClients()
log.Printf("Using HTTP proxy: %s", proxyCfg.ProxyURL)
}
// Auto-start enabled accounts
if *autoStart {
accounts, err := store.GetEnabledAccounts()
if err != nil {
log.Printf("Warning: failed to load accounts: %v", err)
} else {
for _, account := range accounts {
go func(a store.Account) {
if err := instance.StartInstance(a); err != nil {
log.Printf("Failed to auto-start account %s: %v", a.Name, err)
}
}(account)
}
}
}
var wg sync.WaitGroup
wg.Add(2)
// Start Web Console
go func() {
defer wg.Done()
webEngine := gin.New()
// Prevent Gin's automatic path normalization redirects from causing redirect loops
// for the SPA web console (e.g. clients receiving `Location: ./` on `/`).
webEngine.RedirectTrailingSlash = false
webEngine.RedirectFixedPath = false
webEngine.RemoveExtraSlash = false
if *verbose {
webEngine.Use(gin.Logger())
}
webEngine.Use(gin.Recovery())
handler.RegisterConsoleAPI(webEngine, *proxyPort)
log.Printf("Web Console listening on :%d", *webPort)
if err := webEngine.Run(fmt.Sprintf(":%d", *webPort)); err != nil {
log.Fatalf("Web Console failed: %v", err)
}
}()
// Start Proxy
go func() {
defer wg.Done()
proxyEngine := gin.New()
if *verbose {
proxyEngine.Use(gin.Logger())
}
proxyEngine.Use(gin.Recovery())
handler.RegisterProxy(proxyEngine)
log.Printf("Proxy listening on :%d", *proxyPort)
if err := proxyEngine.Run(fmt.Sprintf(":%d", *proxyPort)); err != nil {
log.Fatalf("Proxy failed: %v", err)
}
}()
wg.Wait()
}