2021-09-13 00:39:09 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-07-29 02:07:53 +02:00
|
|
|
"io"
|
2021-09-13 00:39:09 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/client"
|
|
|
|
"github.com/TwiN/gatus/v5/config"
|
|
|
|
"github.com/TwiN/gatus/v5/config/remote"
|
|
|
|
"github.com/TwiN/gatus/v5/core"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store/common"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store/common/paging"
|
2022-06-15 05:36:18 +02:00
|
|
|
"github.com/TwiN/gocache/v2"
|
2021-09-13 00:39:09 +02:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
cacheTTL = 10 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cache = gocache.NewCache().WithMaxSize(100).WithEvictionPolicy(gocache.FirstInFirstOut)
|
|
|
|
)
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// EndpointStatuses handles requests to retrieve all EndpointStatus
|
2022-12-31 03:37:52 +01:00
|
|
|
// Due to how intensive this operation can be on the storage, this function leverages a cache.
|
2022-07-29 02:07:53 +02:00
|
|
|
func EndpointStatuses(cfg *config.Config) http.HandlerFunc {
|
|
|
|
return func(writer http.ResponseWriter, r *http.Request) {
|
|
|
|
page, pageSize := extractPageAndPageSizeFromRequest(r)
|
2022-12-31 03:37:52 +01:00
|
|
|
value, exists := cache.Get(fmt.Sprintf("endpoint-status-%d-%d", page, pageSize))
|
2022-07-29 02:07:53 +02:00
|
|
|
var data []byte
|
|
|
|
if !exists {
|
|
|
|
var err error
|
|
|
|
endpointStatuses, err := store.Get().GetAllEndpointStatuses(paging.NewEndpointStatusParams().WithResults(page, pageSize))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[handler][EndpointStatuses] Failed to retrieve endpoint statuses: %s", err.Error())
|
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// ALPHA: Retrieve endpoint statuses from remote instances
|
|
|
|
if endpointStatusesFromRemote, err := getEndpointStatusesFromRemoteInstances(cfg.Remote); err != nil {
|
|
|
|
log.Printf("[handler][EndpointStatuses] Silently failed to retrieve endpoint statuses from remote: %s", err.Error())
|
|
|
|
} else if endpointStatusesFromRemote != nil {
|
|
|
|
endpointStatuses = append(endpointStatuses, endpointStatusesFromRemote...)
|
|
|
|
}
|
|
|
|
// Marshal endpoint statuses to JSON
|
|
|
|
data, err = json.Marshal(endpointStatuses)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[handler][EndpointStatuses] Unable to marshal object to JSON: %s", err.Error())
|
|
|
|
http.Error(writer, "unable to marshal object to JSON", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cache.SetWithTTL(fmt.Sprintf("endpoint-status-%d-%d", page, pageSize), data, cacheTTL)
|
|
|
|
} else {
|
|
|
|
data = value.([]byte)
|
|
|
|
}
|
|
|
|
writer.Header().Add("Content-Type", "application/json")
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = writer.Write(data)
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2022-07-29 02:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getEndpointStatusesFromRemoteInstances(remoteConfig *remote.Config) ([]*core.EndpointStatus, error) {
|
|
|
|
if remoteConfig == nil || len(remoteConfig.Instances) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var endpointStatusesFromAllRemotes []*core.EndpointStatus
|
|
|
|
httpClient := client.GetHTTPClient(remoteConfig.ClientConfig)
|
|
|
|
for _, instance := range remoteConfig.Instances {
|
|
|
|
response, err := httpClient.Get(instance.URL)
|
2021-09-13 00:39:09 +02:00
|
|
|
if err != nil {
|
2022-07-29 02:07:53 +02:00
|
|
|
return nil, err
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2022-07-29 02:07:53 +02:00
|
|
|
body, err := io.ReadAll(response.Body)
|
2021-09-13 00:39:09 +02:00
|
|
|
if err != nil {
|
2022-07-29 02:07:53 +02:00
|
|
|
_ = response.Body.Close()
|
|
|
|
log.Printf("[handler][getEndpointStatusesFromRemoteInstances] Silently failed to retrieve endpoint statuses from %s: %s", instance.URL, err.Error())
|
|
|
|
continue
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2022-07-29 02:07:53 +02:00
|
|
|
var endpointStatuses []*core.EndpointStatus
|
|
|
|
if err = json.Unmarshal(body, &endpointStatuses); err != nil {
|
|
|
|
_ = response.Body.Close()
|
|
|
|
log.Printf("[handler][getEndpointStatusesFromRemoteInstances] Silently failed to retrieve endpoint statuses from %s: %s", instance.URL, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_ = response.Body.Close()
|
|
|
|
for _, endpointStatus := range endpointStatuses {
|
|
|
|
endpointStatus.Name = instance.EndpointPrefix + endpointStatus.Name
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2022-07-29 02:07:53 +02:00
|
|
|
endpointStatusesFromAllRemotes = append(endpointStatusesFromAllRemotes, endpointStatuses...)
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2022-07-29 02:07:53 +02:00
|
|
|
return endpointStatusesFromAllRemotes, nil
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// EndpointStatus retrieves a single core.EndpointStatus by group and endpoint name
|
|
|
|
func EndpointStatus(writer http.ResponseWriter, r *http.Request) {
|
2021-09-13 00:39:09 +02:00
|
|
|
page, pageSize := extractPageAndPageSizeFromRequest(r)
|
|
|
|
vars := mux.Vars(r)
|
2021-10-29 01:35:46 +02:00
|
|
|
endpointStatus, err := store.Get().GetEndpointStatusByKey(vars["key"], paging.NewEndpointStatusParams().WithResults(page, pageSize).WithEvents(1, common.MaximumNumberOfEvents))
|
2021-09-13 00:39:09 +02:00
|
|
|
if err != nil {
|
2021-10-23 22:47:12 +02:00
|
|
|
if err == common.ErrEndpointNotFound {
|
2021-09-13 00:39:09 +02:00
|
|
|
http.Error(writer, err.Error(), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
log.Printf("[handler][EndpointStatus] Failed to retrieve endpoint status: %s", err.Error())
|
2021-09-13 00:39:09 +02:00
|
|
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if endpointStatus == nil {
|
|
|
|
log.Printf("[handler][EndpointStatus] Endpoint with key=%s not found", vars["key"])
|
2021-09-13 00:39:09 +02:00
|
|
|
http.Error(writer, "not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
output, err := json.Marshal(endpointStatus)
|
2021-09-13 00:39:09 +02:00
|
|
|
if err != nil {
|
2021-10-23 22:47:12 +02:00
|
|
|
log.Printf("[handler][EndpointStatus] Unable to marshal object to JSON: %s", err.Error())
|
2021-09-13 00:39:09 +02:00
|
|
|
http.Error(writer, "unable to marshal object to JSON", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writer.Header().Add("Content-Type", "application/json")
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = writer.Write(output)
|
|
|
|
}
|