Skip to content

Commit

Permalink
refactor: ♻️ some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Nov 23, 2023
1 parent d79cd17 commit 08a5c17
Show file tree
Hide file tree
Showing 212 changed files with 2,162 additions and 1,529 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

.cycle/
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ atlas:
@./scripts/atlas-migrate.sh -c gorm-sync -p "./internal/services/catalog_write_service"
@./scripts/atlas-migrate.sh -c apply -p "./internal/services/catalog_write_service" -o "postgres://postgres:postgres@localhost:5432/catalogs_service?sslmode=disable"

.PHONY: cycle-check
cycle-check:
cd internal/services/catalog_write_service && goimportcycle -dot imports.dot dot -Tpng -o cycle/catalog_write_service.png imports.dot
cd internal/services/catalog_write_service && goimportcycle -dot imports.dot dot -Tpng -o cycle/catalog_write_service.png imports.dot
cd internal/services/catalog_read_service && goimportcycle -dot imports.dot dot -Tpng -o cycle/catalog_read_service.png imports.dot
cd internal/pkg && goimportcycle -dot imports.dot dot -Tpng -o cycle/pkg.png imports.dot


#.PHONY: c4
#c4:
# cd tools/c4 && go mod tidy && sh generate.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@
"instrumentationName": "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/test"
},
"eventStoreDbOptions": {
"connectionString": "esdb://localhost:2113?tls=false"
"host": "localhost",
"httpPort": 2113,
"tcpPort": 1113,
"subscription": {
"subscriptionId": "orders-subscription",
"prefix": ["order-"]
}
},
"migrationOptions": {
"host": "localhost",
Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/config/config_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"
"strings"

"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/constants"
typeMapper "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/reflection/type_mappper"

Expand All @@ -16,17 +16,17 @@ import (
"github.com/spf13/viper"
)

func BindConfig[T any](environments ...environemnt.Environment) (T, error) {
func BindConfig[T any](environments ...environment.Environment) (T, error) {
return BindConfigKey[T]("", environments...)
}

func BindConfigKey[T any](
configKey string,
environments ...environemnt.Environment,
environments ...environment.Environment,
) (T, error) {
var configPath string

environment := environemnt.Environment("")
environment := environment.Environment("")
if len(environments) > 0 {
environment = environments[0]
} else {
Expand Down Expand Up @@ -104,7 +104,7 @@ func BindConfigKey[T any](
// error: An error indicating any issues encountered during the search.
func searchForConfigFileDir(
rootDir string,
environment environemnt.Environment,
environment environment.Environment,
) (string, error) {
var result string

Expand Down
12 changes: 6 additions & 6 deletions internal/pkg/config/configfx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"

"go.uber.org/fx"
)
Expand All @@ -10,16 +10,16 @@ import (
// https://uber-go.github.io/fx/modules.html
var Module = fx.Module(
"configfx",
fx.Provide(func() environemnt.Environment {
return environemnt.ConfigAppEnv()
fx.Provide(func() environment.Environment {
return environment.ConfigAppEnv()
}),
)

var ModuleFunc = func(e environemnt.Environment) fx.Option {
var ModuleFunc = func(e environment.Environment) fx.Option {
return fx.Module(
"configfx",
fx.Provide(func() environemnt.Environment {
return environemnt.ConfigAppEnv(e)
fx.Provide(func() environment.Environment {
return environment.ConfigAppEnv(e)
}),
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package environemnt
package environment

import (
"log"
Expand Down
14 changes: 14 additions & 0 deletions internal/pkg/core/cqrs/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cqrs

import "github.com/mehdihadeli/go-mediatr"

// HandlerRegisterer for registering `RequestHandler` to mediatr registry, if handler implements this interface it will be registered automatically
type HandlerRegisterer interface {
RegisterHandler() error
}

// RequestHandlerWithRegisterer for registering `RequestHandler` to mediatr registry and handling `RequestHandler`
type RequestHandlerWithRegisterer[TRequest any, TResponse any] interface {
HandlerRegisterer
mediatr.RequestHandler[TRequest, TResponse]
}
22 changes: 22 additions & 0 deletions internal/pkg/core/cqrs/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cqrs

import (
"fmt"

"go.uber.org/fx"
)

// when we register multiple handlers with output type `mediatr.RequestHandler` we get exception `type already provided`, so we should use tags annotation

// AsHandler annotates the given constructor to state that
// it provides a handler to the "handlers" group.
func AsHandler(handler interface{}, handlerGroupName string) interface{} {
return fx.Annotate(
handler,
fx.As(new(HandlerRegisterer)),
fx.ResultTags(fmt.Sprintf(
`group:"%s"`,
handlerGroupName,
)),
)
}
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions internal/pkg/elasticsearch/elastic_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package elasticsearch

import (
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
typeMapper "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/reflection/type_mappper"

"github.com/iancoleman/strcase"
Expand All @@ -14,6 +14,6 @@ type ElasticOptions struct {
URL string `mapstructure:"url"`
}

func provideConfig(environment environemnt.Environment) (*ElasticOptions, error) {
func provideConfig(environment environment.Environment) (*ElasticOptions, error) {
return config.BindConfigKey[*ElasticOptions](optionName, environment)
}
4 changes: 2 additions & 2 deletions internal/pkg/eventstroredb/config/eventstoredb_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
typeMapper "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/reflection/type_mappper"

"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -49,7 +49,7 @@ type Subscription struct {
SubscriptionId string `mapstructure:"subscriptionId" validate:"required"`
}

func ProvideConfig(environment environemnt.Environment) (*EventStoreDbOptions, error) {
func ProvideConfig(environment environment.Environment) (*EventStoreDbOptions, error) {
optionName := strcase.ToLowerCamel(typeMapper.GetTypeNameByT[EventStoreDbOptions]())
return config.BindConfigKey[*EventStoreDbOptions](optionName, environment)
}
8 changes: 4 additions & 4 deletions internal/pkg/fxapp/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fxapp
import (
"context"

"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/fxapp/contracts"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/logger"

Expand All @@ -17,15 +17,15 @@ type application struct {
options []fx.Option
logger logger.Logger
fxapp *fx.App
environment environemnt.Environment
environment environment.Environment
}

func NewApplication(
providers []interface{},
decorates []interface{},
options []fx.Option,
logger logger.Logger,
env environemnt.Environment,
env environment.Environment,
) contracts.Application {
return &application{
provides: providers,
Expand Down Expand Up @@ -109,6 +109,6 @@ func (a *application) Logger() logger.Logger {
return a.logger
}

func (a *application) Environment() environemnt.Environment {
func (a *application) Environment() environment.Environment {
return a.environment
}
10 changes: 5 additions & 5 deletions internal/pkg/fxapp/application_builder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fxapp

import (
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/fxapp/contracts"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/logger"
loggerConfig "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/logger/config"
Expand All @@ -17,11 +17,11 @@ type applicationBuilder struct {
decorates []interface{}
options []fx.Option
logger logger.Logger
environment environemnt.Environment
environment environment.Environment
}

func NewApplicationBuilder(environments ...environemnt.Environment) contracts.ApplicationBuilder {
env := environemnt.ConfigAppEnv(environments...)
func NewApplicationBuilder(environments ...environment.Environment) contracts.ApplicationBuilder {
env := environment.ConfigAppEnv(environments...)

var logger logger.Logger
logoption, err := loggerConfig.ProvideLogConfig(env)
Expand Down Expand Up @@ -70,6 +70,6 @@ func (a *applicationBuilder) Logger() logger.Logger {
return a.logger
}

func (a *applicationBuilder) Environment() environemnt.Environment {
func (a *applicationBuilder) Environment() environment.Environment {
return a.environment
}
4 changes: 2 additions & 2 deletions internal/pkg/fxapp/contracts/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package contracts
import (
"context"

"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/logger"

"go.uber.org/fx"
Expand All @@ -17,5 +17,5 @@ type Application interface {
Stop(ctx context.Context) error
Wait() <-chan fx.ShutdownSignal
Logger() logger.Logger
Environment() environemnt.Environment
Environment() environment.Environment
}
4 changes: 2 additions & 2 deletions internal/pkg/fxapp/contracts/application_builder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package contracts

import (
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/logger"

"go.uber.org/fx"
Expand All @@ -19,5 +19,5 @@ type ApplicationBuilder interface {
GetDecorates() []interface{}
Options() []fx.Option
Logger() logger.Logger
Environment() environemnt.Environment
Environment() environment.Environment
}
4 changes: 2 additions & 2 deletions internal/pkg/fxapp/test/test_app_fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"

"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/logger"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/logger/zap"

Expand All @@ -19,7 +19,7 @@ func CreateFxTestApp(
invokes []interface{},
options []fx.Option,
logger logger.Logger,
environment environemnt.Environment,
environment environment.Environment,
) *fxtest.App {
var opts []fx.Option

Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/fxapp/test/test_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"os"

"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/constants"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/fxapp/contracts"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/logger"
Expand All @@ -20,7 +20,7 @@ type testApplication struct {
options []fx.Option
invokes []interface{}
logger logger.Logger
env environemnt.Environment
env environment.Environment
tb fxtest.TB
fxtestApp *fxtest.App
}
Expand All @@ -29,7 +29,7 @@ func (a *testApplication) Logger() logger.Logger {
return a.logger
}

func (a *testApplication) Environment() environemnt.Environment {
func (a *testApplication) Environment() environment.Environment {
return a.env
}

Expand All @@ -39,7 +39,7 @@ func NewTestApplication(
decorates []interface{},
options []fx.Option,
logger logger.Logger,
env environemnt.Environment,
env environment.Environment,
) contracts.Application {
return &testApplication{
tb: tb,
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/fxapp/test/test_application_builder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package test

import (
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/fxapp"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/fxapp/contracts"

Expand All @@ -16,7 +16,7 @@ type TestApplicationBuilder struct {
func NewTestApplicationBuilder(tb fxtest.TB) *TestApplicationBuilder {
return &TestApplicationBuilder{
TB: tb,
ApplicationBuilder: fxapp.NewApplicationBuilder(environemnt.Test),
ApplicationBuilder: fxapp.NewApplicationBuilder(environment.Test),
}
}

Expand All @@ -27,7 +27,7 @@ func (a *TestApplicationBuilder) Build() contracts.Application {
a.GetDecorates(),
a.Options(),
a.Logger(),
environemnt.Test,
environment.Test,
)

return app
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/labstack/echo/v4 v4.11.1
github.com/lib/pq v1.10.9
github.com/mcuadros/go-defaults v1.2.0
github.com/mehdihadeli/go-mediatr v1.2.0
github.com/mehdihadeli/go-mediatr v1.3.0
github.com/michaelklishin/rabbit-hole v1.5.0
github.com/mitchellh/mapstructure v1.5.0
github.com/nolleh/caption_json_formatter v0.2.2
Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ github.com/mcuadros/go-defaults v1.2.0 h1:FODb8WSf0uGaY8elWJAkoLL0Ri6AlZ1bFlenk5
github.com/mcuadros/go-defaults v1.2.0/go.mod h1:WEZtHEVIGYVDqkKSWBdWKUVdRyKlMfulPaGDWIVeCWY=
github.com/mehdihadeli/go-mediatr v1.2.0 h1:XkzYF/uGfW4pwPWYAxKyY+mO6l989PbmCQWSYHuCZtU=
github.com/mehdihadeli/go-mediatr v1.2.0/go.mod h1:I9LuExxAKz6cTDJyP/SSDSmkHaPHK1/usnvEXjUY47k=
github.com/mehdihadeli/go-mediatr v1.3.0 h1:hrb5Scp/nsiR3Y62mjZ0Tc5UX/dRJl4nDFkINBEIESA=
github.com/mehdihadeli/go-mediatr v1.3.0/go.mod h1:lsG+hyH+pEOhmZiZl0KPO72BcZiEReF03CBk4GVJB0k=
github.com/michaelklishin/rabbit-hole v1.5.0 h1:Bex27BiFDsijCM9D0ezSHqyy0kehpYHuNKaPqq/a4RM=
github.com/michaelklishin/rabbit-hole v1.5.0/go.mod h1:vvI1uOitYZi0O5HEGXhaWC1XT80Gy+HvFheJ+5Krlhk=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/gorm_postgres/gorm_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environemnt"
"github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/config/environment"
typeMapper "github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg/reflection/type_mappper"

"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -34,6 +34,6 @@ func (h *GormOptions) Dns() string {
return datasource
}

func provideConfig(environment environemnt.Environment) (*GormOptions, error) {
func provideConfig(environment environment.Environment) (*GormOptions, error) {
return config.BindConfigKey[*GormOptions](optionName, environment)
}
Loading

0 comments on commit 08a5c17

Please sign in to comment.