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:
2025-09-16 11:41:01 +03:30
co-authored by Qwen-Coder
parent d4044b0eaf
commit f136ae58b3
15 changed files with 389 additions and 255 deletions
+15 -17
View File
@@ -2,7 +2,6 @@ package services
import (
"CatsOfMastodonBotGo/internal/config"
"CatsOfMastodonBotGo/internal/database"
"CatsOfMastodonBotGo/internal/domain"
"context"
"encoding/json"
@@ -16,42 +15,41 @@ import (
)
type PostService struct {
db *gorm.DB
db *gorm.DB
cfg *config.Config
}
var PostServiceInstance *PostService
// Constructor
func InitPostService() {
PostServiceInstance = &PostService{db: database.Gorm}
func NewPostService(db *gorm.DB, cfg *config.Config) *PostService {
return &PostService{db: db, cfg: cfg}
}
func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) (error, []domain.Post) {
func (ps *PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) ([]domain.Post, error) {
var requestUrl = instance + "/api/v1/timelines/tag/" + tag + "?limit=40"
req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil)
if err != nil {
return err, nil
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err, nil
return nil, err
}
if resp.StatusCode != 200 || strings.Split(strings.ToLower(resp.Header.Get("Content-Type")), ";")[0] != "application/json" {
return fmt.Errorf("Status code:", resp.StatusCode, " Content-Type:", resp.Header.Get("Content-Type")), nil
return nil, fmt.Errorf("status code: %d, content-type: %s", resp.StatusCode, resp.Header.Get("Content-Type"))
}
var posts []domain.Post = nil
err = json.NewDecoder(resp.Body).Decode(&posts)
if err != nil {
return err, nil
return nil, err
}
// defer: it basically means "do this later when the function returns"
defer resp.Body.Close()
if posts == nil {
return fmt.Errorf("no posts found for tag %s on instance %s", tag, instance), nil
return nil, fmt.Errorf("no posts found for tag %s on instance %s", tag, instance)
}
return nil, posts
return posts, nil
}
func (ps *PostService) GetExistingPostIds() []string {
@@ -66,7 +64,7 @@ func (ps *PostService) GetExistingAccountIds() []string {
return existingAccountIds
}
func (*PostService) GetNewPosts(existingPostIds []string, posts []domain.Post) []domain.Post {
func (ps *PostService) GetNewPosts(existingPostIds []string, posts []domain.Post) []domain.Post {
var newPosts []domain.Post = nil
for _, post := range posts {
if !arrayContains(existingPostIds, post.ID) && len(post.Attachments) > 0 && !post.Account.IsBot {
@@ -85,7 +83,7 @@ func (*PostService) GetNewPosts(existingPostIds []string, posts []domain.Post) [
return newPosts
}
func (*PostService) GetNewAccounts(existingAccountIds []string, posts []domain.Post) []domain.Account {
func (ps *PostService) GetNewAccounts(existingAccountIds []string, posts []domain.Post) []domain.Account {
var newAccounts []domain.Account = nil
for _, post := range posts {
if !arrayContains(existingAccountIds, post.Account.AccId) {
@@ -153,7 +151,7 @@ func (ps *PostService) RejectMedia(mediaId string) bool {
func (ps *PostService) GetMedia() domain.MediaAttachment {
var media domain.MediaAttachment
orderExpr := "RANDOM()" // sqlite
if config.Config.DBEngine != "sqlite" {
if ps.cfg.DBEngine != "sqlite" {
orderExpr = "RAND()" // mariadb/mysql
}
ps.db.Model(&domain.MediaAttachment{}).
@@ -171,4 +169,4 @@ func arrayContains(arr []string, str string) bool {
}
}
return false
}
}