-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.go
74 lines (63 loc) · 1.79 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/context"
)
func indexHandler(config clientConfig) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
// For tesing more than anyting. It should hit the else 99% of the
// time outside of testing.
sgConn, ok := context.GetOk(req, "sg_conn")
var sg Shotgun
if ok {
sg = sgConn.(Shotgun)
} else {
sg = NewShotgun(config.shotgunHost, "fake-script", "fake-key")
}
sgReq, err := sg.Request("info", make(map[string]interface{}))
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
if sgReq.StatusCode >= 400 && sgReq.StatusCode < 500 {
log.Errorf("Shotgun Response Status Code: %v ", sgReq.StatusCode)
rw.WriteHeader(http.StatusInternalServerError)
return
} else if sgReq.StatusCode >= 500 {
rw.WriteHeader(http.StatusBadGateway)
return
}
var infoResp map[string]interface{}
respBody, err := ioutil.ReadAll(sgReq.Body)
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
err = json.Unmarshal(respBody, &infoResp)
if err != nil {
log.Error(err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
versionSlice := infoResp["version"].([]interface{})
infoJson := make(map[string]interface{})
infoJson["shotgun_version"] = fmt.Sprintf("v%d.%d.%d",
int(versionSlice[0].(float64)),
int(versionSlice[1].(float64)),
int(versionSlice[2].(float64)))
infoJson["rest_version"] = Version
encoder := json.NewEncoder(rw)
err = encoder.Encode(infoJson)
if err != nil {
log.Error("Error encoding info json.")
rw.WriteHeader(http.StatusInternalServerError)
return
}
}
}