-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a7118f
commit 428b540
Showing
9 changed files
with
290 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"github.com/patrickmn/go-cache" | ||
"go.uber.org/zap" | ||
"time" | ||
) | ||
|
||
func (s *Server) StartCaches(ctx context.Context) { | ||
go s.updateStatusPageCache(ctx) | ||
} | ||
|
||
const statusPageCacheRefreshInterval = 5 * time.Minute | ||
|
||
func (s *Server) updateStatusPageCache(ctx context.Context) { | ||
ticker := time.NewTicker(statusPageCacheRefreshInterval) | ||
s.updateStatusPageCacheInner(ctx) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
s.updateStatusPageCacheInner(ctx) | ||
} | ||
} | ||
} | ||
|
||
func (s *Server) updateStatusPageCacheInner(ctx context.Context) { | ||
statusPages, err := s.dbClient.GetAllStatusPages(ctx) | ||
if err != nil { | ||
s.logger.Error("failed to get status pages", zap.Error(err)) | ||
return | ||
} | ||
|
||
for _, statusPage := range statusPages { | ||
s.statusPageCache.Set(statusPage.URL, statusPage, cache.DefaultExpiration) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"github.com/gin-gonic/gin" | ||
"github.com/metoro-io/statusphere/common/api" | ||
"github.com/patrickmn/go-cache" | ||
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
"net/http" | ||
) | ||
|
||
type CurrentStatusResponse struct { | ||
IsOkay bool `json:"isOkay"` | ||
} | ||
|
||
// currentStatus is a handler for the /current-status endpoint. | ||
// It has a required query parameter of statusPageUrl | ||
func (s *Server) currentStatus(context *gin.Context) { | ||
ctx := context.Request.Context() | ||
statusPageUrl := context.Query("statusPageUrl") | ||
if statusPageUrl == "" { | ||
context.JSON(http.StatusBadRequest, gin.H{"error": "statusPageUrl is required"}) | ||
return | ||
} | ||
|
||
// Attempt to get the incidents from the cache | ||
incidents, found, err := s.getCurrentIncidentsFromCache(ctx, statusPageUrl) | ||
if err != nil { | ||
s.logger.Error("failed to get incidents from cache", zap.Error(err)) | ||
context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get incidents from cache"}) | ||
return | ||
} | ||
if found { | ||
if len(incidents) > 0 { | ||
context.JSON(http.StatusOK, CurrentStatusResponse{IsOkay: false}) | ||
return | ||
} | ||
context.JSON(http.StatusOK, CurrentStatusResponse{IsOkay: true}) | ||
return | ||
} | ||
|
||
// Attempt to get the incidents from the database | ||
incidents, found, err = s.getCurrentIncidentsFromDatabase(ctx, statusPageUrl) | ||
if err != nil { | ||
s.logger.Error("failed to get incidents from database", zap.Error(err)) | ||
context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get incidents from database"}) | ||
return | ||
} | ||
if !found { | ||
context.JSON(http.StatusNotFound, gin.H{"error": "status page not indexed"}) | ||
return | ||
} | ||
|
||
s.currentIncidentCache.Set(statusPageUrl, incidents, cache.DefaultExpiration) | ||
if len(incidents) > 0 { | ||
context.JSON(http.StatusOK, CurrentStatusResponse{IsOkay: false}) | ||
return | ||
} | ||
context.JSON(http.StatusOK, CurrentStatusResponse{IsOkay: true}) | ||
} | ||
|
||
// getCurrentIncidentsFromCache attempts to get the current incidents from the cache. | ||
// If the incidents are found in the cache, it returns them. | ||
// If the incidents are not found in the cache, it returns false for the second return value. | ||
|
||
func (s *Server) getCurrentIncidentsFromCache(ctx context.Context, statusPageUrl string) ([]api.Incident, bool, error) { | ||
incidents, found := s.currentIncidentCache.Get(statusPageUrl) | ||
if !found { | ||
return nil, false, nil | ||
} | ||
|
||
incidentsCasted, ok := incidents.([]api.Incident) | ||
if !ok { | ||
return nil, false, errors.New("failed to cast incidents to []api.Incident") | ||
} | ||
|
||
return incidentsCasted, true, nil | ||
} | ||
|
||
// getCurrentIncidentsFromDatabase attempts to get the current incidents from the database. | ||
// If the incidents are found in the database, it returns them. | ||
// If the incidents are not found in the database, it returns false for the second return value. | ||
func (s *Server) getCurrentIncidentsFromDatabase(ctx context.Context, statusPageUrl string) ([]api.Incident, bool, error) { | ||
incidents, err := s.dbClient.GetCurrentIncidents(ctx, statusPageUrl) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
if len(incidents) == 0 { | ||
// See if the status page exists | ||
statusPage, err := s.dbClient.GetStatusPage(ctx, statusPageUrl) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
if statusPage == nil { | ||
// The status page does not exist | ||
return nil, false, nil | ||
} | ||
} | ||
|
||
return incidents, true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"github.com/gin-gonic/gin" | ||
"github.com/metoro-io/statusphere/common/api" | ||
"github.com/patrickmn/go-cache" | ||
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
"net/http" | ||
) | ||
|
||
type IncidentsResponse struct { | ||
Incidents []api.Incident `json:"incidents"` | ||
} | ||
|
||
// incidents is a handler for the /incidents endpoint. | ||
// It has a required query parameter of statusPageUrl | ||
func (s *Server) incidents(context *gin.Context) { | ||
ctx := context.Request.Context() | ||
statusPageUrl := context.Query("statusPageUrl") | ||
if statusPageUrl == "" { | ||
context.JSON(http.StatusBadRequest, gin.H{"error": "statusPageUrl is required"}) | ||
return | ||
} | ||
|
||
// Attempt to get the incidents from the cache | ||
incidents, found, err := s.getIncidentsFromCache(ctx, statusPageUrl) | ||
if err != nil { | ||
s.logger.Error("failed to get incidents from cache", zap.Error(err)) | ||
context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get incidents from cache"}) | ||
return | ||
} | ||
if found { | ||
context.JSON(http.StatusOK, IncidentsResponse{Incidents: incidents}) | ||
return | ||
} | ||
|
||
// Attempt to get the incidents from the database | ||
incidents, found, err = s.getIncidentsFromDatabase(ctx, statusPageUrl) | ||
if err != nil { | ||
s.logger.Error("failed to get incidents from database", zap.Error(err)) | ||
context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get incidents from database"}) | ||
return | ||
} | ||
if !found { | ||
context.JSON(http.StatusNotFound, gin.H{"error": "status page not indexed"}) | ||
return | ||
} | ||
|
||
s.incidentCache.Set(statusPageUrl, incidents, cache.DefaultExpiration) | ||
context.JSON(http.StatusOK, IncidentsResponse{Incidents: incidents}) | ||
} | ||
|
||
// getIncidentsFromCache attempts to get the incidents from the cache. | ||
// If the incidents are found in the cache, it returns them. | ||
// If the incidents are not found in the cache, it returns false for the second return value. | ||
func (s *Server) getIncidentsFromCache(ctx context.Context, statusPageUrl string) ([]api.Incident, bool, error) { | ||
incidents, found := s.incidentCache.Get(statusPageUrl) | ||
if !found { | ||
return nil, false, nil | ||
} | ||
|
||
incidentsCasted, ok := incidents.([]api.Incident) | ||
if !ok { | ||
return nil, false, errors.New("failed to cast incidents to []api.Incident") | ||
} | ||
|
||
return incidentsCasted, true, nil | ||
} | ||
|
||
// getIncidentsFromDatabase attempts to get the incidents from the database. | ||
// If the incidents are found in the database, it returns them. | ||
// If the incidents are not found in the database, it returns false for the second return value. | ||
func (s *Server) getIncidentsFromDatabase(ctx context.Context, statusPageUrl string) ([]api.Incident, bool, error) { | ||
incidents, err := s.dbClient.GetIncidents(ctx, statusPageUrl) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
if len(incidents) == 0 { | ||
// See if the status page exists | ||
statusPage, err := s.dbClient.GetStatusPage(ctx, statusPageUrl) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
if statusPage == nil { | ||
// The status page does not exist | ||
return nil, false, nil | ||
} | ||
} | ||
|
||
return incidents, true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/metoro-io/statusphere/common/api" | ||
"net/http" | ||
) | ||
|
||
type StatusPageResponse struct { | ||
StatusPages []api.StatusPage `json:"statusPages"` | ||
} | ||
|
||
func (s *Server) statusPages(context *gin.Context) { | ||
var statusPages []api.StatusPage | ||
for _, statusPage := range s.statusPageCache.Items() { | ||
statusPages = append(statusPages, statusPage.Object.(api.StatusPage)) | ||
} | ||
|
||
context.JSON(http.StatusOK, StatusPageResponse{StatusPages: statusPages}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters