-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjector.go
86 lines (73 loc) · 2.8 KB
/
injector.go
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
package dependencyinjector
import (
"time"
"github.com/mathcale/goexpert-rate-limiter-challenge/config"
"github.com/mathcale/goexpert-rate-limiter-challenge/internal/infra/database"
"github.com/mathcale/goexpert-rate-limiter-challenge/internal/infra/web"
"github.com/mathcale/goexpert-rate-limiter-challenge/internal/infra/web/handlers"
"github.com/mathcale/goexpert-rate-limiter-challenge/internal/infra/web/middlewares"
"github.com/mathcale/goexpert-rate-limiter-challenge/internal/pkg/logger"
"github.com/mathcale/goexpert-rate-limiter-challenge/internal/pkg/ratelimiter"
ratelimiter_strategies "github.com/mathcale/goexpert-rate-limiter-challenge/internal/pkg/ratelimiter/strategies"
"github.com/mathcale/goexpert-rate-limiter-challenge/internal/pkg/responsehandler"
)
type DependencyInjectorInterface interface {
Inject() (*Dependencies, error)
}
type DependencyInjector struct {
Config *config.Conf
}
type Dependencies struct {
Logger logger.LoggerInterface
ResponseHandler responsehandler.WebResponseHandlerInterface
HelloWebHandler handlers.HelloWebHandlerInterface
RateLimiterMiddleware middlewares.RateLimiterMiddlewareInterface
WebServer web.WebServerInterface
RedisDatabase database.RedisDatabaseInterface
RateLimiter ratelimiter.RateLimiterInterface
RedisLimiterStrategy ratelimiter_strategies.LimiterStrategyInterface
}
func NewDependencyInjector(c *config.Conf) *DependencyInjector {
return &DependencyInjector{
Config: c,
}
}
func (di *DependencyInjector) Inject() (*Dependencies, error) {
logger := logger.NewLogger(di.Config.LogLevel)
responseHandler := responsehandler.NewWebResponseHandler()
redisDB, err := database.NewRedisDatabase(*di.Config, logger.GetLogger())
if err != nil {
return nil, err
}
redisLimiterStrategy := ratelimiter_strategies.NewRedisLimiterStrategy(
redisDB.Client,
logger.GetLogger(),
time.Now,
)
limiter := ratelimiter.NewRateLimiter(
logger,
redisLimiterStrategy,
di.Config.RateLimiterIPMaxRequests,
di.Config.RateLimiterTokenMaxRequests,
di.Config.RateLimiterTimeWindowMilliseconds,
)
helloWebHandler := handlers.NewHelloWebHandler(responseHandler)
rateLimiterMiddleware := middlewares.NewRateLimiterMiddleware(logger, responseHandler, limiter)
webRouter := web.NewWebRouter(helloWebHandler, rateLimiterMiddleware)
webServer := web.NewWebServer(
di.Config.WebServerPort,
logger.GetLogger(),
webRouter.Build(),
webRouter.BuildMiddlewares(),
)
return &Dependencies{
Logger: logger,
ResponseHandler: responseHandler,
HelloWebHandler: helloWebHandler,
RateLimiterMiddleware: rateLimiterMiddleware,
WebServer: webServer,
RedisDatabase: redisDB,
RateLimiter: limiter,
RedisLimiterStrategy: redisLimiterStrategy,
}, nil
}