This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from keskad/main
Add health check endpoint
- Loading branch information
Showing
19 changed files
with
230 additions
and
31 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
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
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 @@ | ||
Administrative API endpoints | ||
============================ | ||
|
||
## GET `/health`` | ||
|
||
**Example:** | ||
|
||
```bash | ||
curl -s -X GET 'http://localhost:8080/health' | ||
``` | ||
|
||
**Example response (200):** | ||
|
||
```json | ||
{ | ||
"data": { | ||
"health": [ | ||
{ | ||
"message": "OK", | ||
"name": "DbValidator", | ||
"status": true, | ||
"statusText": "DbValidator=true" | ||
}, | ||
{ | ||
"message": "OK", | ||
"name": "StorageAvailabilityValidator", | ||
"status": true, | ||
"statusText": "StorageAvailabilityValidator=true" | ||
} | ||
] | ||
}, | ||
"status": true | ||
} | ||
``` | ||
|
||
**Other responses:** | ||
- [500](../common-responses.md) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package health | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/riotkit-org/backup-repository/config" | ||
) | ||
|
||
type ConfigurationProviderValidator struct { | ||
cfg config.ConfigurationProvider | ||
} | ||
|
||
func (v ConfigurationProviderValidator) Validate() error { | ||
if err := v.cfg.GetHealth(); err != nil { | ||
return errors.Wrapf(err, "configuration provider is not usable") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewConfigurationProviderValidator(cfg config.ConfigurationProvider) ConfigurationProviderValidator { | ||
return ConfigurationProviderValidator{cfg} | ||
} |
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,22 @@ | ||
package health | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type DbValidator struct { | ||
db *gorm.DB | ||
} | ||
|
||
func (v DbValidator) Validate() error { | ||
err := v.db.Raw("SELECT 1").Error | ||
if err != nil { | ||
return errors.Wrapf(err, "cannot connect to database") | ||
} | ||
return nil | ||
} | ||
|
||
func NewDbValidator(db *gorm.DB) DbValidator { | ||
return DbValidator{db} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package health | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/riotkit-org/backup-repository/storage" | ||
) | ||
|
||
type StorageAvailabilityValidator struct { | ||
storage *storage.Service | ||
} | ||
|
||
func (v StorageAvailabilityValidator) Validate() error { | ||
err := v.storage.TestReadWrite() | ||
if err != nil { | ||
return errors.Wrapf(err, "storage not operable") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewStorageValidator(storage *storage.Service) StorageAvailabilityValidator { | ||
return StorageAvailabilityValidator{storage} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package http | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/pkg/errors" | ||
"github.com/riotkit-org/backup-repository/core" | ||
"github.com/riotkit-org/backup-repository/health" | ||
) | ||
|
||
func addServerHealthEndpoint(r *gin.Engine, ctx *core.ApplicationContainer, rateLimiter gin.HandlerFunc) { | ||
r.GET("/health", rateLimiter, func(c *gin.Context) { | ||
// Authorization | ||
healthCode := c.GetHeader("Authorization") | ||
if healthCode == "" { | ||
healthCode = c.Query("code") | ||
} | ||
if healthCode != ctx.HealthCheckKey { | ||
UnauthorizedResponse(c, errors.New("health code invalid. Should be provided withing 'Authorization' header or 'code' query string. Must match --health-check-code commandline switch value")) | ||
return | ||
} | ||
|
||
healthStatuses := health.Validators{ | ||
health.NewDbValidator(ctx.Db), | ||
health.NewStorageValidator(ctx.Storage), | ||
health.NewConfigurationProviderValidator(*ctx.Config), | ||
}.Validate() | ||
|
||
if !healthStatuses.GetOverallStatus() { | ||
ServerErrorResponseWithData(c, errors.New("one of checks failed"), gin.H{ | ||
"health": healthStatuses, | ||
}) | ||
return | ||
} | ||
|
||
OKResponse(c, gin.H{ | ||
"health": healthStatuses, | ||
}) | ||
}) | ||
} |
Oops, something went wrong.