Skip to content

Commit

Permalink
Merge pull request #12 from meysamhadeli/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
meysamhadeli authored Dec 18, 2022
2 parents edc1066 + 0775a3c commit 76f6996
Show file tree
Hide file tree
Showing 25 changed files with 393 additions and 148 deletions.
2 changes: 2 additions & 0 deletions internal/pkg/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
Expand Down Expand Up @@ -91,6 +92,7 @@ require (
github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/otel/metric v0.32.1 // indirect
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 // indirect
Expand Down
6 changes: 4 additions & 2 deletions internal/pkg/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,9 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-containerregistry v0.5.1/go.mod h1:Ct15B4yir3PLOP5jsy0GNeYVaIZs/MK/Jz5any1wFW0=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
Expand Down Expand Up @@ -913,8 +914,9 @@ github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAh
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/willf/bitset v1.1.11-0.20200630133818-d5bec3311243/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
Expand Down
17 changes: 17 additions & 0 deletions internal/pkg/http/echo/config/echo-config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package config

import (
"fmt"
"net/url"
)

type EchoConfig struct {
Port string `mapstructure:"port" validate:"required"`
Development bool `mapstructure:"development"`
Expand All @@ -9,3 +14,15 @@ type EchoConfig struct {
Timeout int `mapstructure:"timeout"`
Host string `mapstructure:"host"`
}

func (c *EchoConfig) Address() string {
return fmt.Sprintf("%s%s", c.Host, c.Port)
}

func (c *EchoConfig) BasePathAddress() string {
path, err := url.JoinPath(c.Address(), c.BasePath)
if err != nil {
return ""
}
return path
}
6 changes: 6 additions & 0 deletions internal/pkg/http/echo/server/echo-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func (s *EchoServer) ApplyVersioningFromHeader() {
s.Echo.Pre(apiVersion)
}

func (s *EchoServer) RegisterGroupFunc(groupName string, builder func(g *echo.Group)) *EchoServer {
builder(s.Echo.Group(groupName))

return s
}

// APIVersion Header Based Versioning
func apiVersion(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package configurations

import (
"context"
"github.com/labstack/echo/v4"
v1 "github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/features/registering_user/endpoints/v1"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/shared/contracts"
)

func (c *usersModuleConfigurator) configEndpoints(ctx context.Context, group *echo.Group) {
func ConfigEndpoints(ic *contracts.InfrastructureConfiguration, group *echo.Group) {

userEndpointBase := &contracts.IdentityEndpointBase[contracts.InfrastructureConfiguration]{
ProductsGroup: group,
Configuration: *c.InfrastructureConfiguration,
Configuration: *ic,
}

registerUserEndpoint := v1.NewCreteUserEndpoint(userEndpointBase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (ic *infrastructureConfigurator) ConfigInfrastructures(ctx context.Context)

pc := NewUsersModuleConfigurator(infrastructure)

err = pc.ConfigureIdentitiesModule(ctx)
err = ConfigureIdentitiesModule(pc)
if err != nil {
return err, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package configurations

import (
"github.com/mehdihadeli/go-mediatr"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/contracts"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/dtos"
v1 "github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/features/registering_user/commands/v1"
contracts2 "github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/shared/contracts"
)

func (c *usersModuleConfigurator) configUsersMediator(pgRepo contracts.UserRepository) error {
func ConfigUsersMediator(ic *contracts2.InfrastructureConfiguration) error {

//https://stackoverflow.com/questions/72034479/how-to-implement-generic-interfaces
err := mediatr.RegisterRequestHandler[*v1.RegisterUser, *dtos.RegisterUserResponseDto](v1.NewRegisterUserHandler(c.Log, c.Cfg, pgRepo, c.RabbitmqPublisher))
err := mediatr.RegisterRequestHandler[*v1.RegisterUser, *dtos.RegisterUserResponseDto](v1.NewRegisterUserHandler(ic.Log, ic.Cfg, ic.UserRepository, ic.RabbitmqPublisher))
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package configurations

import (
"context"
repositories_imp "github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/data/repositories"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/mappings"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/shared/contracts"
Expand All @@ -15,28 +14,28 @@ type usersModuleConfigurator struct {
*contracts.InfrastructureConfiguration
}

func NewUsersModuleConfigurator(infrastructure *contracts.InfrastructureConfiguration) *usersModuleConfigurator {
return &usersModuleConfigurator{InfrastructureConfiguration: infrastructure}
func NewUsersModuleConfigurator(infrastructure *contracts.InfrastructureConfiguration) *contracts.InfrastructureConfiguration {
return infrastructure
}

func (c *usersModuleConfigurator) ConfigureIdentitiesModule(ctx context.Context) error {
func ConfigureIdentitiesModule(ic *contracts.InfrastructureConfiguration) error {

v1 := c.Echo.Group("/api/v1")
v1 := ic.Echo.Group("/api/v1")
group := v1.Group("/users")

userRepository := repositories_imp.NewPostgresUserRepository(c.Log, c.Cfg, c.Gorm)
ic.UserRepository = repositories_imp.NewPostgresUserRepository(ic.Log, ic.Cfg, ic.Gorm)

err := mappings.ConfigureMappings()
if err != nil {
return err
}

err = c.configUsersMediator(userRepository)
err = ConfigUsersMediator(ic)
if err != nil {
return err
}

c.configEndpoints(ctx, group)
ConfigEndpoints(ic, group)

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

type UserRepository interface {
RegisterUser(ctx context.Context, product *models.User) (*models.User, error)
RegisterUser(ctx context.Context, user *models.User) (*models.User, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ import (
"github.com/jackc/pgx/v4/pgxpool"
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/logger"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/config"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/contracts"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/models"
"github.com/pkg/errors"
"gorm.io/gorm"
)

type postgresUserRepository struct {
type PostgresUserRepository struct {
log logger.ILogger
cfg *config.Config
db *pgxpool.Pool
gorm *gorm.DB
}

func NewPostgresUserRepository(log logger.ILogger, cfg *config.Config, gorm *gorm.DB) *postgresUserRepository {
return &postgresUserRepository{log: log, cfg: cfg, gorm: gorm}
func NewPostgresUserRepository(log logger.ILogger, cfg *config.Config, gorm *gorm.DB) contracts.UserRepository {
return PostgresUserRepository{log: log, cfg: cfg, gorm: gorm}
}

func (p postgresUserRepository) RegisterUser(ctx context.Context, user *models.User) (*models.User, error) {
func (p PostgresUserRepository) RegisterUser(ctx context.Context, user *models.User) (*models.User, error) {

if err := p.gorm.Create(&user).Error; err != nil {
return nil, errors.Wrap(err, "error in the inserting user into the database.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package contracts

import (
"context"
"github.com/go-playground/validator"
"github.com/go-resty/resty/v2"
"github.com/labstack/echo/v4"
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/grpc"
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/logger"
"github.com/meysamhadeli/shop-golang-microservices/internal/pkg/rabbitmq"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/config"
"github.com/meysamhadeli/shop-golang-microservices/internal/services/identity-service/identity/contracts"
"github.com/streadway/amqp"
"go.opentelemetry.io/otel/trace"
"gorm.io/gorm"
Expand All @@ -22,6 +25,9 @@ type InfrastructureConfiguration struct {
JaegerTracer trace.Tracer
Gorm *gorm.DB
Echo *echo.Echo
GrpcClient grpc.GrpcClient
UserRepository contracts.UserRepository
Context context.Context
}

type IdentityEndpointBase[T any] struct {
Expand Down
19 changes: 19 additions & 0 deletions internal/services/product-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/hcsshim v0.9.4 // indirect
github.com/ahmetb/go-linq/v3 v3.2.0 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/containerd/cgroups v1.0.4 // indirect
github.com/containerd/containerd v1.6.8 // indirect
Expand All @@ -41,8 +43,10 @@ require (
github.com/docker/docker v20.10.17+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gavv/httpexpect/v2 v2.8.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.8.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand All @@ -61,10 +65,13 @@ require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/imkira/go-interpol v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
Expand All @@ -77,12 +84,14 @@ require (
github.com/jinzhu/now v1.1.4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/sys/mount v0.3.3 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
Expand All @@ -96,6 +105,8 @@ require (
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sanity-io/litter v1.5.5 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -108,9 +119,16 @@ require (
github.com/uptrace/bun v1.1.8 // indirect
github.com/uptrace/bun/driver/pgdriver v1.1.8 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.34.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.36.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.36.1 // indirect
Expand All @@ -130,4 +148,5 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/postgres v1.3.8 // indirect
mellium.im/sasl v0.3.0 // indirect
moul.io/http2curl/v2 v2.3.0 // indirect
)
Loading

0 comments on commit 76f6996

Please sign in to comment.