Skip to content

Commit

Permalink
fix review part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Пешков Дмитрий committed Sep 23, 2024
1 parent 8907776 commit fca4882
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 16 deletions.
29 changes: 25 additions & 4 deletions cmd/gophermart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
26 changes: 24 additions & 2 deletions internal/authenticate/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 18 additions & 2 deletions internal/client/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 41 additions & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
6 changes: 5 additions & 1 deletion internal/services/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion internal/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion internal/services/withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
46 changes: 44 additions & 2 deletions internal/workers/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit fca4882

Please sign in to comment.