-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
118 lines (100 loc) · 2.99 KB
/
main.go
File metadata and controls
118 lines (100 loc) · 2.99 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/hypnguyen1209/fasthttp-reverse-proxy/config"
"github.com/hypnguyen1209/fasthttp-reverse-proxy/server"
"github.com/hypnguyen1209/fasthttp-reverse-proxy/utils"
"github.com/valyala/fasthttp"
)
func main() {
var (
configPath = flag.String("config", "config.yml", "Path to configuration file")
routesPath = flag.String("routes", "routes.yml", "Path to routes file")
)
flag.Parse()
// Load configuration
cfg, err := config.LoadConfig(*configPath)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
// Load routes
routes, err := config.LoadRoutes(*routesPath)
if err != nil {
log.Fatalf("Failed to load routes: %v", err)
}
// Create proxy server
proxy, err := server.NewProxyServer(cfg, routes, *configPath, *routesPath)
if err != nil {
log.Fatalf("Failed to create proxy server: %v", err)
}
defer proxy.Close()
// Configure fasthttp server
httpServer := &fasthttp.Server{
Handler: proxy.Handler,
MaxConnsPerIP: cfg.Server.MaxConnsPerIP,
MaxRequestBodySize: utils.ParseSize(cfg.Server.MaxRequestBodySize),
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
}
// Parse timeouts
if cfg.Server.ReadTimeout != "" {
if timeout, err := time.ParseDuration(cfg.Server.ReadTimeout); err == nil {
httpServer.ReadTimeout = timeout
}
}
if cfg.Server.WriteTimeout != "" {
if timeout, err := time.ParseDuration(cfg.Server.WriteTimeout); err == nil {
httpServer.WriteTimeout = timeout
}
}
if cfg.Server.IdleTimeout != "" {
if timeout, err := time.ParseDuration(cfg.Server.IdleTimeout); err == nil {
httpServer.IdleTimeout = timeout
}
}
// Server address
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
// Setup graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
log.Println("Shutting down server...")
cancel()
// Close proxy server resources
if err := proxy.Close(); err != nil {
log.Printf("Proxy cleanup error: %v", err)
}
if err := httpServer.Shutdown(); err != nil {
log.Printf("Server shutdown error: %v", err)
}
}()
// Start server
log.Printf("Starting FastHTTP Reverse Proxy on %s", addr)
log.Printf("Health check endpoint: %s", cfg.Health.Path)
log.Printf("Loaded %d routes", len(routes.Routes))
log.Printf("Dynamic configuration reload enabled (SIGHUP or file changes)")
log.Printf("Watching files: %s, %s", *configPath, *routesPath)
var serverErr error
if cfg.TLS.Enabled {
log.Printf("Starting HTTPS server with TLS")
serverErr = httpServer.ListenAndServeTLS(addr, cfg.TLS.CertFile, cfg.TLS.KeyFile)
} else {
serverErr = httpServer.ListenAndServe(addr)
}
if serverErr != nil {
log.Fatalf("Server error: %v", serverErr)
}
<-ctx.Done()
log.Println("Server stopped")
}