mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-27 15:41:41 +01:00
fix(traefik): parse JSON response from sablier
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user