- 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>
30 lines
666 B
Go
30 lines
666 B
Go
package handlers
|
|
|
|
import (
|
|
"CatsOfMastodonBotGo/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type EmbedCardHandler struct {
|
|
postService *services.PostService
|
|
imgKitHelper *services.ImgKitHelper
|
|
}
|
|
|
|
func NewEmbedCardHandler(
|
|
postService *services.PostService,
|
|
imgKitHelper *services.ImgKitHelper,
|
|
) *EmbedCardHandler {
|
|
return &EmbedCardHandler{
|
|
postService: postService,
|
|
imgKitHelper: imgKitHelper,
|
|
}
|
|
}
|
|
|
|
func (ech *EmbedCardHandler) GetEmbedCard(c *gin.Context) {
|
|
post := ech.postService.GetRandomPost()
|
|
c.HTML(200, "home/embed.html", gin.H{
|
|
"postUrl": post.Url,
|
|
"imageUrl": ech.imgKitHelper.GetRemoteUrl(post.Attachments[0].RemoteUrl),
|
|
})
|
|
} |