-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
4 changed files
with
110 additions
and
2 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,21 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/tarkov-database/rest-api/model/api" | ||
"github.com/tarkov-database/rest-api/view" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
) | ||
|
||
// HealthGET handles a GET request on the health endpoint | ||
func HealthGET(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) { | ||
h, err := api.GetHealth() | ||
|
||
if err != nil || !h.OK { | ||
view.RenderJSON(h, http.StatusInternalServerError, w) | ||
} else { | ||
view.RenderJSON(h, http.StatusOK, w) | ||
} | ||
} |
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,74 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/tarkov-database/rest-api/core/database" | ||
) | ||
|
||
// Status represents the status code of a service | ||
type Status int | ||
|
||
const ( | ||
// OK status if all checks were successful | ||
OK Status = iota | ||
|
||
// Warning status if non-critical issues are discovered | ||
Warning | ||
|
||
// Failure status when critical problems are discovered | ||
Failure | ||
) | ||
|
||
// Health represents the object of the health root endpoint | ||
type Health struct { | ||
OK bool `json:"ok"` | ||
Service *Service `json:"service"` | ||
} | ||
|
||
// Service holds all services with their respective status | ||
type Service struct { | ||
Database Status `json:"database"` | ||
} | ||
|
||
// GetHealth performs a self-check and returns the result | ||
func GetHealth() (*Health, error) { | ||
var err error | ||
var ok = true | ||
|
||
svc := &Service{} | ||
|
||
svc.Database, err = getDatabaseStatus() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if svc.Database != OK { | ||
ok = false | ||
} | ||
|
||
health := &Health{ | ||
OK: ok, | ||
Service: svc, | ||
} | ||
|
||
return health, nil | ||
} | ||
|
||
func getDatabaseStatus() (Status, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
|
||
start := time.Now() | ||
|
||
if err := database.Ping(ctx); err != nil { | ||
return Failure, err | ||
} | ||
|
||
if time.Since(start).Seconds() > 3 { | ||
return Warning, nil | ||
} | ||
|
||
return OK, 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