Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add /health endpoint #30

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}