fix(traefik): parse JSON response from sablier

This commit is contained in:
Alexis Couvreur
2022-10-04 19:03:40 +00:00
parent 7fbc0df429
commit 70d93a0c3b

View File

@@ -1,10 +1,9 @@
package strategy
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"time"
)
@@ -13,6 +12,11 @@ var netClient = &http.Client{
Timeout: time.Second * 2,
}
type SablierResponse struct {
State string `json:"state"`
Error string `json:"error"`
}
type Strategy interface {
ServeHTTP(rw http.ResponseWriter, req *http.Request)
}
@@ -25,15 +29,16 @@ func getServiceStatus(request string) (string, error) {
return "error", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
decoder := json.NewDecoder(resp.Body)
var response SablierResponse
err = decoder.Decode(&response)
if err != nil {
return "parsing error", err
return "error from ondemand service", err
}
if resp.StatusCode >= 400 {
return "error from ondemand service", errors.New(string(body))
return "error from ondemand service", errors.New(response.Error)
}
return strings.TrimSuffix(string(body), "\n"), nil
return response.State, nil
}