-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
266 lines (237 loc) · 9.36 KB
/
Copy pathmain.go
File metadata and controls
266 lines (237 loc) · 9.36 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"go-llm-proxy/internal/auth"
"go-llm-proxy/internal/config"
"go-llm-proxy/internal/handler"
"go-llm-proxy/internal/httputil"
"go-llm-proxy/internal/mcp"
"go-llm-proxy/internal/pipeline"
"go-llm-proxy/internal/ratelimit"
"go-llm-proxy/internal/usage"
)
// Version is set at build time via -ldflags:
//
// go build -ldflags "-X main.Version=v0.3.8" .
//
// When unset (dev builds), defaults to "dev".
var Version = "dev"
func main() {
configPath := flag.String("config", "config.yaml", "path to config file")
addUser := flag.Bool("adduser", false, "interactively add a new API key to the config")
serveConfigPage := flag.Bool("serve-config-generator", false, "serve the config generator UI at GET /")
serveDashboard := flag.Bool("serve-dashboard", false, "serve the usage dashboard at /usage")
logMetrics := flag.Bool("log-metrics", false, "enable per-request usage logging to SQLite")
usageReport := flag.Bool("usage-report", false, "print usage summary report and exit")
modelReport := flag.Bool("model-report", false, "print per-model usage report and exit")
reportDays := flag.Int("report-days", 30, "number of days to include in reports")
usageDBPath := flag.String("usage-db", "", "path to SQLite usage database (overrides config)")
logDebug := flag.Bool("log-debug", false, "enable debug-level logging for translation troubleshooting")
showVersion := flag.Bool("version", false, "print version and exit")
flag.Parse()
if *showVersion {
fmt.Println("go-llm-proxy", Version)
return
}
if *addUser {
auth.RunAddUser(*configPath)
return
}
// Handle report modes: load config to find DB path, then print report and exit.
if *usageReport || *modelReport {
dbPath := *usageDBPath
if dbPath == "" {
cs, err := config.NewConfigStore(*configPath)
if err == nil {
if cfg := cs.Get(); cfg.UsageDB != "" {
dbPath = cfg.UsageDB
}
}
}
if dbPath == "" {
dbPath = "usage.db"
}
if *usageReport {
usage.RunUsageReport(dbPath, *reportDays)
}
if *modelReport {
usage.RunModelReport(dbPath, *reportDays)
}
return
}
logLevel := slog.LevelInfo
if *logDebug {
logLevel = slog.LevelDebug
}
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
})))
cs, err := config.NewConfigStore(*configPath)
if err != nil {
slog.Error("failed to load config", "error", err)
os.Exit(1)
}
cfg := cs.Get()
// Initialize usage logger if enabled via CLI flag or config.
var ul *usage.UsageLogger
if *logMetrics || cfg.LogMetrics {
dbPath := *usageDBPath
if dbPath == "" && cfg.UsageDB != "" {
dbPath = cfg.UsageDB
}
if dbPath == "" {
dbPath = "usage.db"
}
var err error
ul, err = usage.NewUsageLogger(dbPath)
if err != nil {
slog.Error("failed to open usage database", "error", err, "path", dbPath)
os.Exit(1)
}
slog.Info("usage logging enabled", "db", dbPath)
}
// Auto-detect context window sizes from backends (async, non-blocking).
config.DetectContextWindows(cs)
// Start health checker for model availability tracking.
healthStore := config.NewHealthStore(cs, 30*time.Second, 5*time.Second)
// Create the processing pipeline (shared by all handlers).
pl := pipeline.NewPipeline(cs, httputil.NewHTTPClient())
proxy := handler.NewProxyHandler(cs, ul, pl)
responses := handler.NewResponsesHandler(cs, ul, pl)
messages := handler.NewMessagesHandler(cs, ul, pl)
models := handler.NewModelsHandler(cs, healthStore)
rl := ratelimit.NewRateLimiter(cfg.TrustedProxies)
var dashRl *ratelimit.RateLimiter
if (*serveDashboard || cfg.UsageDashboard) && ul != nil {
dashRl = ratelimit.NewRateLimiter(cfg.TrustedProxies)
}
cs.SetOnReload(func(newCfg *config.Config) {
rl.SetTrustedProxies(newCfg.TrustedProxies)
healthStore.RefreshFromConfig()
if dashRl != nil {
dashRl.SetTrustedProxies(newCfg.TrustedProxies)
}
config.DetectContextWindows(cs)
})
// Watch config file for changes (auto-reload on save).
stopWatch, err := cs.Watch()
if err != nil {
slog.Error("failed to watch config file", "error", err)
// Non-fatal: SIGHUP still works as a fallback.
}
// Reload config on SIGHUP (manual trigger, systemd ExecReload).
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP)
for range sig {
slog.Info("received SIGHUP, reloading config")
if err := cs.Load(); err != nil {
slog.Error("failed to reload config", "error", err)
}
}
}()
mux := http.NewServeMux()
if *serveConfigPage || cfg.ServeConfigGenerator {
configPage := handler.NewConfigPageHandler(cs, healthStore)
mux.Handle("GET /{$}", configPage)
slog.Info("config generator page enabled at GET /")
}
if dashRl != nil {
dash := handler.NewUsageDashboardHandler(cs, ul, dashRl)
mux.Handle("GET /usage", http.HandlerFunc(dash.LoginPage))
mux.Handle("POST /usage", http.HandlerFunc(dash.HandleLogin))
mux.Handle("POST /usage/logout", http.HandlerFunc(dash.HandleLogout))
mux.Handle("GET /usage/data", http.HandlerFunc(dash.ServeData))
slog.Info("usage dashboard enabled at /usage")
admin := handler.NewAdminHandler(cs, dashRl, healthStore)
mux.Handle("GET /admin", http.HandlerFunc(admin.Root))
mux.Handle("GET /admin/", http.HandlerFunc(admin.Root))
mux.Handle("GET /admin/login", http.HandlerFunc(admin.LoginPage))
mux.Handle("POST /admin/login", http.HandlerFunc(admin.HandleLogin))
mux.Handle("POST /admin/logout", http.HandlerFunc(admin.HandleLogout))
mux.Handle("GET /admin/users", admin.RequirePage(admin.UsersPage))
mux.Handle("GET /admin/users/data", admin.RequireAPI(admin.UsersData))
mux.Handle("POST /admin/users/mutate", admin.RequireAPI(admin.UsersMutate))
mux.Handle("GET /admin/models", admin.RequirePage(admin.ModelsPage))
mux.Handle("GET /admin/models/data", admin.RequireAPI(admin.ModelsData))
mux.Handle("POST /admin/models/mutate", admin.RequireAPI(admin.ModelsMutate))
mux.Handle("GET /admin/processors", admin.RequirePage(admin.ProcessorsPage))
mux.Handle("GET /admin/processors/data", admin.RequireAPI(admin.ProcessorsData))
mux.Handle("POST /admin/processors/mutate", admin.RequireAPI(admin.ProcessorsMutate))
slog.Info("admin page enabled at /admin")
}
mux.Handle("GET /v1/models", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, models)))
mux.Handle("GET /v1/models/status", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, http.HandlerFunc(models.ServeStatus))))
mux.Handle("POST /v1/responses", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, responses)))
mux.Handle("POST /v1/responses/compact", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs,
http.HandlerFunc(responses.HandleCompact),
)))
countTokens := handler.NewCountTokensHandler(cs, ul)
mux.Handle("POST /v1/messages/count_tokens", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, countTokens)))
mux.Handle("POST /anthropic/v1/messages/count_tokens", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, countTokens)))
mux.Handle("POST /v1/messages", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, messages)))
mux.Handle("POST /anthropic/v1/messages", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, messages)))
mux.Handle("/v1/", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, proxy)))
mux.Handle("/anthropic/", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, proxy)))
// MCP endpoint for web search (OpenCode, Qwen Code, any MCP client).
mcpHandler := mcp.NewHandler(cs, pl)
mux.Handle("GET /mcp/sse", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, http.HandlerFunc(mcpHandler.ServeSSE))))
mux.Handle("POST /mcp/messages", ratelimit.RateLimitMiddleware(rl, auth.AuthMiddleware(cs, http.HandlerFunc(mcpHandler.ServeMessages))))
if cfg.Processors.WebSearchKey != "" {
slog.Info("MCP endpoint enabled at /mcp/sse (web_search tool available)")
}
// Qdrant vector database proxy with app isolation.
if cfg.Services.Qdrant != nil {
qdrant := handler.NewQdrantHandler(cs, ul)
mux.Handle("/qdrant/", ratelimit.RateLimitMiddleware(rl, auth.AppKeyAuthMiddleware(cs, qdrant)))
slog.Info("qdrant proxy enabled at /qdrant/", "backend", cfg.Services.Qdrant.Backend, "app_keys", len(cfg.Services.Qdrant.AppKeys))
}
// Start health checker background goroutine.
healthStore.Start(context.Background())
srv := &http.Server{
Addr: cfg.Listen,
Handler: httputil.RecoveryMiddleware(mux),
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
MaxHeaderBytes: 1 << 16, // 64 KB
// Note: no WriteTimeout — streaming SSE responses can run for minutes.
}
// Graceful shutdown on SIGTERM/SIGINT.
go func() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
sig := <-quit
slog.Info("shutting down", "signal", sig.String())
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
slog.Error("shutdown error", "error", err)
}
rl.Close()
if dashRl != nil {
dashRl.Close()
}
if ul != nil {
ul.Close()
}
healthStore.Stop()
if stopWatch != nil {
stopWatch()
}
}()
slog.Info("starting server", "listen", cfg.Listen)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
slog.Error("server failed", "error", err)
os.Exit(1)
}
slog.Info("server stopped")
}