Skip to content

Commit

Permalink
refactor: ♻️ some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Sep 20, 2023
1 parent 8328644 commit 4c0b50c
Show file tree
Hide file tree
Showing 16 changed files with 284 additions and 51 deletions.
8 changes: 0 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ out/
.idea/
local/data/
portainer-data/
.env
.husky/pre-commit

# Logs
Expand Down Expand Up @@ -138,13 +137,6 @@ web_modules/
# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
Expand Down
60 changes: 37 additions & 23 deletions internal/pkg/config/environemnt/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,7 @@ func ConfigAppEnv(environments ...Environment) Environment {
log.Printf(".env file cannot be found, err: %v", err)
}

// https://articles.wesionary.team/environment-variable-configuration-in-your-golang-project-using-viper-4e8289ef664d
// when we `Set` a viper with string value, we should get it from viper with `viper.GetString`, elsewhere we get empty string
// viper will get it from `os env` or viper `internal registry`
pn := viper.GetString(constants.PROJECT_NAME_ENV)
if pn != "" {
// set root working directory of our app in the viper
// https://stackoverflow.com/a/47785436/581476
wd, _ := os.Getwd()

for !strings.HasSuffix(wd, pn) {
wd = filepath.Dir(wd)
}

absRootDirectory, _ := filepath.Abs(wd)

// when we `Set` a viper with string value, we should get it from viper with `viper.GetString`, elsewhere we get empty string
viper.Set(constants.AppRootPath, absRootDirectory)

configPath := filepath.Join(absRootDirectory, "config")

// when we `Set` a viper with string value, we should get it from viper with `viper.GetString`, elsewhere we get empty string
viper.Set(constants.ConfigPath, configPath)
}
setRootWorkingDirectoryEnvironment()

manualEnv := os.Getenv(constants.AppEnv)

Expand Down Expand Up @@ -121,3 +99,39 @@ func loadEnvFilesRecursive() error {

return errors.New(".env file not found in the project hierarchy")
}

func setRootWorkingDirectoryEnvironment() {
// https://articles.wesionary.team/environment-variable-configuration-in-your-golang-project-using-viper-4e8289ef664d
// when we `Set` a viper with string value, we should get it from viper with `viper.GetString`, elsewhere we get empty string
// viper will get it from `os env` or a .env file
pn := viper.GetString(constants.PROJECT_NAME_ENV)
if pn == "" {
log.Fatalf(
"can't find environment variable `%s` in the system environment variables or a .env file",
constants.PROJECT_NAME_ENV,
)
}

// set root working directory of our app in the viper
// https://stackoverflow.com/a/47785436/581476
wd, _ := os.Getwd()

for !strings.HasSuffix(wd, pn) {
wd = filepath.Dir(wd)
}

absoluteRootWorkingDirectory, _ := filepath.Abs(wd)

// when we `Set` a viper with string value, we should get it from viper with `viper.GetString`, elsewhere we get empty string
viper.Set(constants.AppRootPath, absoluteRootWorkingDirectory)

configPath := viper.GetString(constants.ConfigPath)

// check for existing `CONFIG_PATH` variable in system environment variables - we can set it directly in .env file or system environments
if configPath == "" {
configPath := filepath.Join(absoluteRootWorkingDirectory, "config")

// when we `Set` a viper with string value, we should get it from viper with `viper.GetString`, elsewhere we get empty string
viper.Set(constants.ConfigPath, configPath)
}
}
1 change: 1 addition & 0 deletions internal/pkg/migration/gomigrate/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewGoMigratorPostgres(
config.DBName,
)

// In test environment, ewe need a fix for applying application working directory correctly. we will apply this in our environment setup process in `config/environment` file
migration, err := migrate.New(fmt.Sprintf("file://%s", config.MigrationsDir), datasource)
if err != nil {
return nil, errors.WrapIf(err, "failed to initialize migrator")
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/migration/goose/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (m *goosePostgresMigrator) executeCommand(command migration.CommandType, ve
switch command {
case migration.Up:
if version == 0 {
// In test environment, ewe need a fix for applying application working directory correctly. we will apply this in our environment setup process in `config/environment` file
return goose.Run("up", m.db, m.config.MigrationsDir)
}

Expand Down
1 change: 1 addition & 0 deletions internal/services/catalog_read_service/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJECT_NAME=catalog_read_service
7 changes: 7 additions & 0 deletions internal/services/catalog_read_service/cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/mehdihadeli/go-ecommerce-microservices/internal/services/catalogreadservice/internal/shared/app"

"github.com/pterm/pterm"
"github.com/pterm/pterm/putils"
"github.com/spf13/cobra"
)

Expand All @@ -26,6 +28,11 @@ var rootCmd = &cobra.Command{
// @version 1.0
// @description Catalogs Read-Service Api.
func main() {
pterm.DefaultBigText.WithLetters(
putils.LettersFromStringWithStyle("Catalogs", pterm.FgLightGreen.ToStyle()),
putils.LettersFromStringWithStyle(" Read Service", pterm.FgLightMagenta.ToStyle())).
Render()

err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand Down
19 changes: 12 additions & 7 deletions internal/services/catalog_read_service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ require (
github.com/mehdihadeli/go-ecommerce-microservices/internal/pkg v0.0.0-20230831075934-be8df319f588
github.com/mehdihadeli/go-mediatr v1.1.10
github.com/michaelklishin/rabbit-hole v1.5.0
github.com/pterm/pterm v0.12.69
github.com/redis/go-redis/v9 v9.0.5
github.com/satori/go.uuid v1.2.0
github.com/smartystreets/goconvey v1.8.1
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
github.com/swaggo/echo-swagger v1.4.1
github.com/swaggo/swag v1.16.2
Expand All @@ -31,13 +31,15 @@ require (
)

require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/EventStore/EventStore-Client-Go v1.0.2 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Masterminds/squirrel v1.5.4 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect
github.com/ahmetb/go-linq/v3 v3.2.0 // indirect
github.com/ajg/form v1.5.1 // indirect
Expand All @@ -48,12 +50,11 @@ require (
github.com/caarlos0/env/v8 v8.0.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/containerd/containerd v1.7.5 // indirect
github.com/containerd/continuity v0.4.2 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/cli v24.0.5+incompatible // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.5+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
Expand Down Expand Up @@ -81,14 +82,13 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/imkira/go-interpol v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
Expand All @@ -112,10 +112,12 @@ require (
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/magiconair/properties v1.8.7 // 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.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mcuadros/go-defaults v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -130,7 +132,6 @@ require (
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/runc v1.1.9 // indirect
github.com/openzipkin/zipkin-go v0.4.2 // indirect
github.com/ory/dockertest/v3 v3.10.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -141,6 +142,7 @@ require (
github.com/rabbitmq/amqp091-go v1.8.1 // indirect
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5 // indirect
github.com/redis/go-redis/extra/redisotel/v9 v9.0.5 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
Expand All @@ -149,6 +151,7 @@ require (
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/streadway/amqp v1.1.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
Expand All @@ -168,6 +171,7 @@ require (
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/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
Expand All @@ -190,6 +194,7 @@ require (
golang.org/x/net v0.15.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.13.0 // indirect
Expand Down
Loading

0 comments on commit 4c0b50c

Please sign in to comment.