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,21 +7,24 @@ import (
|
||||
)
|
||||
|
||||
type EmbedCardHandler struct {
|
||||
PostService services.PostService
|
||||
postService *services.PostService
|
||||
imgKitHelper *services.ImgKitHelper
|
||||
}
|
||||
|
||||
var EmbedCardHandlerInstance *EmbedCardHandler
|
||||
|
||||
func InitEmbedCardHandler() {
|
||||
EmbedCardHandlerInstance = &EmbedCardHandler{
|
||||
PostService: *services.PostServiceInstance,
|
||||
func NewEmbedCardHandler(
|
||||
postService *services.PostService,
|
||||
imgKitHelper *services.ImgKitHelper,
|
||||
) *EmbedCardHandler {
|
||||
return &EmbedCardHandler{
|
||||
postService: postService,
|
||||
imgKitHelper: imgKitHelper,
|
||||
}
|
||||
}
|
||||
|
||||
func (ps *EmbedCardHandler) GetEmbedCard(c *gin.Context) {
|
||||
post := ps.PostService.GetRandomPost()
|
||||
func (ech *EmbedCardHandler) GetEmbedCard(c *gin.Context) {
|
||||
post := ech.postService.GetRandomPost()
|
||||
c.HTML(200, "home/embed.html", gin.H{
|
||||
"postUrl": post.Url,
|
||||
"imageUrl": services.GetRemoteUrl(post.Attachments[0].RemoteUrl),
|
||||
"imageUrl": ech.imgKitHelper.GetRemoteUrl(post.Attachments[0].RemoteUrl),
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user