forked from pruepei/influx-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (100 loc) · 2.25 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
// Copyright 2016 Eleme. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"errors"
"flag"
"log"
"net/http"
"os"
"time"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
redis "gopkg.in/redis.v5"
"github.com/shell909090/influx-proxy/backend"
)
var (
ErrConfig = errors.New("config parse error")
ConfigFile string
NodeName string
RedisAddr string
LogFilePath string
)
func init() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
flag.StringVar(&LogFilePath, "log-file-path", "/var/log/influx-proxy.log", "output file")
flag.StringVar(&ConfigFile, "config", "", "config file")
flag.StringVar(&NodeName, "node", "l1", "node name")
flag.StringVar(&RedisAddr, "redis", "localhost:6379", "config file")
flag.Parse()
}
type Config struct {
redis.Options
Node string
}
func LoadJson(configfile string, cfg interface{}) (err error) {
file, err := os.Open(configfile)
if err != nil {
return
}
defer file.Close()
dec := json.NewDecoder(file)
err = dec.Decode(&cfg)
return
}
func initLog() {
if LogFilePath == "" {
log.SetOutput(os.Stdout)
} else {
log.SetOutput(&lumberjack.Logger{
Filename: LogFilePath,
MaxSize: 100,
MaxBackups: 5,
MaxAge: 7,
})
}
}
func main() {
initLog()
var err error
var cfg Config
if ConfigFile != "" {
err = LoadJson(ConfigFile, &cfg)
if err != nil {
log.Print("load config failed: ", err)
return
}
log.Printf("json loaded.")
}
if NodeName != "" {
cfg.Node = NodeName
}
if RedisAddr != "" {
cfg.Addr = RedisAddr
}
rcs := backend.NewRedisConfigSource(&cfg.Options, cfg.Node)
nodecfg, err := rcs.LoadNode()
if err != nil {
log.Printf("config source load failed.")
return
}
ic := backend.NewInfluxCluster(rcs, &nodecfg)
ic.LoadConfig()
mux := http.NewServeMux()
NewHttpService(ic, nodecfg.DB).Register(mux)
log.Printf("http service start.")
server := &http.Server{
Addr: nodecfg.ListenAddr,
Handler: mux,
IdleTimeout: time.Duration(nodecfg.IdleTimeout) * time.Second,
}
if nodecfg.IdleTimeout <= 0 {
server.IdleTimeout = 10 * time.Second
}
err = server.ListenAndServe()
if err != nil {
log.Print(err)
return
}
}