-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (83 loc) · 2.09 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"time"
"github.com/deatheyes/mongo-proxy/config"
"github.com/deatheyes/mongo-proxy/meta"
"github.com/deatheyes/mongo-proxy/proxy"
"github.com/golang/glog"
)
var (
version bool
conf string
port string
logDir string
psm string
// version info
sha1 string
date string
branch string
)
func init() {
flag.StringVar(&conf, "conf_dir", "./conf", "config file directory")
flag.StringVar(&port, "port", "27019", "proxy port")
flag.BoolVar(&version, "version", false, "show version")
glog.MaxSize = 1 << 28
}
func showVersion() {
fmt.Printf("mongo-proxy - author yanyu - https://github.com/deatheyes/mongo-proxy - %s - %s - %s", branch, date, sha1)
}
func main() {
flag.Parse()
if version {
showVersion()
return
}
address := ":" + port
if len(port) == 0 {
address = ":27019"
}
fmt.Println("serve on ", port)
c, err := config.FromFile(filepath.Join(conf, "config.yaml"))
if err != nil {
glog.Fatalf("get config from failed: %v", err)
return
}
glog.Infof("mongo-proxy - branch: %s, date: %s, sha1: %s", branch, date, sha1)
// create meta client
clusterMetaClient, err := meta.CreateClient(c.Meta.Cluster)
if err != nil {
glog.Fatalf("init cluster meta client failed: %v", err)
return
}
if err := clusterMetaClient.Connect(context.Background()); err != nil {
glog.Fatalf("connect cluster meta server faild: %v", err)
return
}
authMetaClient, err := meta.CreateClient(c.Meta.Auth)
if err != nil {
glog.Fatalf("init auth meta client failed: %v", err)
return
}
if err := authMetaClient.Connect(context.Background()); err != nil {
glog.Fatalf("connect auth meta server faild: %v", err)
return
}
// create and run proxy
if err := proxy.New().
ClusterMetaServer(clusterMetaClient).
AuthMetaServer(authMetaClient).
AliveTimeout(time.Duration(c.Connection.AliveTimeoutSecond) * time.Second).
Serve(address); err != nil {
glog.Fatalf("init proxy failed: %v", err)
return
}
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
<-interrupt
}