-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (69 loc) · 1.86 KB
/
main.go
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
package main
import (
_ "bytes"
_ "database/sql"
"flag"
"github.com/caybokotze/dbmux/config"
"github.com/caybokotze/dbmux/database"
"github.com/caybokotze/dbmux/proxy"
"log"
"os"
"os/signal"
"syscall"
)
func main() {
Initialise()
}
func Initialise() {
hostPort := flag.Uint("host", 3307, "Specify the port the sql server is running on")
proxyPort := flag.Uint("proxy", 3308, "Specify the port where the current server instance is running")
verbosityEnabled := flag.Bool( "enable-verbosity", true, "Enable verbosity to see the output in terminal")
flag.Parse()
log.SetOutput(os.Stdout)
conf, err := fetchConfiguration(hostPort, proxyPort)
if err != nil {
log.Fatal("Configuration could not be found for this service, please make sure you have a valid config file.")
}
dbn, dbErr := database.CreateConnectionToDbHost(conf)
if dbErr != nil {
log.Fatal("Count not create a connection to the database")
}
p := proxy.CreateNewProxy(proxy.Arguments{
ProxyPort: *proxyPort,
HostPort: *hostPort,
BufferSize: 4096,
ThreadPoolSize: 50,
VerbosityEnabled: *verbosityEnabled,
DatabaseHost: dbn,
})
go p.StartTcpProxying()
waitForSignal()
}
func fetchConfiguration(bindingPort, proxyPort *uint)(configuration config.Configuration, err error) {
conf, err := config.GetConfiguration()
if err != nil {
log.Println("Error fetching config")
return config.Configuration{}, err
}
if *proxyPort != 0 {
conf.ProxyPort = *proxyPort
}
if *bindingPort == 0 {
conf.DbPort = *bindingPort
}
return conf, nil
}
/*
Waits for the TCP channel to open or close.
*/
func waitForSignal() {
var sigChan = make(chan os.Signal, 1)
signal.Notify(sigChan)
for sig := range sigChan {
if sig == syscall.SIGINT || sig == syscall.SIGTERM {
log.Printf("terminated by signal %v\n", sig)
} else {
log.Printf("received signal: %v, ignore\n", sig)
}
}
}