diff --git a/webapp/backend/pkg/database/interface.go b/webapp/backend/pkg/database/interface.go index f140c26..f5dae30 100644 --- a/webapp/backend/pkg/database/interface.go +++ b/webapp/backend/pkg/database/interface.go @@ -10,6 +10,7 @@ import ( type DeviceRepo interface { Close() error + HealthCheck(ctx context.Context) error RegisterDevice(ctx context.Context, dev models.Device) error GetDevices(ctx context.Context) ([]models.Device, error) diff --git a/webapp/backend/pkg/database/scrutiny_repository.go b/webapp/backend/pkg/database/scrutiny_repository.go index 521ba7d..b163c2c 100644 --- a/webapp/backend/pkg/database/scrutiny_repository.go +++ b/webapp/backend/pkg/database/scrutiny_repository.go @@ -195,6 +195,29 @@ func (sr *scrutinyRepository) Close() error { 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) { influxUri, err := url.Parse(influxEndpoint) if err != nil { diff --git a/webapp/backend/pkg/web/handler/health_check.go b/webapp/backend/pkg/web/handler/health_check.go new file mode 100644 index 0000000..a99bf6f --- /dev/null +++ b/webapp/backend/pkg/web/handler/health_check.go @@ -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, + }) +} diff --git a/webapp/backend/pkg/web/server.go b/webapp/backend/pkg/web/server.go index bb82405..3d56fe0 100644 --- a/webapp/backend/pkg/web/server.go +++ b/webapp/backend/pkg/web/server.go @@ -34,15 +34,7 @@ func (ae *AppEngine) Setup(logger *logrus.Entry) *gin.Engine { { api := base.Group("/api") { - api.GET("/health", func(c *gin.Context) { - //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.GET("/health", handler.HealthCheck) 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