adding a healthcheck endpoint that confirms communication with influxdb and sqlite.
ref: #381
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
type DeviceRepo interface {
|
type DeviceRepo interface {
|
||||||
Close() error
|
Close() error
|
||||||
|
HealthCheck(ctx context.Context) error
|
||||||
|
|
||||||
RegisterDevice(ctx context.Context, dev models.Device) error
|
RegisterDevice(ctx context.Context, dev models.Device) error
|
||||||
GetDevices(ctx context.Context) ([]models.Device, error)
|
GetDevices(ctx context.Context) ([]models.Device, error)
|
||||||
|
|||||||
@@ -195,6 +195,29 @@ func (sr *scrutinyRepository) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sr *scrutinyRepository) HealthCheck(ctx context.Context) error {
|
||||||
|
//check influxdb
|
||||||
|
status, err := sr.influxClient.Health(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("influxdb healthcheck failed: %w", err)
|
||||||
|
}
|
||||||
|
if status.Status != "pass" {
|
||||||
|
return fmt.Errorf("influxdb healthcheckf failed: status=%s", status.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
//check sqlite db.
|
||||||
|
database, err := sr.gormClient.DB()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("sqlite healthcheck failed: %w", err)
|
||||||
|
}
|
||||||
|
err = database.Ping()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("sqlite healthcheck failed during ping: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func InfluxSetupComplete(influxEndpoint string) (bool, error) {
|
func InfluxSetupComplete(influxEndpoint string) (bool, error) {
|
||||||
influxUri, err := url.Parse(influxEndpoint)
|
influxUri, err := url.Parse(influxEndpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/analogj/scrutiny/webapp/backend/pkg/database"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HealthCheck(c *gin.Context) {
|
||||||
|
logger := c.MustGet("LOGGER").(*logrus.Entry)
|
||||||
|
deviceRepo := c.MustGet("DEVICE_REPOSITORY").(database.DeviceRepo)
|
||||||
|
logger.Infof("Checking Influxdb & Sqlite health")
|
||||||
|
|
||||||
|
//check sqlite and influxdb health
|
||||||
|
err := deviceRepo.HealthCheck(c)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorln("An error occurred during healthcheck", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO:
|
||||||
|
// check if the /web folder is populated.
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": true,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -34,15 +34,7 @@ func (ae *AppEngine) Setup(logger *logrus.Entry) *gin.Engine {
|
|||||||
{
|
{
|
||||||
api := base.Group("/api")
|
api := base.Group("/api")
|
||||||
{
|
{
|
||||||
api.GET("/health", func(c *gin.Context) {
|
api.GET("/health", handler.HealthCheck)
|
||||||
//TODO:
|
|
||||||
// check if the /web folder is populated.
|
|
||||||
// check if access to influxdb
|
|
||||||
// check if access to sqlitedb.
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"success": true,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
api.POST("/health/notify", handler.SendTestNotification) //check if notifications are configured correctly
|
api.POST("/health/notify", handler.SendTestNotification) //check if notifications are configured correctly
|
||||||
|
|
||||||
api.POST("/devices/register", handler.RegisterDevices) //used by Collector to register new devices and retrieve filtered list
|
api.POST("/devices/register", handler.RegisterDevices) //used by Collector to register new devices and retrieve filtered list
|
||||||
|
|||||||
Reference in New Issue
Block a user