# Story 0.5: Dependency Injection and Application Bootstrap ## Metadata - **Story ID**: 0.5 - **Title**: Dependency Injection and Application Bootstrap - **Epic**: 0 - Project Setup & Foundation - **Status**: Completed - **Priority**: High - **Estimated Time**: 4-5 hours - **Dependencies**: 0.1, 0.2, 0.3 ## Goal Set up dependency injection container using Uber FX and create the application entry point that initializes the platform with proper lifecycle management. ## Description This story implements the dependency injection system using Uber FX and creates the main application entry point. The DI container will manage service lifecycle, dependencies, and provide a clean way to wire services together. ## Deliverables ### 1. DI Container (`internal/di/container.go`) FX-based dependency injection container: - Initialize FX container - Register Config and Logger providers - Basic lifecycle hooks (OnStart, OnStop) - Support service overrides for testing - Graceful shutdown handling ### 2. DI Providers (`internal/di/providers.go`) Provider functions for core services: - `ProvideConfig() fx.Option` - Configuration provider - `ProvideLogger() fx.Option` - Logger provider - Provider functions return FX options for easy composition ### 3. Application Entry Point (`cmd/platform/main.go`) Main application bootstrap: - Load configuration - Initialize DI container with core services - Set up basic application lifecycle - Start minimal HTTP server (placeholder for Epic 1) - Handle graceful shutdown (SIGINT, SIGTERM) - Proper error handling and logging ### 4. Core Module (`internal/di/core_module.go`) Optional: Export core module as FX option: - `CoreModule() fx.Option` - Provides all core services - Easy to compose with future modules ## Implementation Steps 1. **Install Dependencies** ```bash go get go.uber.org/fx@latest ``` 2. **Create DI Container** - Create `internal/di/container.go` - Initialize FX app - Set up lifecycle hooks - Add graceful shutdown 3. **Create Provider Functions** - Create `internal/di/providers.go` - Implement `ProvideConfig()` function - Implement `ProvideLogger()` function - Return FX options 4. **Create Application Entry Point** - Create `cmd/platform/main.go` - Load configuration - Initialize FX app with providers - Set up signal handling - Start minimal server (placeholder) - Handle shutdown gracefully 5. **Test Application** - Verify application starts - Verify graceful shutdown works - Test service injection ## Acceptance Criteria - [x] DI container initializes successfully - [x] Config and Logger are provided via DI - [x] Application starts and runs - [x] Application shuts down gracefully on signals - [x] Lifecycle hooks work correctly - [x] Services can be overridden for testing - [x] Application compiles and runs successfully - [x] Error handling is comprehensive - [x] Logging works during startup/shutdown ## Related ADRs - [ADR-0003: Dependency Injection Framework](../../adr/0003-dependency-injection-framework.md) ## Implementation Notes - Use FX for dependency injection and lifecycle management - Support graceful shutdown with context cancellation - Handle SIGINT and SIGTERM signals - Log startup and shutdown events - Make services easily testable via interfaces - Consider adding health check endpoint in future - Support for service overrides is important for testing ## Testing ```bash # Test application startup go run cmd/platform/main.go # Test graceful shutdown # Start app, then send SIGTERM kill -TERM # Test DI container go test ./internal/di/... ``` ## Files to Create/Modify - `internal/di/container.go` - DI container - `internal/di/providers.go` - Provider functions - `internal/di/core_module.go` - Core module (optional) - `cmd/platform/main.go` - Application entry point - `go.mod` - Add FX dependency