forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (110 loc) · 2.87 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
131
132
133
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"runtime"
"runtime/debug"
"strings"
"github.com/eBay/fabio/consul"
"github.com/eBay/fabio/metrics"
"github.com/eBay/fabio/route"
"github.com/eBay/fabio/ui"
)
var version = "1.0.1"
func main() {
var cfg string
var v bool
flag.StringVar(&cfg, "cfg", "", "path to config file")
flag.BoolVar(&v, "v", false, "show version")
flag.Parse()
if v {
fmt.Println(version)
return
}
log.Printf("[INFO] Version %s starting", version)
if cfg != "" {
if err := loadConfig(cfg); err != nil {
log.Fatal("[FATAL] ", err)
}
}
if err := metrics.Init(metricsTarget, metricsPrefix, metricsInterval, metricsGraphiteAddr); err != nil {
log.Fatal("[FATAL] ", err)
}
if os.Getenv("GOMAXPROCS") == "" {
log.Print("[INFO] Setting GOMAXPROCS=", gomaxprocs)
runtime.GOMAXPROCS(gomaxprocs)
} else {
log.Print("[INFO] Using GOMAXPROCS=", os.Getenv("GOMAXPROCS"), " from env")
}
if os.Getenv("GOGC") == "" {
log.Print("[INFO] Setting GOGC=", gogc)
debug.SetGCPercent(gogc)
} else {
log.Print("[INFO] Using GOGC=", os.Getenv("GOGC"), " from env")
}
if proxyRoutes == "" {
useDynamicRoutes()
} else {
useStaticRoutes()
}
if err := route.SetPickerStrategy(proxyStrategy); err != nil {
log.Fatal("[FATAL] ", err)
}
consul.Addr = consulAddr;
consul.URL = consulURL;
dc, err := consul.Datacenter()
if err != nil {
log.Fatal("[FATAL] ", err)
}
log.Printf("[INFO] Using routing strategy %q", proxyStrategy)
log.Printf("[INFO] Connecting to consul on %q in datacenter %q", consulAddr, dc)
log.Printf("[INFO] Consul can be reached via %q", consulURL)
log.Printf("[INFO] UI listening on %q", uiAddr)
go func() {
if err := ui.Start(uiAddr, consulKVPath); err != nil {
log.Fatal("[FATAL] ui: ", err)
}
}()
tr := &http.Transport{
ResponseHeaderTimeout: proxyTimeout,
MaxIdleConnsPerHost: proxyMaxConn,
Dial: (&net.Dialer{
Timeout: proxyDialTimeout,
KeepAlive: proxyTimeout,
}).Dial,
}
proxy := route.NewProxy(tr, proxyHeaderClientIP, proxyHeaderTLS, proxyHeaderTLSValue)
listen(proxyAddr, proxyShutdownWait, proxy)
}
func useDynamicRoutes() {
log.Printf("[INFO] Using dynamic routes from consul on %s", consulAddr)
log.Printf("[INFO] Using tag prefix %q", consulTagPrefix)
log.Printf("[INFO] Watching KV path %q", consulKVPath)
go func() {
w, err := consul.NewWatcher(consulTagPrefix, consulKVPath)
if err != nil {
log.Fatal("[FATAL] ", err)
}
w.Watch()
}()
}
func useStaticRoutes() {
var err error
var t route.Table
if strings.HasPrefix(proxyRoutes, "@") {
proxyRoutes = proxyRoutes[1:]
log.Print("[INFO] Using static routes from ", proxyRoutes)
t, err = route.ParseFile(proxyRoutes)
} else {
log.Print("[INFO] Using static routes from config file")
t, err = route.ParseString(proxyRoutes)
}
if err != nil {
log.Fatal("[FATAL] ", err)
}
route.SetTable(t)
}