diff --git a/Dockerfile b/Dockerfile index a448fd9..acc0ba1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,8 @@ RUN set -ex \ FROM alpine +ENV PUSHBITS_HTTP_PORT="8080" + EXPOSE 8080 WORKDIR /app @@ -31,4 +33,6 @@ RUN set -ex \ USER 1000 +HEALTHCHECK --interval=30s --timeout=5s --start-period=5s CMD curl --fail http://localhost:$PUSHBITS_HTTP_PORT/health || exit 1 + ENTRYPOINT ["./run"] diff --git a/api/health.go b/api/health.go new file mode 100644 index 0000000..7c43159 --- /dev/null +++ b/api/health.go @@ -0,0 +1,22 @@ +package api + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +// HealthHandler holds information for processing requests about the server's health. +type HealthHandler struct { + DB Database +} + +// Health returns the health status of the server. +func (h *HealthHandler) Health(ctx *gin.Context) { + if err := h.DB.Health(); err != nil { + ctx.AbortWithError(http.StatusInternalServerError, err) + return + } + + ctx.JSON(http.StatusOK, gin.H{}) +} diff --git a/api/interfaces.go b/api/interfaces.go index 99a1dad..cd60938 100644 --- a/api/interfaces.go +++ b/api/interfaces.go @@ -6,6 +6,8 @@ import ( // The Database interface for encapsulating database access. type Database interface { + Health() error + CreateApplication(application *model.Application) error DeleteApplication(application *model.Application) error GetApplicationByID(ID uint) (*model.Application, error) diff --git a/database/health.go b/database/health.go new file mode 100644 index 0000000..acf94e6 --- /dev/null +++ b/database/health.go @@ -0,0 +1,6 @@ +package database + +// Health reports the status of the database connection. +func (d *Database) Health() error { + return d.sqldb.Ping() +} diff --git a/router/router.go b/router/router.go index abfe7c3..699e766 100644 --- a/router/router.go +++ b/router/router.go @@ -24,6 +24,7 @@ func Create(debug bool, cm *credentials.Manager, db *database.Database, dp *disp auth := authentication.Authenticator{DB: db} applicationHandler := api.ApplicationHandler{DB: db, DP: dp} + healthHandler := api.HealthHandler{DB: db} notificationHandler := api.NotificationHandler{DB: db, DP: dp} userHandler := api.UserHandler{AH: &applicationHandler, CM: cm, DB: db, DP: dp} @@ -42,6 +43,8 @@ func Create(debug bool, cm *credentials.Manager, db *database.Database, dp *disp applicationGroup.PUT("/:id", api.RequireIDInURI(), applicationHandler.UpdateApplication) } + r.GET("/health", healthHandler.Health) + r.POST("/message", auth.RequireApplicationToken(), notificationHandler.CreateNotification) userGroup := r.Group("/user")