diff --git a/cmd/laas/docs/docs.go b/cmd/laas/docs/docs.go index 84b6d85..b9c6ce9 100644 --- a/cmd/laas/docs/docs.go +++ b/cmd/laas/docs/docs.go @@ -215,6 +215,36 @@ const docTemplate = `{ } } }, + "/health": { + "get": { + "description": "Check health of the service", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Health" + ], + "summary": "Check health", + "operationId": "getHealth", + "responses": { + "200": { + "description": "Heath is OK", + "schema": { + "$ref": "#/definitions/models.LicenseError" + } + }, + "500": { + "description": "Connection to DB failed", + "schema": { + "$ref": "#/definitions/models.LicenseError" + } + } + } + } + }, "/licenses": { "get": { "description": "Filter licenses based on different parameters", diff --git a/cmd/laas/docs/swagger.json b/cmd/laas/docs/swagger.json index dc833ab..6ee8850 100644 --- a/cmd/laas/docs/swagger.json +++ b/cmd/laas/docs/swagger.json @@ -209,6 +209,36 @@ } } }, + "/health": { + "get": { + "description": "Check health of the service", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Health" + ], + "summary": "Check health", + "operationId": "getHealth", + "responses": { + "200": { + "description": "Heath is OK", + "schema": { + "$ref": "#/definitions/models.LicenseError" + } + }, + "500": { + "description": "Connection to DB failed", + "schema": { + "$ref": "#/definitions/models.LicenseError" + } + } + } + } + }, "/licenses": { "get": { "description": "Filter licenses based on different parameters", diff --git a/cmd/laas/docs/swagger.yaml b/cmd/laas/docs/swagger.yaml index 37dbdf5..fc91e8d 100644 --- a/cmd/laas/docs/swagger.yaml +++ b/cmd/laas/docs/swagger.yaml @@ -641,6 +641,26 @@ paths: summary: Get a changelog tags: - Audits + /health: + get: + consumes: + - application/json + description: Check health of the service + operationId: getHealth + produces: + - application/json + responses: + "200": + description: Heath is OK + schema: + $ref: '#/definitions/models.LicenseError' + "500": + description: Connection to DB failed + schema: + $ref: '#/definitions/models.LicenseError' + summary: Check health + tags: + - Health /licenses: get: consumes: diff --git a/pkg/api/api.go b/pkg/api/api.go index b0d0373..104ae3e 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -15,6 +15,7 @@ import ( ginSwagger "github.com/swaggo/gin-swagger" "github.com/fossology/LicenseDb/pkg/auth" + "github.com/fossology/LicenseDb/pkg/db" "github.com/fossology/LicenseDb/pkg/models" ) @@ -66,6 +67,10 @@ func Router() *gin.Engine { obMap.GET("topic/:topic", GetObligationMapByTopic) obMap.GET("license/:license", GetObligationMapByLicense) } + health := unAuthorizedv1.Group("/health") + { + health.GET("", GetHealth) + } } authorizedv1 := r.Group("/api/v1") @@ -120,3 +125,43 @@ func HandleInvalidUrl(c *gin.Context) { } c.JSON(http.StatusNotFound, er) } + +// The GetHealth function returns if the DB is running and connected. +// +// @Summary Check health +// @Description Check health of the service +// @Id getHealth +// @Tags Health +// @Accept json +// @Produce json +// @Success 200 {object} models.LicenseError "Heath is OK" +// @Failure 500 {object} models.LicenseError "Connection to DB failed" +// @Router /health [get] +func GetHealth(c *gin.Context) { + // Fetch one license from DB to check if connection is still working. + var license models.LicenseDB + err := db.DB.Where(&models.LicenseDB{}).First(&license).Error + if license.Id == 0 || err != nil { + errorMessage := "" + if err != nil { + errorMessage = err.Error() + } + er := models.LicenseError{ + Status: http.StatusInternalServerError, + Message: "Database is not running or connected", + Error: errorMessage, + Path: c.Request.URL.Path, + Timestamp: time.Now().Format(time.RFC3339), + } + c.JSON(http.StatusInternalServerError, er) + return + } + er := models.LicenseError{ + Status: http.StatusOK, + Message: "Database is running and connected", + Error: "", + Path: c.Request.URL.Path, + Timestamp: time.Now().Format(time.RFC3339), + } + c.JSON(http.StatusOK, er) +}