Skip to content

Commit

Permalink
cart-api: replacing Gin with Default go1.22 Mux
Browse files Browse the repository at this point in the history
  • Loading branch information
jurabek committed Mar 30, 2024
1 parent 55da493 commit 12d795e
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 1,624 deletions.
2 changes: 1 addition & 1 deletion src/backend/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ services:
- redis
restart: always
ports:
- 5200
- 5200:5200

order-api:
image: restaurant/order-api
Expand Down
75 changes: 22 additions & 53 deletions src/backend/services/cart-api/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import (
"github.com/dnwe/otelsarama"
"github.com/jurabek/cart-api/cmd/config"
"github.com/jurabek/cart-api/internal/database"
"github.com/jurabek/cart-api/internal/docs"
"github.com/jurabek/cart-api/internal/events"
grpcsvc "github.com/jurabek/cart-api/internal/grpc"
"github.com/jurabek/cart-api/internal/handlers"
"github.com/jurabek/cart-api/internal/middlewares"
"github.com/jurabek/cart-api/internal/instrumentation"
pbv1 "github.com/jurabek/cart-api/pb/v1"
"github.com/jurabek/cart-api/pkg/reciever"
"github.com/redis/go-redis/v9"
"github.com/swaggo/swag/example/basic/docs"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"

"github.com/redis/go-redis/extra/redisotel/v9"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
Expand All @@ -41,12 +41,6 @@ import (
"github.com/jurabek/cart-api/internal/repositories"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/gin-gonic/gin"

// swagger embed files // docs is generated by Swag CLI, you have to import it.
swaggerFiles "github.com/swaggo/files" // swagger embed files
ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
)

var (
Expand All @@ -68,35 +62,18 @@ var (
func main() {
ctx := context.Background()
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
gin.SetMode(gin.DebugMode)

basePath, _ := os.LookupEnv("BASE_PATH")
docs.SwaggerInfo.BasePath = basePath

tp, err := initTracer(ctx)
close, err := instrumentation.StartOTEL(ctx)
if err != nil {
log.Fatal().Err(err)
}

mr, err := initMeter(ctx)
if err != nil {
log.Fatal().Err(err)
log.Fatal().Err(err).Msg("Error starting otel")
}

defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down tracer provider: %v", err)
}
if err := mr.Shutdown(context.Background()); err != nil {
log.Printf("Error shutting down meter provider: %v", err)
}
}()
defer close()

handleSigterm()
router := gin.Default()
router.Use(middlewares.RequestMiddleware())
router.Use(otelgin.Middleware("cart-api"))

router := http.NewServeMux()
cfg := config.Init()

redisClient, err := initRedis(cfg.RedisHost)
Expand Down Expand Up @@ -124,30 +101,22 @@ func main() {
go grpcServer(grpcsvc.NewCartGrpcService(cartRepository))

cartHandler := handlers.NewCartHandler(cartRepository)
apiV1 := router.Group(basePath + "/api/v1")
{
cart := apiV1.Group("/cart")
{
cart.POST("", handlers.ErrorHandler(cartHandler.Create))
cart.GET(":id", handlers.ErrorHandler(cartHandler.Get))
cart.DELETE(":id", handlers.ErrorHandler(cartHandler.Delete))
cart.PUT(":id", handlers.ErrorHandler(cartHandler.Update))
cart.POST(":id/item", handlers.ErrorHandler(cartHandler.AddItem)) // adds item or increments quantity by CartID
cart.PUT(":id/item/:itemID", handlers.ErrorHandler(cartHandler.UpdateItem)) // updates line item item_id is ignored
cart.DELETE(":id/item/:itemID", handlers.ErrorHandler(cartHandler.DeleteItem))
}
}

// Home page should be redirected to swagger page
router.GET(basePath+"/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, basePath+"/swagger/index.html")
})
cartBasePath := basePath + "/api/v1/cart"
router.HandleFunc("POST "+cartBasePath, handlers.ErrorHandler(cartHandler.Create))
router.HandleFunc("GET "+cartBasePath+":id", handlers.ErrorHandler(cartHandler.Get))
router.HandleFunc("DELETE "+cartBasePath+":id", handlers.ErrorHandler(cartHandler.Delete))
router.HandleFunc("PUT "+cartBasePath+":id", handlers.ErrorHandler(cartHandler.Update))
router.HandleFunc("POST "+cartBasePath+":id/item", handlers.ErrorHandler(cartHandler.AddItem)) // adds item or increments quantity by CartID
router.HandleFunc("PUT "+cartBasePath+":id/item/:itemID", handlers.ErrorHandler(cartHandler.UpdateItem)) // updates line item item_id is ignored
router.HandleFunc("DELETE "+cartBasePath+":id/item/:itemID", handlers.ErrorHandler(cartHandler.DeleteItem))

otelRouter := otelhttp.NewHandler(router, "server",
otelhttp.WithMessageEvents(otelhttp.ReadEvents, otelhttp.WriteEvents),
)

router.GET(basePath+"/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler,
func(c *ginSwagger.Config) {
c.URL = basePath + "/swagger/doc.json"
}))
_ = router.Run()
log.Info().Msg("Starting server on port 8080...")
log.Fatal().Err(http.ListenAndServe(":5200", otelRouter))
}

func grpcServer(svc pbv1.CartServiceServer) {
Expand Down Expand Up @@ -243,7 +212,7 @@ func initMeter(ctx context.Context) (*sdkmetric.MeterProvider, error) {
return nil, err
}

mp := sdkmetric.NewMeterProvider(
mp := sdkmetric.NewMeterProvider(
sdkmetric.WithResource(res),
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(exp)),
)
Expand Down
41 changes: 11 additions & 30 deletions src/backend/services/cart-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ module github.com/jurabek/cart-api
go 1.22

require (
github.com/gin-gonic/gin v1.9.1
github.com/gomodule/redigo v2.0.0+incompatible
github.com/google/uuid v1.3.1
github.com/google/uuid v1.4.0
github.com/redis/go-redis/v9 v9.0.5
github.com/rs/zerolog v1.31.0
github.com/stretchr/testify v1.8.4
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.2
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.47.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.47.0
go.opentelemetry.io/otel v1.24.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.22.0
Expand All @@ -22,24 +19,25 @@ require (
github.com/IBM/sarama v1.42.1
github.com/dnwe/otelsarama v0.0.0-20231212173111-631a0a53d5d4
github.com/redis/go-redis/extra/redisotel/v9 v9.0.5
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.24.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.24.0
go.opentelemetry.io/otel/sdk/metric v1.24.0
)

require (
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/eapache/go-resiliency v1.4.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
Expand All @@ -50,53 +48,36 @@ require (
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.22.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/arch v0.7.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/spec v0.20.14 // indirect
github.com/go-openapi/swag v0.22.7 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/swaggo/files v1.0.1
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/grpc v1.60.1
google.golang.org/grpc v1.61.1
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 12d795e

Please sign in to comment.