-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (71 loc) · 2.1 KB
/
main.go
File metadata and controls
89 lines (71 loc) · 2.1 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
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/aikts/proxyai/internal"
)
func main() {
// Parse flags
flag.Parse()
// Get the custom targets flag value after parsing
customTargets := flag.Lookup("targets").Value.String()
// Process custom targets from command line if provided
if customTargets != "" {
processCustomTargets(customTargets)
}
// Load additional or override proxy targets from environment variables
loadProxyTargetsFromEnv()
// Set up logging
log.SetOutput(os.Stdout)
log.SetFlags(log.LstdFlags | log.Lshortfile)
// Log configuration
logConfig()
// Register proxy handlers
registerHandlers()
// Configure the server with timeouts
server := &http.Server{
Addr: config.ListenAddr,
ReadHeaderTimeout: config.ReadHeaderTimeout,
Handler: nil, // Use default ServeMux
}
// Set up graceful shutdown
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
log.Println("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Server shutdown failed: %v", err)
}
}()
// Start the server
log.Printf("Starting multi-API proxy server on %s", server.Addr)
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("Server error: %v", err)
}
log.Println("Server stopped")
}
// Register all HTTP handlers
func registerHandlers() {
// Create handlers for each proxy target path
for _, proxyTarget := range config.ProxyTargets {
// Create a copy of the target for the closure
http.HandleFunc(proxyTarget.PathPrefix, func(w http.ResponseWriter, r *http.Request) {
internal.ProxyHandler(w, r, proxyTarget, config)
})
log.Printf("Registered handler for %s -> %s", proxyTarget.PathPrefix, proxyTarget.TargetHost)
}
// Add a health check endpoint
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
}