Skip to content

Commit

Permalink
Merge pull request #30 from fossology/feat/api/health
Browse files Browse the repository at this point in the history
feat(api): add /health endpoint
  • Loading branch information
GMishx authored Jan 5, 2024
2 parents aa42310 + 988b0b8 commit 6bba2bd
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 30 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
45 changes: 45 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}

0 comments on commit 6bba2bd

Please sign in to comment.