Files
goplt/docs/content/stories/epic0/0.5-di-and-bootstrap.md
0x1d 610677af72
All checks were successful
CI / Lint (pull_request) Successful in 10s
CI / Format Check (pull_request) Successful in 2s
CI / Test (pull_request) Successful in 11s
CI / Build (pull_request) Successful in 6s
docs: mark all epic0 stories as completed
Update status of all epic0 stories (0.1-0.5) from Pending to Completed:
- 0.1: Project Initialization - Directory structure and Go module setup
- 0.2: Configuration Management System - Viper-based config implemented
- 0.3: Structured Logging System - Zap logger with middleware implemented
- 0.4: CI/CD Pipeline - GitHub Actions workflow with tests and linting
- 0.5: DI and Bootstrap - FX-based DI container with lifecycle management

All stories have been implemented with tests and are working.
2025-11-05 13:44:00 +01:00

3.8 KiB

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

    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

  • DI container initializes successfully
  • Config and Logger are provided via DI
  • Application starts and runs
  • Application shuts down gracefully on signals
  • Lifecycle hooks work correctly
  • Services can be overridden for testing
  • Application compiles and runs successfully
  • Error handling is comprehensive
  • Logging works during startup/shutdown

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

# Test application startup
go run cmd/platform/main.go

# Test graceful shutdown
# Start app, then send SIGTERM
kill -TERM <pid>

# 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