added health routes
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"authentication/db"
|
||||
"authentication/models"
|
||||
"authentication/redisclient"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func HealthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(models.HealthResponse{
|
||||
Status: "ok",
|
||||
})
|
||||
}
|
||||
|
||||
// ReadyHandler checks if the service is ready to handle requests
|
||||
// @Summary Readiness check endpoint
|
||||
// @Description Returns readiness status including database and Redis connectivity
|
||||
// @Tags health
|
||||
// @Produce json
|
||||
// @Success 200 {object} HealthResponse
|
||||
// @Failure 503 {object} HealthResponse
|
||||
// @Router /ready [get]
|
||||
func ReadyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
services := make(map[string]string)
|
||||
allHealthy := true
|
||||
|
||||
// Check database
|
||||
if db.DB != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
if err := db.DB.PingContext(ctx); err != nil {
|
||||
services["database"] = "unhealthy"
|
||||
allHealthy = false
|
||||
} else {
|
||||
services["database"] = "healthy"
|
||||
}
|
||||
} else {
|
||||
services["database"] = "not_initialized"
|
||||
allHealthy = false
|
||||
}
|
||||
|
||||
// Check Redis
|
||||
if redisclient.RDB != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
if _, err := redisclient.RDB.Ping(ctx).Result(); err != nil {
|
||||
services["redis"] = "unhealthy"
|
||||
allHealthy = false
|
||||
} else {
|
||||
services["redis"] = "healthy"
|
||||
}
|
||||
} else {
|
||||
services["redis"] = "not_initialized"
|
||||
allHealthy = false
|
||||
}
|
||||
|
||||
status := "ready"
|
||||
statusCode := http.StatusOK
|
||||
if !allHealthy {
|
||||
status = "not_ready"
|
||||
statusCode = http.StatusServiceUnavailable
|
||||
}
|
||||
|
||||
w.WriteHeader(statusCode)
|
||||
json.NewEncoder(w).Encode(models.HealthResponse{
|
||||
Status: status,
|
||||
Services: services,
|
||||
})
|
||||
}
|
||||
@@ -241,7 +241,7 @@ func main() {
|
||||
|
||||
helper.LogInfo("INFO: Authentication Microservice is running on http://localhost:8081")
|
||||
server := &http.Server{
|
||||
Addr: ":8081",
|
||||
Addr: ":8080",
|
||||
Handler: handler,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 300 * time.Second,
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
// HealthResponse represents the health check response
|
||||
type HealthResponse struct {
|
||||
Status string `json:"status"`
|
||||
Services map[string]string `json:"services,omitempty"`
|
||||
}
|
||||
+5
-1
@@ -9,6 +9,10 @@ import (
|
||||
)
|
||||
|
||||
func SetupRoutes(router *mux.Router, db *sql.DB) {
|
||||
|
||||
router.HandleFunc("/health", handlers.HealthHandler).Methods("GET")
|
||||
router.HandleFunc("/ready", handlers.ReadyHandler).Methods("GET")
|
||||
|
||||
authRoutes := router.PathPrefix("/v1/auth").Subrouter()
|
||||
authRoutes.HandleFunc("/login", handlers.GoogleLogin).Methods("GET")
|
||||
authRoutes.HandleFunc("/callback", handlers.GoogleCallback).Methods("GET")
|
||||
@@ -16,7 +20,7 @@ func SetupRoutes(router *mux.Router, db *sql.DB) {
|
||||
authRoutes.HandleFunc("/logout", handlers.LogoutHandler).Methods("GET")
|
||||
|
||||
// authRoutes.HandleFunc("/microsoft/login", handlers.MicrosoftLogin).Methods("GET")
|
||||
// authRoutes.HandleFunc("/microsoft/callback", handlers.MicrosoftCallback).Methods("GET")
|
||||
// authRoutes.HandleFunc("/microsoft/callback", handlers.MicrosotCallback).Methods("GET")
|
||||
|
||||
router.PathPrefix("/swagger/").Handler(httpSwagger.WrapHandler)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user