You've already forked orderservice
feat: added healthcheck
This commit is contained in:
@@ -0,0 +1,63 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HealthHandler struct {
|
||||||
|
DB *sqlx.DB
|
||||||
|
Redis *redis.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHealthHandler(db *sqlx.DB, redisDB *redis.Client) *HealthHandler {
|
||||||
|
return &HealthHandler{
|
||||||
|
DB: db,
|
||||||
|
Redis: redisDB,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type HealthResponse struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
Details map[string]string `json:"details"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
details := map[string]string{}
|
||||||
|
|
||||||
|
if err := h.DB.PingContext(ctx); err != nil {
|
||||||
|
details["postgres"] = "unhealthy: " + err.Error()
|
||||||
|
} else {
|
||||||
|
details["postgres"] = "ok"
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.Redis.Ping(ctx).Err(); err != nil {
|
||||||
|
details["redis"] = "unhealthy: " + err.Error()
|
||||||
|
} else {
|
||||||
|
details["redis"] = "ok"
|
||||||
|
}
|
||||||
|
|
||||||
|
status := "ok"
|
||||||
|
for _, v := range details {
|
||||||
|
if v != "ok" {
|
||||||
|
status = "unhealthy"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := HealthResponse{Status: status, Details: details}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if status != "ok" {
|
||||||
|
w.WriteHeader(http.StatusServiceUnavailable)
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(resp)
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"orderservice/internal/interceptor"
|
"orderservice/internal/interceptor"
|
||||||
|
|
||||||
grpcHandlers "orderservice/internal/handler/grpc"
|
grpcHandlers "orderservice/internal/handler/grpc"
|
||||||
|
httpHandlers "orderservice/internal/handler/http"
|
||||||
orderPostgresRepo "orderservice/internal/repository/postgres"
|
orderPostgresRepo "orderservice/internal/repository/postgres"
|
||||||
"orderservice/internal/service"
|
"orderservice/internal/service"
|
||||||
|
|
||||||
@@ -57,8 +58,12 @@ func runHTTPHandler(s *Server, grpcServerEndpoint *string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.Handle("/healthz", httpHandlers.NewHealthHandler(s.db, s.redisDB))
|
||||||
|
mux.Handle("/", gwmux)
|
||||||
|
|
||||||
addr := fmt.Sprintf(":%d", s.config.HTTPPort)
|
addr := fmt.Sprintf(":%d", s.config.HTTPPort)
|
||||||
return http.ListenAndServe(addr, gwmux)
|
return http.ListenAndServe(addr, mux)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDatabase(cfg config.Config) (*sqlx.DB, error) {
|
func getDatabase(cfg config.Config) (*sqlx.DB, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user