mirror of https://github.com/statping/statping
cache updates - api updates
parent
4cfa5c96b9
commit
b232d5b1ff
|
@ -246,13 +246,13 @@ func recordSuccess(s *Service) {
|
||||||
// recordFailure will create a new 'failure' record in the database for a offline service
|
// recordFailure will create a new 'failure' record in the database for a offline service
|
||||||
func recordFailure(s *Service, issue string) {
|
func recordFailure(s *Service, issue string) {
|
||||||
s.Online = false
|
s.Online = false
|
||||||
fail := &types.Failure{
|
fail := &failure{&types.Failure{
|
||||||
Service: s.Id,
|
Service: s.Id,
|
||||||
Issue: issue,
|
Issue: issue,
|
||||||
PingTime: s.PingTime,
|
PingTime: s.PingTime,
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
}
|
}}
|
||||||
utils.Log(2, fmt.Sprintf("Service %v Failing: %v | Lookup in: %0.2f ms", s.Name, issue, fail.PingTime*1000))
|
utils.Log(2, fmt.Sprintf("Service %v Failing: %v | Lookup in: %0.2f ms", s.Name, issue, fail.PingTime*1000))
|
||||||
s.CreateFailure(fail)
|
s.CreateFailure(fail)
|
||||||
notifier.OnFailure(s.Service, fail)
|
notifier.OnFailure(s.Service, fail.Failure)
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,8 @@ type failure struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateFailure will create a new failure record for a service
|
// CreateFailure will create a new failure record for a service
|
||||||
func (s *Service) CreateFailure(f *types.Failure) (int64, error) {
|
func (s *Service) CreateFailure(fail types.FailureInterface) (int64, error) {
|
||||||
|
f := fail.(*failure)
|
||||||
f.Service = s.Id
|
f.Service = s.Id
|
||||||
s.Failures = append(s.Failures, f)
|
s.Failures = append(s.Failures, f)
|
||||||
row := failuresDB().Create(f)
|
row := failuresDB().Create(f)
|
||||||
|
@ -64,10 +65,10 @@ func (s *Service) DeleteFailures() {
|
||||||
s.Failures = nil
|
s.Failures = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LimitedFailures will return the last 10 failures from a service
|
// LimitedFailures will return the last amount of failures from a service
|
||||||
func (s *Service) LimitedFailures() []*failure {
|
func (s *Service) LimitedFailures(amount int64) []*failure {
|
||||||
var failArr []*failure
|
var failArr []*failure
|
||||||
col := failuresDB().Where("service = ?", s.Id).Order("id desc").Limit(10)
|
col := failuresDB().Where("service = ?", s.Id).Order("id asc").Limit(amount)
|
||||||
col.Find(&failArr)
|
col.Find(&failArr)
|
||||||
return failArr
|
return failArr
|
||||||
}
|
}
|
||||||
|
@ -78,6 +79,11 @@ func (f *failure) Ago() string {
|
||||||
return got
|
return got
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Select returns a *types.Failure
|
||||||
|
func (f *failure) Select() *types.Failure {
|
||||||
|
return f.Failure
|
||||||
|
}
|
||||||
|
|
||||||
// Delete will remove a failure record from the database
|
// Delete will remove a failure record from the database
|
||||||
func (f *failure) Delete() error {
|
func (f *failure) Delete() error {
|
||||||
db := failuresDB().Delete(f)
|
db := failuresDB().Delete(f)
|
||||||
|
|
|
@ -58,8 +58,8 @@ type Notification struct {
|
||||||
Author string `gorm:"-" json:"author"`
|
Author string `gorm:"-" json:"author"`
|
||||||
AuthorUrl string `gorm:"-" json:"author_url"`
|
AuthorUrl string `gorm:"-" json:"author_url"`
|
||||||
Icon string `gorm:"-" json:"icon"`
|
Icon string `gorm:"-" json:"icon"`
|
||||||
Delay time.Duration `gorm:"-" json:"delay"`
|
Delay time.Duration `gorm:"-" json:"delay,string"`
|
||||||
Queue []*QueueData `gorm:"-" json:"queue"`
|
Queue []*QueueData `gorm:"-" json:"-"`
|
||||||
Running chan bool `gorm:"-" json:"-"`
|
Running chan bool `gorm:"-" json:"-"`
|
||||||
Online bool `gorm:"-" json:"online"`
|
Online bool `gorm:"-" json:"online"`
|
||||||
testable bool `gorm:"-" json:"testable"`
|
testable bool `gorm:"-" json:"testable"`
|
||||||
|
|
|
@ -350,11 +350,11 @@ func insertFailureRecords(since time.Time, amount int64) {
|
||||||
for fi := int64(1); fi <= amount; fi++ {
|
for fi := int64(1); fi <= amount; fi++ {
|
||||||
createdAt = createdAt.Add(2 * time.Minute)
|
createdAt = createdAt.Add(2 * time.Minute)
|
||||||
|
|
||||||
failure := &types.Failure{
|
failure := &failure{&types.Failure{
|
||||||
Service: service.Id,
|
Service: service.Id,
|
||||||
Issue: "testing right here",
|
Issue: "testing right here",
|
||||||
CreatedAt: createdAt,
|
CreatedAt: createdAt,
|
||||||
}
|
}}
|
||||||
|
|
||||||
service.CreateFailure(failure)
|
service.CreateFailure(failure)
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,7 +92,11 @@ func (c *Core) SelectAllServices(start bool) ([]*Service, error) {
|
||||||
service.Start()
|
service.Start()
|
||||||
service.CheckinProcess()
|
service.CheckinProcess()
|
||||||
}
|
}
|
||||||
service.AllFailures()
|
failures := service.LimitedFailures(100)
|
||||||
|
service.Failures = nil
|
||||||
|
for _, fail := range failures {
|
||||||
|
service.Failures = append(service.Failures, fail)
|
||||||
|
}
|
||||||
CoreApp.Services = append(CoreApp.Services, service)
|
CoreApp.Services = append(CoreApp.Services, service)
|
||||||
}
|
}
|
||||||
sort.Sort(ServiceOrder(CoreApp.Services))
|
sort.Sort(ServiceOrder(CoreApp.Services))
|
||||||
|
@ -164,7 +168,7 @@ type DateScanObj struct {
|
||||||
|
|
||||||
// lastFailure returns the last failure a service had
|
// lastFailure returns the last failure a service had
|
||||||
func (s *Service) lastFailure() *failure {
|
func (s *Service) lastFailure() *failure {
|
||||||
limited := s.LimitedFailures()
|
limited := s.LimitedFailures(1)
|
||||||
if len(limited) == 0 {
|
if len(limited) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -176,7 +180,7 @@ func (s *Service) lastFailure() *failure {
|
||||||
// service.SmallText()
|
// service.SmallText()
|
||||||
// // Online since Monday 3:04:05PM, Jan _2 2006
|
// // Online since Monday 3:04:05PM, Jan _2 2006
|
||||||
func (s *Service) SmallText() string {
|
func (s *Service) SmallText() string {
|
||||||
last := s.LimitedFailures()
|
last := s.LimitedFailures(1)
|
||||||
hits, _ := s.LimitedHits()
|
hits, _ := s.LimitedHits()
|
||||||
zone := CoreApp.Timezone
|
zone := CoreApp.Timezone
|
||||||
if s.Online {
|
if s.Online {
|
||||||
|
|
|
@ -256,10 +256,10 @@ func TestServiceFailedTCPCheck(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateServiceFailure(t *testing.T) {
|
func TestCreateServiceFailure(t *testing.T) {
|
||||||
fail := &types.Failure{
|
fail := &failure{&types.Failure{
|
||||||
Issue: "This is not an issue, but it would container HTTP response errors.",
|
Issue: "This is not an issue, but it would container HTTP response errors.",
|
||||||
Method: "http",
|
Method: "http",
|
||||||
}
|
}}
|
||||||
service := SelectService(8)
|
service := SelectService(8)
|
||||||
id, err := service.CreateFailure(fail)
|
id, err := service.CreateFailure(fail)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
|
@ -37,7 +37,7 @@ type apiResponse struct {
|
||||||
|
|
||||||
func apiIndexHandler(w http.ResponseWriter, r *http.Request) {
|
func apiIndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var out core.Core
|
var out core.Core
|
||||||
|
@ -55,7 +55,7 @@ func apiIndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiRenewHandler(w http.ResponseWriter, r *http.Request) {
|
func apiRenewHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
|
@ -70,7 +70,7 @@ func apiRenewHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiCheckinHandler(w http.ResponseWriter, r *http.Request) {
|
func apiCheckinHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -121,7 +121,7 @@ func apiServicePingDataHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiServiceHandler(w http.ResponseWriter, r *http.Request) {
|
func apiServiceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -137,7 +137,7 @@ func apiServiceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiCreateServiceHandler(w http.ResponseWriter, r *http.Request) {
|
func apiCreateServiceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var service *types.Service
|
var service *types.Service
|
||||||
|
@ -159,7 +159,7 @@ func apiCreateServiceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -185,7 +185,7 @@ func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -211,14 +211,13 @@ func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiAllServicesHandler(w http.ResponseWriter, r *http.Request) {
|
func apiAllServicesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
allServices := core.CoreApp.Services
|
allServices := core.CoreApp.Services
|
||||||
var services []types.ServiceInterface
|
var services []types.ServiceInterface
|
||||||
for _, s := range allServices {
|
for _, s := range allServices {
|
||||||
service := s.Select()
|
service := s.Select()
|
||||||
service.Failures = nil
|
|
||||||
services = append(services, core.ReturnService(service))
|
services = append(services, core.ReturnService(service))
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
@ -227,7 +226,7 @@ func apiAllServicesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiUserHandler(w http.ResponseWriter, r *http.Request) {
|
func apiUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -242,7 +241,7 @@ func apiUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiUserUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
func apiUserUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -267,7 +266,7 @@ func apiUserUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiUserDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
func apiUserDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -293,7 +292,7 @@ func apiUserDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiAllUsersHandler(w http.ResponseWriter, r *http.Request) {
|
func apiAllUsersHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
users, err := core.SelectAllUsers()
|
users, err := core.SelectAllUsers()
|
||||||
|
@ -306,7 +305,7 @@ func apiAllUsersHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiCreateUsersHandler(w http.ResponseWriter, r *http.Request) {
|
func apiCreateUsersHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var user *types.User
|
var user *types.User
|
||||||
|
@ -334,7 +333,7 @@ func apiCreateUsersHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) {
|
func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -349,7 +348,7 @@ func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -386,7 +385,7 @@ func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiAllMessagesHandler(w http.ResponseWriter, r *http.Request) {
|
func apiAllMessagesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
messages, err := core.SelectMessages()
|
messages, err := core.SelectMessages()
|
||||||
|
@ -400,7 +399,7 @@ func apiAllMessagesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiMessageGetHandler(w http.ResponseWriter, r *http.Request) {
|
func apiMessageGetHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -415,7 +414,7 @@ func apiMessageGetHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiMessageDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
func apiMessageDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -443,7 +442,7 @@ func apiMessageDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func apiMessageUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
func apiMessageUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !isAPIAuthorized(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -480,8 +479,8 @@ func apiMessageUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func apiNotifiersHandler(w http.ResponseWriter, r *http.Request) {
|
func apiNotifiersHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !IsAuthenticated(r) {
|
if !isAPIAuthorized(r) {
|
||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
sendUnauthorizedJson(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var notifiers []*notifier.Notification
|
var notifiers []*notifier.Notification
|
||||||
|
@ -493,6 +492,37 @@ func apiNotifiersHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
json.NewEncoder(w).Encode(notifiers)
|
json.NewEncoder(w).Encode(notifiers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func apiAllServiceFailuresHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !isAPIAuthorized(r) {
|
||||||
|
sendUnauthorizedJson(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
allServices, _ := core.CoreApp.SelectAllServices(false)
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(allServices)
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiServiceFailuresHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !isAPIAuthorized(r) {
|
||||||
|
sendUnauthorizedJson(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
service := core.SelectService(utils.StringInt(vars["id"]))
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(service.AllFailures())
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendUnauthorizedJson(w http.ResponseWriter, r *http.Request) {
|
||||||
|
data := map[string]interface{}{
|
||||||
|
"error": "unauthorized",
|
||||||
|
"url": r.RequestURI,
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
json.NewEncoder(w).Encode(data)
|
||||||
|
}
|
||||||
|
|
||||||
func isAPIAuthorized(r *http.Request) bool {
|
func isAPIAuthorized(r *http.Request) bool {
|
||||||
if os.Getenv("GO_ENV") == "test" {
|
if os.Getenv("GO_ENV") == "test" {
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -87,6 +87,12 @@ func cached(duration, contentType string, handler func(w http.ResponseWriter, r
|
||||||
c := httptest.NewRecorder()
|
c := httptest.NewRecorder()
|
||||||
handler(c, r)
|
handler(c, r)
|
||||||
content := c.Body.Bytes()
|
content := c.Body.Bytes()
|
||||||
|
result := c.Result()
|
||||||
|
if result.StatusCode != 200 {
|
||||||
|
w.WriteHeader(result.StatusCode)
|
||||||
|
w.Write(content)
|
||||||
|
return
|
||||||
|
}
|
||||||
if d, err := time.ParseDuration(duration); err == nil {
|
if d, err := time.ParseDuration(duration); err == nil {
|
||||||
CacheStorage.Set(r.RequestURI, content, d)
|
CacheStorage.Set(r.RequestURI, content, d)
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,6 +112,7 @@ func Router() *mux.Router {
|
||||||
r.Handle("/api/services/{id}/ping", http.HandlerFunc(apiServicePingDataHandler)).Methods("GET")
|
r.Handle("/api/services/{id}/ping", http.HandlerFunc(apiServicePingDataHandler)).Methods("GET")
|
||||||
r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceUpdateHandler)).Methods("POST")
|
r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceUpdateHandler)).Methods("POST")
|
||||||
r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceDeleteHandler)).Methods("DELETE")
|
r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceDeleteHandler)).Methods("DELETE")
|
||||||
|
r.Handle("/api/service/{id}/failures", http.HandlerFunc(apiServiceFailuresHandler)).Methods("GET")
|
||||||
r.Handle("/api/checkin/{api}", http.HandlerFunc(apiCheckinHandler))
|
r.Handle("/api/checkin/{api}", http.HandlerFunc(apiCheckinHandler))
|
||||||
|
|
||||||
// API USER Routes
|
// API USER Routes
|
||||||
|
|
|
@ -33,7 +33,6 @@ func renderServiceChartsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/javascript")
|
w.Header().Set("Content-Type", "text/javascript")
|
||||||
w.Header().Set("Cache-Control", "max-age=60")
|
w.Header().Set("Cache-Control", "max-age=60")
|
||||||
|
|
||||||
//var data []string
|
|
||||||
end := time.Now().UTC()
|
end := time.Now().UTC()
|
||||||
start := time.Now().Add((-24 * 7) * time.Hour).UTC()
|
start := time.Now().Add((-24 * 7) * time.Hour).UTC()
|
||||||
var srvs []*core.Service
|
var srvs []*core.Service
|
||||||
|
|
|
@ -33,10 +33,11 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{ range Services }}
|
{{ range Services }}
|
||||||
{{ if .LimitedFailures }}
|
{{$failures := .LimitedFailures 16}}
|
||||||
|
{{ if $failures }}
|
||||||
<h4 class="text-truncate">{{.Name}} Failures</h4>
|
<h4 class="text-truncate">{{.Name}} Failures</h4>
|
||||||
<div class="list-group mt-3 mb-4">
|
<div class="list-group mt-3 mb-4">
|
||||||
{{ range .LimitedFailures }}
|
{{ range $failures }}
|
||||||
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
|
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
|
||||||
<div class="d-flex w-100 justify-content-between">
|
<div class="d-flex w-100 justify-content-between">
|
||||||
<h5 class="mb-1">{{.ParseError}}</h5>
|
<h5 class="mb-1">{{.ParseError}}</h5>
|
||||||
|
|
|
@ -75,17 +75,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{if .ActiveMessages}}
|
|
||||||
<div class="col-12 mb-4">
|
|
||||||
{{range .ActiveMessages}}
|
|
||||||
<div class="alert alert-warning" role="alert">
|
|
||||||
<h3>{{.Title}}</h3>
|
|
||||||
<span>{{safe .Description}}</span>
|
|
||||||
</div>
|
|
||||||
{{end}}
|
|
||||||
</div>
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
|
@ -70,9 +70,10 @@
|
||||||
<div class="col-12 small text-center mt-3 text-muted">{{$s.DowntimeText}}</div>
|
<div class="col-12 small text-center mt-3 text-muted">{{$s.DowntimeText}}</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{ if $s.LimitedFailures }}
|
{{$failures := $s.LimitedFailures 16}}
|
||||||
|
{{ if $failures }}
|
||||||
<div class="list-group mt-3 mb-4">
|
<div class="list-group mt-3 mb-4">
|
||||||
{{ range $s.LimitedFailures }}
|
{{ range $failures }}
|
||||||
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
|
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
|
||||||
<div class="d-flex w-100 justify-content-between">
|
<div class="d-flex w-100 justify-content-between">
|
||||||
<h5 class="mb-1">{{.ParseError}}</h5>
|
<h5 class="mb-1">{{.ParseError}}</h5>
|
||||||
|
|
|
@ -27,12 +27,12 @@ type Failure struct {
|
||||||
Method string `gorm:"column:method" json:"method,omitempty"`
|
Method string `gorm:"column:method" json:"method,omitempty"`
|
||||||
MethodId int64 `gorm:"column:method_id" json:"method_id,omitempty"`
|
MethodId int64 `gorm:"column:method_id" json:"method_id,omitempty"`
|
||||||
Service int64 `gorm:"index;column:service" json:"-"`
|
Service int64 `gorm:"index;column:service" json:"-"`
|
||||||
PingTime float64 `gorm:"column:ping_time"`
|
PingTime float64 `gorm:"column:ping_time" json:"ping"`
|
||||||
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
|
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
|
||||||
FailureInterface `gorm:"-" json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type FailureInterface interface {
|
type FailureInterface interface {
|
||||||
|
Select() *Failure
|
||||||
Ago() string // Ago returns a human readable timestamp
|
Ago() string // Ago returns a human readable timestamp
|
||||||
ParseError() string // ParseError returns a human readable error for a service failure
|
ParseError() string // ParseError returns a human readable error for a service failure
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ type Service struct {
|
||||||
LastResponse string `gorm:"-" json:"-"`
|
LastResponse string `gorm:"-" json:"-"`
|
||||||
LastStatusCode int `gorm:"-" json:"status_code"`
|
LastStatusCode int `gorm:"-" json:"status_code"`
|
||||||
LastOnline time.Time `gorm:"-" json:"last_online"`
|
LastOnline time.Time `gorm:"-" json:"last_online"`
|
||||||
Failures []interface{} `gorm:"-" json:"failures,omitempty"`
|
Failures []FailureInterface `gorm:"-" json:"failures,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServiceInterface interface {
|
type ServiceInterface interface {
|
||||||
|
|
Loading…
Reference in New Issue