qa: If admin link is down, use local data instead
This commit is contained in:
parent
9a5347b8ef
commit
c31f76e9c3
@ -8,8 +8,6 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var adminLink string
|
var adminLink string
|
||||||
@ -51,48 +49,3 @@ func fwdAdmin(method, urlpath string, body io.Reader, out interface{}) error {
|
|||||||
dec := json.NewDecoder(resp.Body)
|
dec := json.NewDecoder(resp.Body)
|
||||||
return dec.Decode(&out)
|
return dec.Decode(&out)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fwdAdminRequest(c *gin.Context, urlpath string) {
|
|
||||||
u, err := url.Parse(adminLink)
|
|
||||||
if err != nil {
|
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var user, pass string
|
|
||||||
if u.User != nil {
|
|
||||||
user = u.User.Username()
|
|
||||||
pass, _ = u.User.Password()
|
|
||||||
u.User = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(urlpath) > 0 {
|
|
||||||
u.Path = path.Join(u.Path, urlpath)
|
|
||||||
} else {
|
|
||||||
u.Path = path.Join(u.Path, c.Request.URL.Path)
|
|
||||||
}
|
|
||||||
|
|
||||||
r, err := http.NewRequest(c.Request.Method, u.String(), c.Request.Body)
|
|
||||||
if err != nil {
|
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(user) != 0 || len(pass) != 0 {
|
|
||||||
r.SetBasicAuth(user, pass)
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(r)
|
|
||||||
if err != nil {
|
|
||||||
c.AbortWithStatusJSON(http.StatusBadGateway, gin.H{"errmsg": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
headers := map[string]string{}
|
|
||||||
for key := range resp.Header {
|
|
||||||
headers[key] = resp.Header.Get(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.DataFromReader(resp.StatusCode, resp.ContentLength, resp.Header.Get("Content-Type"), resp.Body, headers)
|
|
||||||
}
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
adminapi "srs.epita.fr/fic-server/admin/api"
|
||||||
"srs.epita.fr/fic-server/libfic"
|
"srs.epita.fr/fic-server/libfic"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -68,5 +69,13 @@ func showExercice(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fwdAdminRequest(c, fmt.Sprintf("/api/exercices/%s", string(c.Param("eid"))))
|
var e adminapi.Exercice
|
||||||
|
err := fwdAdmin(c.Request.Method, fmt.Sprintf("/api/exercices/%s", string(c.Param("eid"))), nil, &e)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to make request to admin:", err.Error())
|
||||||
|
c.JSON(http.StatusOK, c.MustGet("exercice"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, e)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user