Repository navigation
Expand file tree
/
Copy pathdependencies.go
More file actions
182 lines (162 loc) · 6.8 KB
/
Copy pathdependencies.go
File metadata and controls
182 lines (162 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package http
import (
"context"
"github.com/hamidgh01/Go-Blog-API/config"
"github.com/hamidgh01/Go-Blog-API/internal/application/service_errors"
"github.com/hamidgh01/Go-Blog-API/internal/application/services"
"github.com/hamidgh01/Go-Blog-API/internal/domain/repository"
"github.com/hamidgh01/Go-Blog-API/internal/http/handlers"
"github.com/hamidgh01/Go-Blog-API/internal/http/middlewares"
redisInfra "github.com/hamidgh01/Go-Blog-API/internal/infra/redis"
"github.com/hamidgh01/Go-Blog-API/internal/infra/security/hashing"
"github.com/hamidgh01/Go-Blog-API/internal/infra/security/jwt"
"github.com/hamidgh01/Go-Blog-API/pkg/constants"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
)
type accessControlMiddlewareSignature func(
accessibility constants.EndpointAccessibility,
getResourceOwnerIdService func(ctx context.Context, pk uint64) (uint64, *service_errors.ServiceError),
) gin.HandlerFunc
// DependencyContainer holds all application dependencies
type DependencyContainer struct {
// Repositories
UserRepository repository.UserRepository
PostRepository repository.PostRepository
CommentRepository repository.CommentRepository
TagRepository repository.TagRepository
LinkRepository repository.LinkRepository
ListRepository repository.ListRepository
FollowRepository repository.FollowRepository
LikeRepository repository.LikeRepository
SavePostRepository repository.SavePostRepository
SaveListRepository repository.SaveListRepository
PostTagsRepository repository.PostTagsRepository
// other infrastructure services (security, caching, etc.)
JwtManager *jwt.JWTManager
PasswordHasher *hashing.PasswordHasher
TokenRevoker *redisInfra.TokenRevoker
UserInfoCache *redisInfra.UserInfoCache
// Services
AuthService *services.AuthService
UserService *services.UserService
PostService *services.PostService
CommentService *services.CommentService
TagService *services.TagService
LinkService *services.LinkService
ListService *services.ListService
FollowService *services.FollowService
LikeService *services.LikeService
SavePostService *services.SavePostService
SaveListService *services.SaveListService
PostTagsService *services.PostTagsService
// Handlers
AuthHandler *handlers.AuthHandler
UserHandler *handlers.UserHandler
PostHandler *handlers.PostHandler
CommentHandler *handlers.CommentHandler
TagHandler *handlers.TagHandler
LinkHandler *handlers.LinkHandler
ListHandler *handlers.ListHandler
FollowHandler *handlers.FollowHandler
LikeHandler *handlers.LikeHandler
SavePostHandler *handlers.SavePostHandler
SaveListHandler *handlers.SaveListHandler
PostTagsHandler *handlers.PostTagsHandler
// Middlewares
AuthMiddleware *middlewares.AuthenticationMiddleware
AccessControlMiddleware accessControlMiddlewareSignature
}
// NewDependencyContainer creates and wires all dependencies
func NewDependencyContainer(
cfg *config.Config, repoInjector repository.RepositoryInjector, redis *redis.Client,
) *DependencyContainer {
// initialize repositories
userRepo := repoInjector.GetUserRepository()
postRepo := repoInjector.GetPostRepository()
commentRepo := repoInjector.GetCommentRepository()
listRepo := repoInjector.GetListRepository()
linkRepo := repoInjector.GetLinkRepository()
tagRepo := repoInjector.GetTagRepository()
followRepo := repoInjector.GetFollowRepository()
likeRepo := repoInjector.GetLikeRepository()
savePostRepo := repoInjector.GetSavePostRepository()
saveListRepo := repoInjector.GetSaveListRepository()
postTagsRepo := repoInjector.GetPostTagsRepository()
// initialize infrastructure services
jwtManager := jwt.NewJWTManager(&cfg.Jwt)
passwordHasher := hashing.NewPasswordHasher()
tokenRevoker := redisInfra.NewTokenRevoker(redis)
userInfoCache := redisInfra.NewUserInfoCache(redis)
// initialize services
authService := services.NewAuthService(userRepo, passwordHasher, jwtManager, tokenRevoker, userInfoCache, &cfg.Server)
userService := services.NewUserService(userRepo, passwordHasher, userInfoCache)
postService := services.NewPostService(postRepo)
commentService := services.NewCommentService(commentRepo)
listService := services.NewListService(listRepo)
linkService := services.NewLinkService(linkRepo)
tagService := services.NewTagService(tagRepo)
followService := services.NewFollowService(followRepo)
likeService := services.NewLikeService(likeRepo)
savePostService := services.NewSavePostService(savePostRepo, listRepo)
saveListService := services.NewSaveListService(saveListRepo)
postTagsService := services.NewPostTagsService(postTagsRepo, postRepo)
// initialize handlers
authHandler := handlers.NewAuthHandler(authService)
userHandler := handlers.NewUserHandler(userService)
postHandler := handlers.NewPostHandler(postService)
commentHandler := handlers.NewCommentHandler(commentService)
listHandler := handlers.NewListHandler(listService)
linkHandler := handlers.NewLinkHandler(linkService)
tagHandler := handlers.NewTagHandler(tagService)
followHandler := handlers.NewFollowHandler(followService)
likeHandler := handlers.NewLikeHandler(likeService)
savePostHandler := handlers.NewSavePostHandler(savePostService)
saveListHandler := handlers.NewSaveListHandler(saveListService)
postTagsHandler := handlers.NewPostTagsHandler(postTagsService)
// middlewares
authMiddleware := middlewares.NewAuthenticationMiddleware(jwtManager, userInfoCache)
return &DependencyContainer{
UserRepository: userRepo,
PostRepository: postRepo,
CommentRepository: commentRepo,
ListRepository: listRepo,
LinkRepository: linkRepo,
TagRepository: tagRepo,
FollowRepository: followRepo,
LikeRepository: likeRepo,
SavePostRepository: savePostRepo,
SaveListRepository: saveListRepo,
PostTagsRepository: postTagsRepo,
JwtManager: jwtManager,
PasswordHasher: passwordHasher,
TokenRevoker: tokenRevoker,
UserInfoCache: userInfoCache,
AuthService: authService,
UserService: userService,
PostService: postService,
CommentService: commentService,
ListService: listService,
LinkService: linkService,
TagService: tagService,
FollowService: followService,
LikeService: likeService,
SavePostService: savePostService,
SaveListService: saveListService,
PostTagsService: postTagsService,
AuthHandler: authHandler,
UserHandler: userHandler,
PostHandler: postHandler,
CommentHandler: commentHandler,
ListHandler: listHandler,
LinkHandler: linkHandler,
TagHandler: tagHandler,
FollowHandler: followHandler,
LikeHandler: likeHandler,
SavePostHandler: savePostHandler,
SaveListHandler: saveListHandler,
PostTagsHandler: postTagsHandler,
AuthMiddleware: authMiddleware,
AccessControlMiddleware: middlewares.AccessControlMiddleware,
}
}