diff --git a/config.json.templ b/config.json.templ index 3833e78..6abee90 100644 --- a/config.json.templ +++ b/config.json.templ @@ -5,6 +5,8 @@ }, "client": { "address": "http://localhost:8086", + "username": "iot-relay", + "password": "", "db": "grafana", "measurement": "iot", "host": "iot-relay" diff --git a/internal/client/client.go b/internal/client/client.go index d5f1c09..f3ccb79 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -42,7 +42,14 @@ func GetHandler(config config.Config) types.Callback { url := config.Client.Address + "/write?db=" + config.Client.DB - resp, err := http.Post(url, "application/octet-stream", strings.NewReader(line)) + client := &http.Client{} + req, err := http.NewRequest("POST", url, strings.NewReader(line)) + + if len(config.Client.Username) != 0 && len(config.Client.Password) != 0 { + req.SetBasicAuth(config.Client.Username, config.Client.Password) + } + + resp, err := client.Do(req) if err != nil { log.Println(err) return err diff --git a/internal/config/config.go b/internal/config/config.go index a713409..81e7a7c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -13,6 +13,8 @@ type Config struct { } `json:"server"` Client struct { Address string `json:"address"` + Username string `json:"username"` + Password string `json:"password"` DB string `json:"db"` Measurement string `json:"measurement"` Host string `json:"host"`