/
main.go
107 lines (92 loc) · 2.42 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
package main
import (
"flag"
"log"
"time"
"net/http"
_ "net/http/pprof"
"github.com/BurntSushi/toml"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/gabrielperezs/elinproxy/httpsrv"
"github.com/gabrielperezs/elinproxy/httpsrv/cacherules"
)
const (
version = "0.3.2"
)
var (
pprof bool
prometheusPort string
serverConfFile string
cacheConfFile string
server *httpsrv.SRV
)
func main() {
flag.StringVar(&serverConfFile, "server", "server.conf", "Config for the server")
flag.StringVar(&cacheConfFile, "cache", "cache.conf", "Cache rules")
flag.BoolVar(&pprof, "pprof", false, "Enable pprof in :6060")
flag.StringVar(&prometheusPort, "prometheusport", ":2112", "Prometheus metrics via HTTP")
flag.Parse()
if pprof {
go func() {
log.Printf("Starting http/pprof port :6060")
http.ListenAndServe(":6060", nil)
}()
}
if prometheusPort != "false" {
go func() {
defer func() {
if err := recover(); err != nil {
log.Printf("Prometheus metrics error: %s", err)
}
}()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(prometheusPort, nil)
}()
}
startOrReload()
}
func startOrReload() {
log.Printf("Version %s", version)
srvConf, err := loadServerConf(serverConfFile)
if err != nil {
log.Printf("ERROR Server Config: %v", err)
return
}
srvConf.Handler.CacheRules, err = loadCacheRules(cacheConfFile)
if err != nil {
log.Printf("ERROR Cache Config: %v", err)
return
}
if server != nil {
server.Reload(srvConf)
} else {
server = httpsrv.New(srvConf)
server.Listen()
}
}
func loadServerConf(file string) (*httpsrv.Config, error) {
srvConf := &httpsrv.Config{}
if _, err := toml.DecodeFile(serverConfFile, srvConf); err != nil {
return nil, err
}
var err error
srvConf.Handler.Cache.ExtraTTL, err = time.ParseDuration(srvConf.Handler.Cache.ExtraTTLString)
if err != nil {
log.Printf("D: Cache ExtraTTL: %s", srvConf.Handler.Cache.ExtraTTLString)
return nil, err
}
srvConf.Handler.Cache.MinLSMTTL, err = time.ParseDuration(srvConf.Handler.Cache.MinLSMTTLString)
if err != nil {
log.Printf("D: Cache MinLSMTTL: %s", srvConf.Handler.Cache.MinLSMTTLString)
return nil, err
}
return srvConf, nil
}
func loadCacheRules(file string) (*cacherules.Rules, error) {
cacheConf := &cacherules.Rules{}
if _, err := toml.DecodeFile(file, cacheConf); err != nil {
return nil, err
}
cacheConf.Parse()
return cacheConf, nil
}