Fix gRPC health checks and add API Gateway Consul registration
Some checks failed
CI / Test (pull_request) Failing after 50s
CI / Lint (pull_request) Failing after 32s
CI / Build (pull_request) Successful in 17s
CI / Format Check (pull_request) Failing after 2s

- Fix gRPC health checks: Set serving status for default service (empty string) in all services
  - Consul checks the default service by default, not specific service names
  - All services now set both default and specific service status to SERVING

- Update Consul registration logic to automatically detect HTTP vs gRPC services
  - HTTP services (API Gateway) use HTTP health checks
  - gRPC services use gRPC health checks
  - Detection based on service tags and metadata

- Add API Gateway Consul registration
  - Register with Docker service name in Docker environment
  - Use HTTP health checks for API Gateway
  - Proper host/port configuration handling

- Add API Gateway HTTP-to-gRPC handlers
  - Implement service-specific handlers for Auth and Identity services
  - Translate HTTP requests to gRPC calls
  - Map gRPC error codes to HTTP status codes
This commit is contained in:
2025-11-06 22:04:55 +01:00
parent 04022b835e
commit 988adf6cc5
8 changed files with 610 additions and 66 deletions

View File

@@ -58,14 +58,25 @@ func main() {
}
gateway.SetupRoutes(srv.Router())
// Register with Consul
// Determine port and host for registration
gatewayPort := cfg.GetInt("gateway.port")
if gatewayPort == 0 {
gatewayPort = 8080
gatewayPort = cfg.GetInt("server.port")
if gatewayPort == 0 {
gatewayPort = 8080
}
}
// In Docker, always use the Docker service name for health checks
// Consul (also in Docker) needs to reach the service via Docker DNS
gatewayHost := cfg.GetString("gateway.host")
if gatewayHost == "" {
gatewayHost = "localhost"
if os.Getenv("ENVIRONMENT") == "production" || os.Getenv("DOCKER") == "true" {
gatewayHost = "api-gateway" // Docker service name - required for Consul health checks
} else if gatewayHost == "" {
gatewayHost = cfg.GetString("server.host")
if gatewayHost == "" || gatewayHost == "0.0.0.0" {
gatewayHost = "localhost" // Local development
}
}
serviceInstance := &registry.ServiceInstance{
@@ -75,7 +86,8 @@ func main() {
Port: gatewayPort,
Tags: []string{"gateway", "http"},
Metadata: map[string]string{
"version": "1.0.0",
"version": "1.0.0",
"protocol": "http",
},
}

View File

@@ -324,6 +324,9 @@ func provideAuditService() fx.Option {
// Register health service
healthServer := health.NewServer()
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
// Set serving status for the default service (empty string) - this is what Consul checks
healthServer.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
// Also set for the specific service name
healthServer.SetServingStatus("audit.v1.AuditService", grpc_health_v1.HealthCheckResponse_SERVING)
// Register reflection for grpcurl

View File

@@ -406,6 +406,9 @@ func provideAuthService() fx.Option {
// Register health service
healthServer := health.NewServer()
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
// Set serving status for the default service (empty string) - this is what Consul checks
healthServer.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
// Also set for the specific service name
healthServer.SetServingStatus("auth.v1.AuthService", grpc_health_v1.HealthCheckResponse_SERVING)
// Register reflection for grpcurl

View File

@@ -271,6 +271,9 @@ func provideAuthzService() fx.Option {
// Register health service
healthServer := health.NewServer()
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
// Set serving status for the default service (empty string) - this is what Consul checks
healthServer.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
// Also set for the specific service name
healthServer.SetServingStatus("authz.v1.AuthzService", grpc_health_v1.HealthCheckResponse_SERVING)
// Register reflection for grpcurl

View File

@@ -418,6 +418,9 @@ func provideIdentityService() fx.Option {
// Register health service
healthServer := health.NewServer()
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
// Set serving status for the default service (empty string) - this is what Consul checks
healthServer.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
// Also set for the specific service name
healthServer.SetServingStatus("identity.v1.IdentityService", grpc_health_v1.HealthCheckResponse_SERVING)
// Register reflection for grpcurl