# Story 3.3: Service Loader and Initialization ## Metadata - **Story ID**: 3.3 - **Title**: Service Loader and Initialization - **Epic**: 3 - Module Framework (Feature Services) - **Status**: Pending - **Priority**: High - **Estimated Time**: 6-8 hours - **Dependencies**: 3.1, 1.2, 1.7 ## Goal Implement service initialization helpers for feature services with dependency resolution, Consul registration, and automatic migration execution. Each service initializes itself in its own entry point. ## Description This story implements service initialization helpers that feature services use in their entry points (`cmd/{service}/main.go`). Services initialize themselves, resolve dependencies (other services), register with Consul, run migrations, and start their gRPC servers. The loader provides helpers for common service initialization patterns. ## Deliverables ### 1. Service Initialization Helpers (`internal/service/init.go`) - `InitializeService(cfg, module IModule)` function - Bootstrap core kernel services - Initialize service-specific components - Register with Consul service registry - Run database migrations (per-service schema) - Start gRPC server - Handle graceful shutdown ### 2. Service Dependency Resolution (`internal/service/deps.go`) - Resolve service dependencies (check other services are available via Consul) - Wait for dependent services to be healthy - Service discovery integration - Dependency validation ### 3. Service Bootstrap Pattern (`internal/service/bootstrap.go`) - Common bootstrap pattern for feature services - FX lifecycle integration - Service registration helpers - Health check setup - Migration runner (per-service) ### 4. Service Template/Scaffolding - Service template generator - Standard service structure - Example service entry point **Note:** Each feature service has its own `cmd/{service}/main.go` that uses these helpers to initialize itself. ## Implementation Steps 1. **Create Service Initialization Helpers** - Create `internal/service/init.go` - Implement service bootstrap pattern - Integrate with Consul service registry 2. **Implement Dependency Resolution** - Create `internal/service/deps.go` - Check service dependencies via Consul - Wait for services to be healthy 3. **Create Service Bootstrap** - Create `internal/service/bootstrap.go` - Common bootstrap pattern - FX lifecycle integration 4. **Create Service Template** - Service template generator - Example service entry point 5. **Test Service Initialization** - Test service startup - Test Consul registration - Test dependency resolution ## Acceptance Criteria - [x] Service initialization helpers work correctly - [x] Services register with Consul automatically - [x] Service migrations run on startup (per-service schema) - [x] Service dependency resolution works via Consul - [x] Services wait for dependencies to be healthy - [x] FX lifecycle integration works - [x] Service bootstrap pattern is reusable - [x] Service template generator creates standard structure ## Related ADRs - [ADR-0021: Module Loading Strategy](../../adr/0021-module-loading-strategy.md) - [ADR-0029: Microservices Architecture](../../adr/0029-microservices-architecture.md) - [ADR-0030: Service Communication Strategy](../../adr/0030-service-communication-strategy.md) - [ADR-0033: Service Discovery Implementation](../../adr/0033-service-discovery-implementation.md) ## Files to Create/Modify - `internal/service/init.go` - Service initialization helpers - `internal/service/deps.go` - Service dependency resolution - `internal/service/bootstrap.go` - Service bootstrap pattern - `internal/service/template.go` - Service template generator - Example: `cmd/blog-service/main.go` - Example service entry point