2023-07-09 02:37:41 +02:00
|
|
|
package api
|
2021-09-13 00:39:09 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-04-02 03:47:14 +02:00
|
|
|
"errors"
|
2021-09-13 00:39:09 +02:00
|
|
|
"fmt"
|
|
|
|
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/client"
|
|
|
|
"github.com/TwiN/gatus/v5/config"
|
2024-05-10 04:56:16 +02:00
|
|
|
"github.com/TwiN/gatus/v5/config/endpoint"
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/config/remote"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store/common"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store/common/paging"
|
2024-11-14 05:54:00 +01:00
|
|
|
"github.com/TwiN/logr"
|
2023-07-09 02:37:41 +02:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-09-13 00:39:09 +02:00
|
|
|
)
|
|
|
|
|
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.
|
2023-07-09 02:37:41 +02:00
|
|
|
func EndpointStatuses(cfg *config.Config) fiber.Handler {
|
|
|
|
return func(c *fiber.Ctx) error {
|
|
|
|
page, pageSize := extractPageAndPageSizeFromRequest(c)
|
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 {
|
|
|
|
endpointStatuses, err := store.Get().GetAllEndpointStatuses(paging.NewEndpointStatusParams().WithResults(page, pageSize))
|
|
|
|
if err != nil {
|
2024-11-14 05:54:00 +01:00
|
|
|
logr.Errorf("[api.EndpointStatuses] Failed to retrieve endpoint statuses: %s", err.Error())
|
2023-07-09 02:37:41 +02:00
|
|
|
return c.Status(500).SendString(err.Error())
|
2022-07-29 02:07:53 +02:00
|
|
|
}
|
|
|
|
// ALPHA: Retrieve endpoint statuses from remote instances
|
|
|
|
if endpointStatusesFromRemote, err := getEndpointStatusesFromRemoteInstances(cfg.Remote); err != nil {
|
2024-11-14 05:54:00 +01:00
|
|
|
logr.Errorf("[handler.EndpointStatuses] Silently failed to retrieve endpoint statuses from remote: %s", err.Error())
|
2022-07-29 02:07:53 +02:00
|
|
|
} else if endpointStatusesFromRemote != nil {
|
|
|
|
endpointStatuses = append(endpointStatuses, endpointStatusesFromRemote...)
|
|
|
|
}
|
|
|
|
// Marshal endpoint statuses to JSON
|
|
|
|
data, err = json.Marshal(endpointStatuses)
|
|
|
|
if err != nil {
|
2024-11-14 05:54:00 +01:00
|
|
|
logr.Errorf("[api.EndpointStatuses] Unable to marshal object to JSON: %s", err.Error())
|
2023-07-09 02:37:41 +02:00
|
|
|
return c.Status(500).SendString("unable to marshal object to JSON")
|
2022-07-29 02:07:53 +02:00
|
|
|
}
|
|
|
|
cache.SetWithTTL(fmt.Sprintf("endpoint-status-%d-%d", page, pageSize), data, cacheTTL)
|
|
|
|
} else {
|
|
|
|
data = value.([]byte)
|
|
|
|
}
|
2023-07-09 02:37:41 +02:00
|
|
|
c.Set("Content-Type", "application/json")
|
|
|
|
return c.Status(200).Send(data)
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2022-07-29 02:07:53 +02:00
|
|
|
}
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
func getEndpointStatusesFromRemoteInstances(remoteConfig *remote.Config) ([]*endpoint.Status, error) {
|
2022-07-29 02:07:53 +02:00
|
|
|
if remoteConfig == nil || len(remoteConfig.Instances) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
var endpointStatusesFromAllRemotes []*endpoint.Status
|
2022-07-29 02:07:53 +02:00
|
|
|
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
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
var endpointStatuses []*endpoint.Status
|
2024-08-27 03:30:53 +02:00
|
|
|
if err = json.NewDecoder(response.Body).Decode(&endpointStatuses); err != nil {
|
2022-07-29 02:07:53 +02:00
|
|
|
_ = response.Body.Close()
|
2024-11-14 05:54:00 +01:00
|
|
|
logr.Errorf("[api.getEndpointStatusesFromRemoteInstances] Silently failed to retrieve endpoint statuses from %s: %s", instance.URL, err.Error())
|
2022-07-29 02:07:53 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
// EndpointStatus retrieves a single endpoint.Status by group and endpoint name
|
2023-07-09 02:37:41 +02:00
|
|
|
func EndpointStatus(c *fiber.Ctx) error {
|
|
|
|
page, pageSize := extractPageAndPageSizeFromRequest(c)
|
|
|
|
endpointStatus, err := store.Get().GetEndpointStatusByKey(c.Params("key"), paging.NewEndpointStatusParams().WithResults(page, pageSize).WithEvents(1, common.MaximumNumberOfEvents))
|
2021-09-13 00:39:09 +02:00
|
|
|
if err != nil {
|
2024-04-02 03:47:14 +02:00
|
|
|
if errors.Is(err, common.ErrEndpointNotFound) {
|
2023-07-09 02:37:41 +02:00
|
|
|
return c.Status(404).SendString(err.Error())
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2024-11-14 05:54:00 +01:00
|
|
|
logr.Errorf("[api.EndpointStatus] Failed to retrieve endpoint status: %s", err.Error())
|
2023-07-09 02:37:41 +02:00
|
|
|
return c.Status(500).SendString(err.Error())
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2023-07-09 02:37:41 +02:00
|
|
|
if endpointStatus == nil { // XXX: is this check necessary?
|
2024-11-14 05:54:00 +01:00
|
|
|
logr.Errorf("[api.EndpointStatus] Endpoint with key=%s not found", c.Params("key"))
|
2023-07-09 02:37:41 +02:00
|
|
|
return c.Status(404).SendString("not found")
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
output, err := json.Marshal(endpointStatus)
|
2021-09-13 00:39:09 +02:00
|
|
|
if err != nil {
|
2024-11-14 05:54:00 +01:00
|
|
|
logr.Errorf("[api.EndpointStatus] Unable to marshal object to JSON: %s", err.Error())
|
2023-07-09 02:37:41 +02:00
|
|
|
return c.Status(500).SendString("unable to marshal object to JSON")
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|
2023-07-09 02:37:41 +02:00
|
|
|
c.Set("Content-Type", "application/json")
|
|
|
|
return c.Status(200).Send(output)
|
2021-09-13 00:39:09 +02:00
|
|
|
}
|