-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
72 lines (61 loc) · 2.12 KB
/
Copy pathmain.go
File metadata and controls
72 lines (61 loc) · 2.12 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
package main
import (
"context"
"log"
"os"
"os/signal"
"sync"
"syscall"
"github.com/supporttools/go-sql-proxy/pkg/config"
"github.com/supporttools/go-sql-proxy/pkg/logging"
"github.com/supporttools/go-sql-proxy/pkg/metrics"
"github.com/supporttools/go-sql-proxy/pkg/proxy"
)
var logger = logging.SetupLogging()
func main() {
logger.Println("Starting go-sql-proxy server...")
config.LoadConfiguration()
if config.CFG.Debug {
logger.Println("Debug mode enabled")
logger.Println("Configuration:")
logger.Printf("Debug: %t", config.CFG.Debug)
logger.Printf("Metrics Port: %d", config.CFG.MetricsPort)
logger.Printf("Source Database Server: %s", config.CFG.SourceDatabaseServer)
logger.Printf("Source Database Port: %d", config.CFG.SourceDatabasePort)
logger.Printf("Source Database User: %s", config.CFG.SourceDatabaseUser)
//logger.Printf("Source Database Password: %s", config.CFG.SourceDatabasePassword)
logger.Printf("Bind Address: %s", config.CFG.BindAddress)
logger.Printf("Bind Port: %d", config.CFG.BindPort)
}
go func() {
logger.Println("Starting metrics server...")
metrics.StartMetricsServer()
}()
// Create a context to manage the proxy server
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Ensure cancel is called to release resources if main exits before signal
p := proxy.NewProxy(ctx, config.CFG.SourceDatabaseServer, config.CFG.SourceDatabasePort, config.CFG.UseSSL)
p.EnableDecoding = true
var wg sync.WaitGroup
// Setup signal handling
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
log.Println("Signal received, stopping and exiting...")
cancel() // Notify all operations to start shutting down
wg.Wait() // Wait for all goroutines to finish
os.Exit(0) // Ensure the program exits
}()
wg.Add(1)
go func() {
defer wg.Done()
// Start the proxy server in a goroutine to allow shutdown process to proceed
err := proxy.StartProxy(p, config.CFG.BindPort)
if err != nil {
log.Fatalf("Failed to start proxy server: %v", err)
}
}()
// Wait here until signal is received and handled
<-ctx.Done()
}