/
params.go
81 lines (68 loc) · 1.88 KB
/
params.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
package main
import (
"io/ioutil"
"github.com/influxdb/influxdb/client"
goyaml "gopkg.in/yaml.v2"
)
const PARAM_FILE string = "params.yml"
type Params struct {
Port int `yaml:"port,omitempty"`
Host string `yaml:"host,omitempty"`
InfluxDBClientConfig *client.ClientConfig `yaml:"influxdb,omitempty"`
InfluxDBClient *client.Client `yaml:"-"`
}
func (this *Params) LoadFromYamlFile(fileName string) error {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
return this.LoadFromYamlData(data)
}
func (this *Params) LoadFromYamlData(data []byte) error {
var err error
p := new(Params)
if err = goyaml.Unmarshal(data, p); err != nil {
return err
}
if p.Host != "" {
this.Host = p.Host
}
if p.Port > 0 {
this.Port = p.Port
}
if p.InfluxDBClientConfig == nil {
return nil
}
if p.InfluxDBClientConfig.Host != "" {
this.InfluxDBClientConfig.Host = p.InfluxDBClientConfig.Host
}
if p.InfluxDBClientConfig.Database != "" {
this.InfluxDBClientConfig.Database = p.InfluxDBClientConfig.Database
}
if p.InfluxDBClientConfig.Password != "" {
this.InfluxDBClientConfig.Password = p.InfluxDBClientConfig.Password
}
if p.InfluxDBClientConfig.Username != "" {
this.InfluxDBClientConfig.Username = p.InfluxDBClientConfig.Username
}
this.InfluxDBClientConfig.IsSecure = p.InfluxDBClientConfig.IsSecure
return nil
}
func (this *Params) initInfluxDB() error {
var err error
this.InfluxDBClient, err = client.NewClient(this.InfluxDBClientConfig)
return err
}
func GetDefaultParams() *Params {
influxdbClientConfig := &client.ClientConfig{}
return &Params{7777, "localhost", influxdbClientConfig, nil}
}
var params = GetDefaultParams()
func init() {
if err := params.LoadFromYamlFile(PARAM_FILE); err != nil {
panic(err)
}
if err := params.initInfluxDB(); err != nil {
panic(err)
}
}