forked from jamescun/switcher
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
60 lines (49 loc) · 1.32 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
package main
import (
"encoding/json"
"flag"
"log"
"os"
"time"
)
type AppConfig struct {
ListenAddress string `json:"listen"`
DefaultAddress string `json:"default"`
ProbeTimeout time.Duration `json:"timeout"`
ConnectTimeout time.Duration `json:"connect_timeout"`
ProtocolList []json.RawMessage `json:"protocols"`
}
func parseConfig() AppConfig {
configFile := flag.String("config", "default.cfg", "Json format config file")
flag.Parse()
var config AppConfig
if f, err := os.Open(*configFile); err != nil {
log.Fatalf("[FATAL] config file open error:", err)
} else {
defer f.Close()
if err = json.NewDecoder(f).Decode(&config); err != nil {
log.Fatalln("[FATAL] config parser error")
}
}
return config
}
func main() {
config := parseConfig()
mux := NewMux()
mux.defaultAddress = config.DefaultAddress
if t := config.ConnectTimeout; t > 0 {
mux.connectTimeout = t * time.Second
}
if t := config.ProbeTimeout; t > 0 {
mux.probeTimeout = t * time.Second
}
var pm ProtocolManager
if err := pm.LoadProtocols(config.ProtocolList); err != nil {
log.Panicln("protocols load error:", err)
}
mux.pm = &pm
log.Printf("[INFO] listen: %s\n", config.ListenAddress)
if err := mux.ListenAndServe(config.ListenAddress); err != nil {
log.Fatalf("[FATAL] listen: %s\n", err)
}
}