refactor: Align Epic 0 & Epic 1 with true microservices architecture

Refactor core kernel and infrastructure to support true microservices
architecture where services are independently deployable.

Phase 1: Core Kernel Cleanup
- Remove database provider from CoreModule (services create their own)
- Update ProvideHealthRegistry to not depend on database
- Add schema support to database client (NewClientWithSchema)
- Update main entry point to remove database dependency
- Core kernel now provides only: config, logger, error bus, health, metrics, tracer, service registry

Phase 2: Service Registry Implementation
- Create ServiceRegistry interface (pkg/registry/registry.go)
- Implement Consul registry (internal/registry/consul/consul.go)
- Add Consul dependency (github.com/hashicorp/consul/api)
- Add registry configuration to config/default.yaml
- Add ProvideServiceRegistry() to DI container

Phase 3: Service Client Interfaces
- Create service client interfaces:
  - pkg/services/auth.go - AuthServiceClient
  - pkg/services/identity.go - IdentityServiceClient
  - pkg/services/authz.go - AuthzServiceClient
  - pkg/services/audit.go - AuditServiceClient
- Create ServiceClientFactory (internal/client/factory.go)
- Create stub gRPC client implementations (internal/client/grpc/)
- Add ProvideServiceClientFactory() to DI container

Phase 4: gRPC Service Definitions
- Create proto files for all core services:
  - api/proto/auth.proto
  - api/proto/identity.proto
  - api/proto/authz.proto
  - api/proto/audit.proto
- Add generate-proto target to Makefile

Phase 5: API Gateway Implementation
- Create API Gateway service entry point (cmd/api-gateway/main.go)
- Create Gateway implementation (services/gateway/gateway.go)
- Add gateway configuration to config/default.yaml
- Gateway registers with Consul and routes requests to backend services

All code compiles successfully. Core services (Auth, Identity, Authz, Audit)
will be implemented in Epic 2 using these foundations.
This commit is contained in:
2025-11-06 09:23:36 +01:00
parent 38a251968c
commit 16731fc1d1
25 changed files with 1826 additions and 21 deletions

View File

@@ -7,6 +7,7 @@ import (
"os"
"time"
"git.dcentral.systems/toolz/goplt/internal/client"
configimpl "git.dcentral.systems/toolz/goplt/internal/config"
errorbusimpl "git.dcentral.systems/toolz/goplt/internal/errorbus"
"git.dcentral.systems/toolz/goplt/internal/health"
@@ -14,10 +15,12 @@ import (
loggerimpl "git.dcentral.systems/toolz/goplt/internal/logger"
"git.dcentral.systems/toolz/goplt/internal/metrics"
"git.dcentral.systems/toolz/goplt/internal/observability"
"git.dcentral.systems/toolz/goplt/internal/registry/consul"
"git.dcentral.systems/toolz/goplt/internal/server"
"git.dcentral.systems/toolz/goplt/pkg/config"
"git.dcentral.systems/toolz/goplt/pkg/errorbus"
"git.dcentral.systems/toolz/goplt/pkg/logger"
"git.dcentral.systems/toolz/goplt/pkg/registry"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"go.uber.org/fx"
@@ -158,13 +161,11 @@ func ProvideErrorBus() fx.Option {
}
// ProvideHealthRegistry creates an FX option that provides the health check registry.
// Note: Database health checkers are registered by services that create their own database clients.
func ProvideHealthRegistry() fx.Option {
return fx.Provide(func(dbClient *database.Client) (*health.Registry, error) {
return fx.Provide(func() (*health.Registry, error) {
registry := health.NewRegistry()
// Register database health checker
registry.Register("database", health.NewDatabaseChecker(dbClient))
// Services will register their own health checkers (e.g., database, external dependencies)
return registry, nil
})
}
@@ -176,6 +177,72 @@ func ProvideMetrics() fx.Option {
})
}
// ProvideServiceRegistry creates an FX option that provides the service registry.
func ProvideServiceRegistry() fx.Option {
return fx.Provide(func(cfg config.ConfigProvider) (registry.ServiceRegistry, error) {
registryType := cfg.GetString("registry.type")
if registryType == "" {
registryType = "consul"
}
switch registryType {
case "consul":
consulCfg := consul.Config{
Address: cfg.GetString("registry.consul.address"),
Datacenter: cfg.GetString("registry.consul.datacenter"),
Scheme: cfg.GetString("registry.consul.scheme"),
}
// Set defaults
if consulCfg.Address == "" {
consulCfg.Address = "localhost:8500"
}
if consulCfg.Datacenter == "" {
consulCfg.Datacenter = "dc1"
}
if consulCfg.Scheme == "" {
consulCfg.Scheme = "http"
}
// Parse health check configuration
healthCheckInterval := cfg.GetDuration("registry.consul.health_check.interval")
if healthCheckInterval == 0 {
healthCheckInterval = 10 * time.Second
}
healthCheckTimeout := cfg.GetDuration("registry.consul.health_check.timeout")
if healthCheckTimeout == 0 {
healthCheckTimeout = 3 * time.Second
}
healthCheckDeregisterAfter := cfg.GetDuration("registry.consul.health_check.deregister_after")
if healthCheckDeregisterAfter == 0 {
healthCheckDeregisterAfter = 30 * time.Second
}
healthCheckHTTP := cfg.GetString("registry.consul.health_check.http")
if healthCheckHTTP == "" {
healthCheckHTTP = "/healthz"
}
consulCfg.HealthCheck = consul.HealthCheckConfig{
Interval: healthCheckInterval,
Timeout: healthCheckTimeout,
DeregisterAfter: healthCheckDeregisterAfter,
HTTP: healthCheckHTTP,
}
return consul.NewRegistry(consulCfg)
default:
return nil, fmt.Errorf("unsupported registry type: %s", registryType)
}
})
}
// ProvideServiceClientFactory creates an FX option that provides the service client factory.
func ProvideServiceClientFactory() fx.Option {
return fx.Provide(func(reg registry.ServiceRegistry) (*client.ServiceClientFactory, error) {
return client.NewServiceClientFactory(reg), nil
})
}
// ProvideTracer creates an FX option that provides the OpenTelemetry tracer.
func ProvideTracer() fx.Option {
return fx.Provide(func(cfg config.ConfigProvider, lc fx.Lifecycle) (trace.TracerProvider, error) {
@@ -318,18 +385,21 @@ func ProvideHTTPServer() fx.Option {
})
}
// CoreModule returns an FX option that provides all core services.
// This includes configuration, logging, database, error bus, health checks, metrics, tracing, and HTTP server.
// CoreModule returns an FX option that provides all core kernel infrastructure services.
// This includes configuration, logging, error bus, health checks, metrics, tracing, service registry, and service client factory.
// Note: Database and HTTP server are NOT included - services will create their own instances.
// HTTP server foundation is available via server.NewServer() for services to use.
func CoreModule() fx.Option {
return fx.Options(
ProvideConfig(),
ProvideLogger(),
ProvideDatabase(),
ProvideErrorBus(),
ProvideHealthRegistry(),
ProvideMetrics(),
ProvideTracer(),
ProvideHTTPServer(),
ProvideServiceRegistry(),
ProvideServiceClientFactory(),
// Note: ProvideDatabase() and ProvideHTTPServer() are removed - services create their own
)
}