diff --git a/cmd/gophermart/main.go b/cmd/gophermart/main.go index d254ef1..d218331 100644 --- a/cmd/gophermart/main.go +++ b/cmd/gophermart/main.go @@ -34,16 +34,37 @@ func main() { orderQueue := make(chan string) defer close(orderQueue) - authService := authenticate.NewAuthenticateService(loggerRes, conf.SecretKey, conf.TokenExp) + authService := authenticate.NewAuthenticateService( + authenticate.WithLogger(loggerRes), + authenticate.WithSecretKey(conf.SecretKey), + authenticate.WithTokenExp(conf.TokenExp), + ) userService := services.NewUserService(dbRes, conf.QueryTimeOut) orderService := services.NewOrderService(orderQueue, dbRes, loggerRes, conf.QueryTimeOut) withdrawalService := services.NewWithdrawalService(dbRes, loggerRes, conf.QueryTimeOut) - orderClient := client.NewOrderClient(loggerRes, conf.AccrualSystemAddress) + orderClient := client.NewOrderClient( + client.WithLogger(loggerRes), + client.WithAccrualSystemAddress(conf.AccrualSystemAddress), + ) - orderWorker := workers.NewOrderWorker(mainCtx, orderService, orderQueue, loggerRes, conf, orderClient) + orderWorker := workers.NewOrderWorker( + workers.WithCtx(mainCtx), + workers.WithOrderService(orderService), + workers.WithOrderQueue(orderQueue), + workers.WithLogger(loggerRes), + workers.WithConfig(conf), + workers.WithOrderClient(orderClient), + ) orderWorker.Run() - appServer := server.NewServer(userService, orderService, withdrawalService, loggerRes, conf.RunAddr, authService) + appServer := server.NewServer( + server.WithUserService(userService), + server.WithOrderService(orderService), + server.WithWithdrawalService(withdrawalService), + server.WithLogger(loggerRes), + server.WithRunAddress(conf.RunAddr), + server.WithAuthService(authService), + ) err = appServer.Start() if errors.Is(err, http.ErrServerClosed) { loggerRes.Error("Server stop") diff --git a/internal/authenticate/authenticate.go b/internal/authenticate/authenticate.go index f52a74b..0f05b1c 100644 --- a/internal/authenticate/authenticate.go +++ b/internal/authenticate/authenticate.go @@ -24,8 +24,30 @@ type Authenticate struct { tokenExp time.Duration } -func NewAuthenticateService(logger *zap.Logger, secretKey string, tokenExp time.Duration) *Authenticate { - return &Authenticate{logger: logger, secretKey: secretKey, tokenExp: tokenExp} +func NewAuthenticateService(opts ...func(*Authenticate)) *Authenticate { + auth := &Authenticate{} + for _, opt := range opts { + opt(auth) + } + return auth +} + +func WithLogger(logger *zap.Logger) func(*Authenticate) { + return func(a *Authenticate) { + a.logger = logger + } +} + +func WithSecretKey(secretKey string) func(*Authenticate) { + return func(a *Authenticate) { + a.secretKey = secretKey + } +} + +func WithTokenExp(tokenExp time.Duration) func(*Authenticate) { + return func(a *Authenticate) { + a.tokenExp = tokenExp + } } func (a *Authenticate) createCookie(UserID uuid.UUID) (http.Cookie, error) { diff --git a/internal/client/order.go b/internal/client/order.go index d8e97a4..6662f2a 100644 --- a/internal/client/order.go +++ b/internal/client/order.go @@ -15,8 +15,24 @@ type OrderClient struct { accrualSystemAddress string } -func NewOrderClient(logger *zap.Logger, accrualSystemAddress string) *OrderClient { - return &OrderClient{logger: logger, accrualSystemAddress: accrualSystemAddress} +func NewOrderClient(opts ...func(*OrderClient)) *OrderClient { + orderClient := &OrderClient{} + for _, opt := range opts { + opt(orderClient) + } + return orderClient +} + +func WithLogger(logger *zap.Logger) func(*OrderClient) { + return func(o *OrderClient) { + o.logger = logger + } +} + +func WithAccrualSystemAddress(accrualSystemAddress string) func(*OrderClient) { + return func(o *OrderClient) { + o.accrualSystemAddress = accrualSystemAddress + } } func (c OrderClient) CheckOrder(OrderNum string) (services.OrderFromAccrual, int, error) { diff --git a/internal/server/server.go b/internal/server/server.go index da728fb..8396ed0 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -19,9 +19,47 @@ type HTTPServer struct { authService *authenticate.Authenticate } -func NewServer(userService *services.UserService, orderService *services.OrderService, withdrawalService *services.WithdrawalService, logger *zap.Logger, runAddress string, authService *authenticate.Authenticate) *HTTPServer { - return &HTTPServer{ - userService: userService, orderService: orderService, withdrawalService: withdrawalService, logger: logger, runAddress: runAddress, authService: authService, +func NewServer(opts ...func(*HTTPServer)) *HTTPServer { + server := &HTTPServer{} + for _, opt := range opts { + opt(server) + } + return server +} + +func WithUserService(userService *services.UserService) func(*HTTPServer) { + return func(h *HTTPServer) { + h.userService = userService + } +} + +func WithOrderService(orderService *services.OrderService) func(*HTTPServer) { + return func(h *HTTPServer) { + h.orderService = orderService + } +} + +func WithWithdrawalService(withdrawalService *services.WithdrawalService) func(*HTTPServer) { + return func(h *HTTPServer) { + h.withdrawalService = withdrawalService + } +} + +func WithLogger(logger *zap.Logger) func(*HTTPServer) { + return func(h *HTTPServer) { + h.logger = logger + } +} + +func WithRunAddress(runAddress string) func(*HTTPServer) { + return func(h *HTTPServer) { + h.runAddress = runAddress + } +} + +func WithAuthService(authService *authenticate.Authenticate) func(*HTTPServer) { + return func(h *HTTPServer) { + h.authService = authService } } diff --git a/internal/services/order.go b/internal/services/order.go index be356fc..822eeaa 100644 --- a/internal/services/order.go +++ b/internal/services/order.go @@ -44,7 +44,11 @@ type OrderService struct { } func NewOrderService(orderQueue chan string, db *sql.DB, logger *zap.Logger, queryTimeOut time.Duration) *OrderService { - return &OrderService{orderQueue: orderQueue, db: db, logger: logger, queryTimeOut: queryTimeOut} + return &OrderService{ + orderQueue: orderQueue, + db: db, logger: logger, + queryTimeOut: queryTimeOut, + } } func (o *OrderService) Add(ctx context.Context, orderNum string) error { diff --git a/internal/services/user.go b/internal/services/user.go index f6c6276..dbb9050 100644 --- a/internal/services/user.go +++ b/internal/services/user.go @@ -40,7 +40,10 @@ type UserService struct { } func NewUserService(db *sql.DB, queryTimeOut time.Duration) *UserService { - return &UserService{db: db, queryTimeOut: queryTimeOut} + return &UserService{ + db: db, + queryTimeOut: queryTimeOut, + } } func (u *UserService) Add(ctx context.Context, input InputDataUser) (uuid.UUID, error) { diff --git a/internal/services/withdrawal.go b/internal/services/withdrawal.go index 6008566..fa911e7 100644 --- a/internal/services/withdrawal.go +++ b/internal/services/withdrawal.go @@ -44,7 +44,11 @@ type WithdrawalService struct { } func NewWithdrawalService(db *sql.DB, logger *zap.Logger, queryTimeOut time.Duration) *WithdrawalService { - return &WithdrawalService{db: db, logger: logger, queryTimeOut: queryTimeOut} + return &WithdrawalService{ + db: db, + logger: logger, + queryTimeOut: queryTimeOut, + } } func (w *WithdrawalService) Add(ctx context.Context, input InputWithdrawal) error { diff --git a/internal/workers/order.go b/internal/workers/order.go index 9154509..bd1a8eb 100644 --- a/internal/workers/order.go +++ b/internal/workers/order.go @@ -19,8 +19,50 @@ type OrderWorker struct { orderClient *client.OrderClient } -func NewOrderWorker(ctx context.Context, orderService *services.OrderService, orderQueue chan string, logger *zap.Logger, conf config.Config, orderClient *client.OrderClient) *OrderWorker { - return &OrderWorker{ctx: ctx, orderService: orderService, orderQueue: orderQueue, logger: logger, conf: conf, orderClient: orderClient} +func NewOrderWorker(opts ...func(*OrderWorker)) *OrderWorker { + orderWorker := &OrderWorker{ + ctx: context.Background(), + } + for _, opt := range opts { + opt(orderWorker) + } + return orderWorker +} + +func WithCtx(ctx context.Context) func(*OrderWorker) { + return func(o *OrderWorker) { + o.ctx = ctx + } +} + +func WithOrderService(orderService *services.OrderService) func(*OrderWorker) { + return func(o *OrderWorker) { + o.orderService = orderService + } +} + +func WithOrderQueue(orderQueue chan string) func(*OrderWorker) { + return func(o *OrderWorker) { + o.orderQueue = orderQueue + } +} + +func WithLogger(logger *zap.Logger) func(*OrderWorker) { + return func(o *OrderWorker) { + o.logger = logger + } +} + +func WithConfig(conf config.Config) func(*OrderWorker) { + return func(o *OrderWorker) { + o.conf = conf + } +} + +func WithOrderClient(orderClient *client.OrderClient) func(*OrderWorker) { + return func(o *OrderWorker) { + o.orderClient = orderClient + } } func (w *OrderWorker) Run() {