-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (118 loc) · 3.05 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"runtime"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
"github.com/RedTimeDB/RedTimeProxy/backend"
"github.com/RedTimeDB/RedTimeProxy/service"
"github.com/RedTimeDB/RedTimeProxy/util"
)
var (
configFile string
version bool
//GitCommit is commit id
GitCommit = "not build"
//BuildTime is build time
BuildTime = "not build"
)
func init() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Llongfile)
log.SetOutput(os.Stdout)
flag.StringVar(&configFile, "config", "proxy.yaml", "proxy config file")
flag.BoolVar(&version, "version", false, "proxy version")
flag.Parse()
}
func main() {
if version {
fmt.Printf("Version: %s\n", backend.Version)
fmt.Printf("Git commit: %s\n", GitCommit)
fmt.Printf("Build time: %s\n", BuildTime)
fmt.Printf("Go version: %s\n", runtime.Version())
fmt.Printf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
return
}
cfg, err := backend.NewConfigYaml(configFile) // Read config file and create config object.
if err != nil {
log.Printf("illegal config file: %s\n", err)
return
}
log.Printf("version: %s, commit: %s, build: %s", backend.Version, GitCommit, BuildTime)
cfg.PrintSummary()
err = util.MakeDir(cfg.DataDir)
if err != nil {
log.Fatalln("create data dir error")
return
}
//判断是够开启UDP-Server
if cfg.UDPEnable {
//开启UDP
go func() {
defer func() {
if r := recover(); r != nil {
log.Println("UDP Server recover", r)
}
}()
err := service.NewUDPService(cfg).ListenAndServe()
if err != nil {
log.Println(err)
}
}()
}
// Serve our metrics.
go func() {
log.Printf("metrics listening at %s", ":9009")
if err := http.ListenAndServe(":9009", promhttp.Handler()); err != nil {
log.Panicf("error while serving metrics: %s", err)
}
}()
//判断是否开启MQTT监听
if cfg.MQTTEnable {
go func() {
col, err := service.NewMQTTService(cfg)
if err != nil {
log.Fatalln(err)
}
c := col.Collect()
for {
select {
case msg := <-c:
//log.Println(msg)
col.WriteMQTT(msg)
}
}
}()
}
mdlw := middleware.New(middleware.Config{
Recorder: metrics.NewRecorder(metrics.Config{}),
})
mux := http.NewServeMux()
service.NewHTTPService(cfg).Register(mux)
server := &http.Server{
Addr: cfg.ListenAddr,
Handler: mux,
IdleTimeout: time.Duration(cfg.IdleTimeout) * time.Second,
}
h := std.Handler("", mdlw, mux)
// Serve our handler.
if cfg.HTTPSEnabled {
log.Printf("https service start, listen on %s", server.Addr)
err = http.ListenAndServeTLS(cfg.ListenAddr, cfg.HTTPSCert, cfg.HTTPSKey, h)
//err = server.ListenAndServeTLS(cfg.HTTPSCert, cfg.HTTPSKey)
} else {
log.Printf("http service start, listen on %s", server.Addr)
err = http.ListenAndServe(cfg.ListenAddr, h)
//err = server.ListenAndServe()
}
if err != nil {
log.Print(err)
return
}
}