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": {
"address": "http://localhost:8086",
"username": "iot-relay",
"password": "",
"db": "grafana",
"measurement": "iot",
"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
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

View file

@ -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"`