Skip to content

Commit

Permalink
feat: add a health endpoint and upgrade Dockerfile and Compose
Browse files Browse the repository at this point in the history
- Upgrade Golang version in Dockerfile from 1.19.5-alpine3.17 to 1.23-alpine
- Add health check endpoint "/health" in server.go
  • Loading branch information
Pradumnasaraf committed Sep 16, 2024
1 parent 97dc7cf commit 737dc0e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19.5-alpine3.17 AS builder
FROM golang:1.23-alpine AS builder
WORKDIR /build
EXPOSE 8081
COPY go.mod go.sum ./
Expand Down
29 changes: 18 additions & 11 deletions docker-compose.yml → compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.9"

services:
api:
container_name: go-api
Expand All @@ -9,29 +7,38 @@ services:
image: go-api
ports:
- 8080:8080
env_file:
- .env.example
environment:
- MONGO_URI=mongodb://db:27017
- MONGO_DB=opensource
- MONGO_COLLECTION=contributors
- PORT=8080
- BASIC_AUTH_USERNAME=opensource
- BASIC_AUTH_PASSWORD=greensquare
depends_on:
- db
networks:
- go-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 5
depends_on:
db:
condition: service_healthy

db:
container_name: go-mongodb
image: mongo:6.0
image: mongo:7.0.0
volumes:
- dbdata:/data/db
networks:
- go-network
healthcheck:
test: ["CMD", "pgrep", "mongod"]
interval: 10s
timeout: 10s
retries: 5
start_period: 40s

volumes:
dbdata:

networks:
go-network:
driver: bridge
driver: bridge
8 changes: 8 additions & 0 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ func PlaygroundHandler() gin.HandlerFunc {
h.ServeHTTP(c.Writer, c.Request)
}
}

func HealthCheckHandler() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "healthy",
})
}
}
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func main() {

router.Use(middleware.BasicAuth())
router.GET("/", handler.PlaygroundHandler())
router.GET("/health", handler.HealthCheckHandler())
router.POST("/query", handler.GraphqlHandler())

log.Fatal(router.Run())
Expand Down

0 comments on commit 737dc0e

Please sign in to comment.