added basic auth for client

This commit is contained in:
overflowerror 2021-11-14 12:44:42 +01:00
parent 3ac76e0d99
commit 6ac99dc2af
3 changed files with 12 additions and 1 deletions

View file

@ -5,6 +5,8 @@
}, },
"client": { "client": {
"address": "http://localhost:8086", "address": "http://localhost:8086",
"username": "iot-relay",
"password": "",
"db": "grafana", "db": "grafana",
"measurement": "iot", "measurement": "iot",
"host": "iot-relay" "host": "iot-relay"

View file

@ -42,7 +42,14 @@ func GetHandler(config config.Config) types.Callback {
url := config.Client.Address + "/write?db=" + config.Client.DB 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 { if err != nil {
log.Println(err) log.Println(err)
return err return err

View file

@ -13,6 +13,8 @@ type Config struct {
} `json:"server"` } `json:"server"`
Client struct { Client struct {
Address string `json:"address"` Address string `json:"address"`
Username string `json:"username"`
Password string `json:"password"`
DB string `json:"db"` DB string `json:"db"`
Measurement string `json:"measurement"` Measurement string `json:"measurement"`
Host string `json:"host"` Host string `json:"host"`