- Replace global variable pattern with proper dependency injection - Add uber-go/fx for automatic dependency resolution - Refactor all services and handlers to use constructor injection - Eliminate fragile initialization order dependencies - Improve testability and modularity - Add structured logging with zap Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
75 lines
2.5 KiB
Markdown
75 lines
2.5 KiB
Markdown
# FX Dependency Injection Implementation
|
|
|
|
This document summarizes the changes made to implement uber-go/fx dependency injection in the CatsOfMastodonBotGo application.
|
|
|
|
## Overview
|
|
|
|
We've replaced the global variable pattern with proper dependency injection using uber-go/fx. This improves:
|
|
- Testability
|
|
- Modularity
|
|
- Explicit dependency management
|
|
- Eliminates fragile initialization order dependencies
|
|
|
|
## Key Changes
|
|
|
|
### 1. Added Dependencies
|
|
- Added `go.uber.org/fx` dependency to go.mod
|
|
- Added `go.uber.org/zap` for structured logging
|
|
|
|
### 2. Refactored main.go
|
|
- Replaced manual initialization with fx application
|
|
- Used `fx.Provide` to register constructors for all components
|
|
- Used `fx.Invoke` to start background tasks and server
|
|
- Added proper logger integration
|
|
|
|
### 3. Refactored Configuration (config/config.go)
|
|
- Removed global `Config` variable
|
|
- Removed `Init()` function
|
|
- Kept `Load()` function as constructor
|
|
|
|
### 4. Refactored Database (database/database.go)
|
|
- Removed global `Gorm` variable
|
|
- Function now accepts config as parameter
|
|
|
|
### 5. Refactored Services
|
|
- **PostService**: Now accepts database and config as dependencies
|
|
- **ImgKitHelper**: Now accepts config as dependency
|
|
|
|
### 6. Refactored Auth
|
|
- **JwtTokenGenerator**: Now accepts config as dependency
|
|
- **GiteaOAuth2Handler**: Now accepts config as dependency
|
|
|
|
### 7. Refactored Handlers
|
|
- All handlers now use constructor injection
|
|
- Dependencies are explicitly declared in constructor signatures
|
|
- Removed global instance variables
|
|
|
|
### 8. Refactored Server/Router
|
|
- Router now receives all handlers through fx dependency injection
|
|
- Uses fx.Lifecycle for proper startup/shutdown handling
|
|
|
|
## Benefits Achieved
|
|
|
|
1. **Eliminated Global State**: No more global variables causing tight coupling
|
|
2. **Explicit Dependencies**: All dependencies are clearly visible in constructor signatures
|
|
3. **Automatic Wiring**: fx automatically resolves and injects dependencies
|
|
4. **Improved Testability**: Easy to mock dependencies for unit tests
|
|
5. **Reduced Boilerplate**: No need for manual initialization functions
|
|
6. **Error Detection**: Dependency resolution errors caught at startup
|
|
|
|
## Files Modified
|
|
|
|
- cmd/CatsOfMastodonBotGo/main.go
|
|
- go.mod
|
|
- go.sum
|
|
- internal/auth/jwt.go
|
|
- internal/auth/oauth2.go
|
|
- internal/config/config.go
|
|
- internal/database/database.go
|
|
- internal/server/router.go
|
|
- internal/services/imgKitHelper.go
|
|
- internal/services/postService.go
|
|
- internal/web/handlers/adminDash.go
|
|
- internal/web/handlers/apiEndpoint.go
|
|
- internal/web/handlers/embedCard.go
|
|
- internal/web/handlers/oauth.go |