-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
48 lines (40 loc) · 879 Bytes
/
client.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
package influxdb
import (
"fmt"
client "github.com/influxdata/influxdb1-client"
"github.com/spf13/viper"
"log"
"net/url"
)
type Client struct {
host string
port int
database string
client *client.Client
}
func (i *Client) Init() {
i.host = viper.GetString("influxdb.host")
i.port = viper.GetInt("influxdb.port")
i.database = viper.GetString("influxdb.database")
host, err := url.Parse(fmt.Sprintf("http://%s:%d", i.host, i.port))
if err != nil {
log.Fatal(err)
}
i.client, err = client.NewClient(client.Config{URL: *host})
if err != nil {
log.Fatal(err)
}
}
func (i *Client) AddPoint(point client.Point) {
points := make([]client.Point,1)
points[0] = point
bps := client.BatchPoints{
Points: points,
Database: i.database,
RetentionPolicy: "autogen",
}
_, err := i.client.Write(bps)
if err != nil {
log.Fatal(err)
}
}