87 lines
2.1 KiB
Go
Raw Normal View History

2023-10-08 15:59:42 +07:00
package config
import (
"fmt"
"os"
"sync"
"github.com/spf13/viper"
_ "gopkg.in/yaml.v3"
)
const (
YAML_PATH = "infra/furtuna.%s"
ENV_MODE = "ENV_MODE"
DEFAULT_ENV_MODE = "development"
)
var (
validEnvMode = map[string]struct{}{
"local": {},
"development": {},
"production": {},
}
)
type Config struct {
2024-08-10 23:52:09 +07:00
Server Server `mapstructure:"server"`
Database Database `mapstructure:"postgresql"`
Jwt Jwt `mapstructure:"jwt"`
OSSConfig OSSConfig `mapstructure:"oss"`
Midtrans Midtrans `mapstructure:"midtrans"`
Brevo Brevo `mapstructure:"brevo"`
Email Email `mapstructure:"email"`
Withdraw Withdraw `mapstructure:"withdrawal"`
Discovery Discovery `mapstructure:"discovery"`
Order Order `mapstructure:"order"`
FeatureToggle FeatureToggle `mapstructure:"feature_toggle"`
2024-10-15 11:52:34 +07:00
LinkQu LinkQu `mapstructure:"linkqu"`
2023-10-08 15:59:42 +07:00
}
var (
config *Config
configOnce sync.Once
)
func LoadConfig() *Config {
envMode := os.Getenv(ENV_MODE)
if _, ok := validEnvMode[envMode]; !ok {
envMode = DEFAULT_ENV_MODE // default env mode
}
cfgFilePath := fmt.Sprintf(YAML_PATH, envMode)
configOnce.Do(func() {
v := viper.New()
v.SetConfigType("yaml")
v.AddConfigPath(".")
v.SetConfigName(cfgFilePath)
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("failed to read config file: %s", err))
}
config = &Config{}
if err := v.Unmarshal(config); err != nil {
panic(fmt.Errorf("failed to unmarshal config: %s", err))
}
})
return config
}
func (c *Config) Auth() *AuthConfig {
return &AuthConfig{
jwtTokenSecret: c.Jwt.Token.Secret,
jwtTokenExpiresTTL: c.Jwt.Token.ExpiresTTL,
2024-06-04 02:59:31 +07:00
jwtOrderSecret: c.Jwt.TokenOrder.Secret,
jwtOrderExpiresTTL: c.Jwt.TokenOrder.ExpiresTTL,
2024-07-23 01:36:25 +07:00
jwtSecretResetPassword: JWT{
secret: c.Jwt.TokenResetPassword.Secret,
expireTTL: c.Jwt.TokenResetPassword.ExpiresTTL,
},
2024-07-31 23:02:15 +07:00
jwtWithdraw: JWT{
secret: c.Jwt.TokenWithdraw.Secret,
expireTTL: c.Jwt.TokenWithdraw.ExpiresTTL,
},
2023-10-08 15:59:42 +07:00
}
}