2019-11-05 23:32:09 +00:00
|
|
|
package feta
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
import "net/http"
|
|
|
|
import "encoding/json"
|
|
|
|
|
|
|
|
import "github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
type hash map[string]interface{}
|
|
|
|
|
2019-11-06 01:15:31 +00:00
|
|
|
func (a *FetaAPIServer) instances() []hash {
|
2019-11-05 23:32:09 +00:00
|
|
|
resp := make([]hash, 0)
|
|
|
|
now := time.Now()
|
2019-11-06 01:15:31 +00:00
|
|
|
for _, v := range a.feta.manager.listInstances() {
|
2019-11-05 23:32:09 +00:00
|
|
|
i := make(hash)
|
|
|
|
// FIXME figure out why a very short lock here deadlocks
|
|
|
|
v.Lock()
|
|
|
|
i["hostname"] = v.hostname
|
|
|
|
i["nextCheck"] = v.nextFetch.UTC().Format(time.RFC3339)
|
|
|
|
i["nextCheckAfter"] = (-1 * now.Sub(v.nextFetch)).String()
|
|
|
|
i["successCount"] = v.successCount
|
|
|
|
i["errorCount"] = v.errorCount
|
|
|
|
i["identified"] = v.identified
|
|
|
|
i["status"] = v.status
|
|
|
|
i["software"] = "unknown"
|
|
|
|
i["version"] = "unknown"
|
|
|
|
if v.identified {
|
|
|
|
i["software"] = v.serverImplementationString
|
|
|
|
i["version"] = v.serverVersionString
|
|
|
|
}
|
|
|
|
v.Unlock()
|
|
|
|
resp = append(resp, i)
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2019-11-06 01:15:31 +00:00
|
|
|
func (a *FetaAPIServer) getIndexHandler() http.HandlerFunc {
|
2019-11-05 23:32:09 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
index := &gin.H{
|
|
|
|
"page": "index",
|
|
|
|
"instances": a.instances(),
|
|
|
|
"status": "ok",
|
|
|
|
"now": time.Now().UTC().Format(time.RFC3339),
|
2019-11-06 01:15:31 +00:00
|
|
|
"uptime": a.feta.Uptime().String(),
|
2019-11-05 23:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json, err := json.Marshal(index)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(json)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 01:15:31 +00:00
|
|
|
func (a *FetaAPIServer) getHealthCheckHandler() http.HandlerFunc {
|
2019-11-05 23:32:09 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
resp := &gin.H{
|
|
|
|
"status": "ok",
|
|
|
|
"now": time.Now().UTC().Format(time.RFC3339),
|
2019-11-06 01:15:31 +00:00
|
|
|
"uptime": a.feta.Uptime().String(),
|
2019-11-05 23:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(json)
|
|
|
|
}
|
|
|
|
}
|