[hw-1] implement cart service

This commit is contained in:
Никита Шубин
2025-05-25 15:49:17 +00:00
parent 3d3f10647b
commit 5077f04b0c
28 changed files with 1151 additions and 2 deletions

104
cart/internal/app/app.go Normal file
View File

@@ -0,0 +1,104 @@
package app
import (
"fmt"
"net"
"net/http"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"route256/cart/internal/app/server"
"route256/cart/internal/domain/cart/repository"
"route256/cart/internal/domain/cart/service"
product_service "route256/cart/internal/domain/products/service"
"route256/cart/internal/infra/config"
"route256/cart/internal/infra/http/middlewares"
"route256/cart/internal/infra/http/round_trippers"
)
const (
productsRetryAttemptsDefault = 3
productsInitialDelaySecDefault = 1
)
type App struct {
config *config.Config
server http.Server
}
func NewApp(configPath string) (*App, error) {
c, err := config.LoadConfig(configPath)
if err != nil {
return nil, fmt.Errorf("unable to load config: %w", err)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if c.Service.LogLevel != "" {
level, err := zerolog.ParseLevel(c.Service.LogLevel)
if err != nil {
return nil, fmt.Errorf("unknown log level `%s` provided: %w", c.Service.LogLevel, err)
}
zerolog.SetGlobalLevel(level)
}
log.WithLevel(zerolog.GlobalLevel()).Msgf("using logging level=`%s`", zerolog.GlobalLevel().String())
app := &App{
config: c,
}
app.server.Handler = app.bootstrapHandlers()
return app, nil
}
func (app *App) ListenAndServe() error {
address := fmt.Sprintf("%s:%s", app.config.Service.Host, app.config.Service.Port)
l, err := net.Listen("tcp", address)
if err != nil {
return err
}
log.Info().Msgf("Serving cart at http://%s", l.Addr())
return app.server.Serve(l)
}
func (app *App) bootstrapHandlers() http.Handler {
transport := http.DefaultTransport
transport = round_trippers.NewLogRoundTripper(transport)
transport = round_trippers.NewRetryRoundTripper(transport, productsRetryAttemptsDefault, productsInitialDelaySecDefault)
httpClient := http.Client{
Transport: transport,
Timeout: 10 * time.Second,
}
productService := product_service.NewProductService(
httpClient,
app.config.ProductService.Token,
fmt.Sprintf("%s:%s", app.config.ProductService.Host, app.config.ProductService.Port),
)
const userCartCap = 100
cartRepository := repository.NewInMemoryRepository(userCartCap)
cartService := service.NewCartService(cartRepository, productService)
s := server.NewServer(cartService)
mx := http.NewServeMux()
mx.HandleFunc("POST /user/{user_id}/cart/{sku_id}", s.AddItemHandler)
mx.HandleFunc("GET /user/{user_id}/cart", s.GetItemsByUserIDHandler)
mx.HandleFunc("DELETE /user/{user_id}/cart/{sku_id}", s.DeleteItemHandler)
mx.HandleFunc("DELETE /user/{user_id}/cart", s.DeleteItemsByUserIDHandler)
h := middlewares.NewTimerMiddleware(mx)
return h
}