- 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>
31 lines
787 B
Go
31 lines
787 B
Go
package handlers
|
|
|
|
import (
|
|
"CatsOfMastodonBotGo/internal/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ApiEndpointHandler struct {
|
|
postService *services.PostService
|
|
imgKitHelper *services.ImgKitHelper
|
|
}
|
|
|
|
func NewApiEndpointHandler(
|
|
postService *services.PostService,
|
|
imgKitHelper *services.ImgKitHelper,
|
|
) *ApiEndpointHandler {
|
|
return &ApiEndpointHandler{
|
|
postService: postService,
|
|
imgKitHelper: imgKitHelper,
|
|
}
|
|
}
|
|
|
|
func (aeh *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
|
|
post := aeh.postService.GetRandomPost()
|
|
for i := range post.Attachments {
|
|
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)
|
|
} |