etri-smartfarm-poc-controll.../model/cache.go

41 lines
684 B
Go
Raw Normal View History

2021-11-26 01:58:47 +00:00
package model
import (
"bytes"
"encoding/json"
"etrismartfarmpoccontroller/constants"
"fmt"
"io/ioutil"
"net/http"
)
func (s *dbHandler) GetSID(sname string) (string, error) {
sid, ok := s.cache[sname]
if !ok {
payload := map[string]string{"sname": sname}
b, _ := json.Marshal(payload)
req, err := http.NewRequest("GET",
fmt.Sprintf("http://%s/%s", constants.Config["serverAddr"], "services"),
bytes.NewReader(b),
)
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
sid = string(b)
}
return sid, nil
}