-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from isd-sgcu/JOH-37/migrate-to-env
[JOH-37] Migrate from Config to Envs
- Loading branch information
Showing
11 changed files
with
225 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
APP_PORT=3004 | ||
APP_ENV=development | ||
APP_SECRET=secret | ||
|
||
DB_URL=postgres://root:root@localhost:5432/johnjud_db | ||
|
||
JWT_SECRET=secret | ||
JWT_EXPIRES_IN=3600 | ||
JWT_REFRESH_TOKEN_TTL=604800 | ||
JWT_ISSUER=issuer | ||
JWT_RESET_TOKEN_TTL=900 | ||
|
||
REDIS_HOST=localhost | ||
REDIS_PORT=6379 | ||
REDIS_PASSWORD= | ||
|
||
AUTH_CLIENT_URL=http://localhost:3000 | ||
|
||
SENDGRID_API_KEY=api_key | ||
SENDGRID_NAME=johnjud | ||
[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,119 @@ | ||
package cfgldr | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type Database struct { | ||
Host string `mapstructure:"host"` | ||
Port int `mapstructure:"port"` | ||
Name string `mapstructure:"name"` | ||
Username string `mapstructure:"username"` | ||
Password string `mapstructure:"password"` | ||
SSL string `mapstructure:"ssl"` | ||
Url string `mapstructure:"URL"` | ||
} | ||
|
||
type App struct { | ||
Port int `mapstructure:"port"` | ||
Debug bool `mapstructure:"debug"` | ||
Secret string `mapstructure:"secret"` | ||
Port int `mapstructure:"PORT"` | ||
Env string `mapstructure:"ENV"` | ||
Secret string `mapstructure:"SECRET"` | ||
} | ||
|
||
type Jwt struct { | ||
Secret string `mapstructure:"secret"` | ||
ExpiresIn int `mapstructure:"expires_in"` | ||
RefreshTokenTTL int `mapstructure:"refresh_token_ttl"` | ||
Issuer string `mapstructure:"issuer"` | ||
ResetTokenTTL int `mapstructure:"reset_token_ttl"` | ||
Secret string `mapstructure:"SECRET"` | ||
ExpiresIn int `mapstructure:"EXPIRES_IN"` | ||
RefreshTokenTTL int `mapstructure:"REFRESH_TOKEN_TTL"` | ||
Issuer string `mapstructure:"ISSUER"` | ||
ResetTokenTTL int `mapstructure:"RESET_TOKEN_TTL"` | ||
} | ||
|
||
type Redis struct { | ||
Host string `mapstructure:"host"` | ||
Port int `mapstructure:"port"` | ||
Password string `mapstructure:"password"` | ||
Host string `mapstructure:"HOST"` | ||
Port int `mapstructure:"PORT"` | ||
Password string `mapstructure:"PASSWORD"` | ||
} | ||
|
||
type Auth struct { | ||
ClientURL string `mapstructure:"client_url"` | ||
ClientURL string `mapstructure:"CLIENT_URL"` | ||
} | ||
|
||
type Sendgrid struct { | ||
ApiKey string `mapstructure:"api_key"` | ||
Name string `mapstructure:"name"` | ||
Address string `mapstructure:"address"` | ||
ApiKey string `mapstructure:"API_KEY"` | ||
Name string `mapstructure:"NAME"` | ||
Address string `mapstructure:"ADDRESS"` | ||
} | ||
|
||
type Config struct { | ||
App App `mapstructure:"app"` | ||
Database Database `mapstructure:"database"` | ||
Jwt Jwt `mapstructure:"jwt"` | ||
Redis Redis `mapstructure:"redis"` | ||
Auth Auth `mapstructure:"auth"` | ||
Sendgrid Sendgrid `mapstructure:"sendgrid"` | ||
App App | ||
Database Database | ||
Jwt Jwt | ||
Redis Redis | ||
Auth Auth | ||
Sendgrid Sendgrid | ||
} | ||
|
||
func LoadConfig() (config *Config, err error) { | ||
viper.AddConfigPath("./config") | ||
viper.SetConfigName("config") | ||
viper.SetConfigType("yaml") | ||
func LoadConfig() (*Config, error) { | ||
dbCfgLdr := viper.New() | ||
dbCfgLdr.SetEnvPrefix("DB") | ||
dbCfgLdr.AutomaticEnv() | ||
dbCfgLdr.AllowEmptyEnv(false) | ||
dbConfig := Database{} | ||
if err := dbCfgLdr.Unmarshal(&dbConfig); err != nil { | ||
return nil, err | ||
} | ||
|
||
appCfgLdr := viper.New() | ||
appCfgLdr.SetEnvPrefix("APP") | ||
appCfgLdr.AutomaticEnv() | ||
appCfgLdr.AllowEmptyEnv(false) | ||
appConfig := App{} | ||
if err := appCfgLdr.Unmarshal(&appConfig); err != nil { | ||
return nil, err | ||
} | ||
|
||
jwtCfgLdr := viper.New() | ||
jwtCfgLdr.SetEnvPrefix("JWT") | ||
jwtCfgLdr.AutomaticEnv() | ||
jwtCfgLdr.AllowEmptyEnv(false) | ||
jwtConfig := Jwt{} | ||
if err := jwtCfgLdr.Unmarshal(&jwtConfig); err != nil { | ||
return nil, err | ||
} | ||
|
||
viper.AutomaticEnv() | ||
redisCfgLdr := viper.New() | ||
redisCfgLdr.SetEnvPrefix("REDIS") | ||
redisCfgLdr.AutomaticEnv() | ||
redisCfgLdr.AllowEmptyEnv(false) | ||
redisConfig := Redis{} | ||
if err := redisCfgLdr.Unmarshal(&redisConfig); err != nil { | ||
return nil, err | ||
} | ||
|
||
authCfgLdr := viper.New() | ||
authCfgLdr.SetEnvPrefix("AUTH") | ||
authCfgLdr.AutomaticEnv() | ||
authCfgLdr.AllowEmptyEnv(false) | ||
authConfig := Auth{} | ||
if err := authCfgLdr.Unmarshal(&authConfig); err != nil { | ||
return nil, err | ||
} | ||
|
||
err = viper.ReadInConfig() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error occurs while reading the config") | ||
sendgridCfgLdr := viper.New() | ||
sendgridCfgLdr.SetEnvPrefix("SENDRID") | ||
sendgridCfgLdr.AutomaticEnv() | ||
sendgridCfgLdr.AllowEmptyEnv(false) | ||
sendgridConfig := Sendgrid{} | ||
if err := sendgridCfgLdr.Unmarshal(&sendgridConfig); err != nil { | ||
return nil, err | ||
} | ||
|
||
err = viper.Unmarshal(&config) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error occurs while unmarshal the config") | ||
config := &Config{ | ||
Database: dbConfig, | ||
App: appConfig, | ||
Jwt: jwtConfig, | ||
Redis: redisConfig, | ||
Auth: authConfig, | ||
Sendgrid: sendgridConfig, | ||
} | ||
|
||
return | ||
return config, nil | ||
} | ||
|
||
func (ac *App) IsDevelopment() bool { | ||
return ac.Env == "development" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
version: "3" | ||
|
||
services: | ||
auth: | ||
container_name: johnjud-auth | ||
restart: unless-stopped | ||
build: . | ||
ports: | ||
- 3002:3002 | ||
environment: | ||
- APP_PORT=3004 | ||
- APP_ENV=production | ||
- APP_SECRET=secret | ||
- DB_URL=postgres://root:root@johnjud-local-db:5432/johnjud_db | ||
- JWT_SECRET=secret | ||
- JWT_EXPIRES_IN=3600 | ||
- JWT_REFRESH_TOKEN_TTL=604800 | ||
- JWT_ISSUER=issuer | ||
- JWT_RESET_TOKEN_TTL=900 | ||
- REDIS_HOST=localhost | ||
- REDIS_PORT=6379 | ||
- REDIS_PASSWORD= | ||
- AUTH_CLIENT_URL=http://localhost:3000 | ||
- SENDGRID_API_KEY=api_key | ||
- SENDGRID_NAME=johnjud | ||
- [email protected] | ||
networks: | ||
- johnjud | ||
|
||
local-db: | ||
image: postgres:15.1-alpine3.17 | ||
container_name: johnjud-local-db | ||
restart: unless-stopped | ||
environment: | ||
POSTGRES_USER: root | ||
POSTGRES_PASSWORD: root | ||
POSTGRES_DB: johnjud_db | ||
volumes: | ||
- postgres:/var/lib/postgresql/data | ||
ports: | ||
- "5432:5432" | ||
networks: | ||
- johnjud | ||
|
||
cache: | ||
image: redis | ||
restart: unless-stopped | ||
ports: | ||
- "6379:6379" | ||
networks: | ||
- johnjud | ||
|
||
volumes: | ||
postgres: | ||
networks: | ||
johnjud: |
Oops, something went wrong.