Refactor: Implement uber-go/fx dependency injection
- 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>
This commit is contained in:
@@ -7,23 +7,25 @@ import (
|
||||
)
|
||||
|
||||
type ApiEndpointHandler struct {
|
||||
PostService services.PostService
|
||||
postService *services.PostService
|
||||
imgKitHelper *services.ImgKitHelper
|
||||
}
|
||||
|
||||
var ApiEndpointHandlerInstance *ApiEndpointHandler
|
||||
|
||||
func InitApiEndpointHandler() {
|
||||
ApiEndpointHandlerInstance = &ApiEndpointHandler{
|
||||
PostService: *services.PostServiceInstance,
|
||||
func NewApiEndpointHandler(
|
||||
postService *services.PostService,
|
||||
imgKitHelper *services.ImgKitHelper,
|
||||
) *ApiEndpointHandler {
|
||||
return &ApiEndpointHandler{
|
||||
postService: postService,
|
||||
imgKitHelper: imgKitHelper,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (ps *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
|
||||
post := ps.PostService.GetRandomPost()
|
||||
func (aeh *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
|
||||
post := aeh.postService.GetRandomPost()
|
||||
for i := range post.Attachments {
|
||||
post.Attachments[i].RemoteUrl = services.GetRemoteUrl(post.Attachments[i].RemoteUrl)
|
||||
post.Attachments[i].PreviewUrl = services.GetPreviewUrl(post.Attachments[i].RemoteUrl)
|
||||
post.Attachments[i].RemoteUrl = aeh.imgKitHelper.GetRemoteUrl(post.Attachments[i].RemoteUrl)
|
||||
post.Attachments[i].PreviewUrl = aeh.imgKitHelper.GetPreviewUrl(post.Attachments[i].RemoteUrl)
|
||||
}
|
||||
c.JSON(200, post)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user