Skip to content
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
7 changes: 7 additions & 0 deletions internal/health/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package health

import "go.mongodb.org/mongo-driver/v2/mongo"

type HealthController struct {
DB *mongo.Client
}
17 changes: 17 additions & 0 deletions internal/health/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package health

import (
"context"
"net/http"

"github.com/gin-gonic/gin"
)

func (hc *HealthController) Get(c *gin.Context) {
err := hc.DB.Ping(context.TODO(), nil)
if err != nil {
c.AbortWithStatusJSON(http.StatusServiceUnavailable, gin.H{"error": "Database connection is down"})
return
}
c.Status(http.StatusOK)
}
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/SnackLog/database-api-wrapper/internal/config"
"github.com/SnackLog/database-api-wrapper/internal/database"
"github.com/SnackLog/database-api-wrapper/internal/handlers/product"
"github.com/SnackLog/database-api-wrapper/internal/health"
serviceConfigLib "github.com/SnackLog/service-config-lib"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/v2/mongo"
Expand Down Expand Up @@ -55,13 +56,21 @@ func disconnectDatabase(client *mongo.Client) {

func setupRouter(db *mongo.Client) *gin.Engine {
r := gin.Default()
products := r.Group("/products")
setupHealthCheckEndpoint(db, r)

products := r.Group("/products")
setupEndpoints(products, db)

return r
}

func setupHealthCheckEndpoint(db *mongo.Client, router *gin.Engine) {
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GET handler registration is incomplete. The second argument (handler function) is missing. This should instantiate a HealthController and pass its Get method as the handler.

Copilot uses AI. Check for mistakes.
hc := &health.HealthController{
Comment on lines 66 to +68
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setupHealthCheckEndpoint function should accept the database client as a parameter (similar to setupEndpoints) to pass it to the HealthController, since the health check needs to ping the database.

Copilot uses AI. Check for mistakes.
DB: db,
}
router.GET("/health", hc.Get)
}

func setupEndpoints(router *gin.RouterGroup, db *mongo.Client) {
productController := &product.ProductController{DB: db}

Expand Down