90 lines
1.5 KiB
Go
90 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/joho/godotenv"
|
|
"os"
|
|
)
|
|
|
|
type DbConfig struct {
|
|
DBUser string
|
|
DBPass string
|
|
DBHost string
|
|
DBPort string
|
|
DBName string
|
|
}
|
|
type LoginConfig struct {
|
|
LoginAPI string
|
|
AuthMeAPI string
|
|
LogoutAPI string
|
|
}
|
|
|
|
type TokenConfig struct {
|
|
PassiveAssetIssuer string
|
|
JwtSignatureKey []byte
|
|
}
|
|
|
|
type ApiConfig struct {
|
|
ApiPort string
|
|
}
|
|
|
|
type TokenApi struct {
|
|
TokenApiKey string
|
|
}
|
|
|
|
type Config struct {
|
|
DbConfig
|
|
TokenConfig
|
|
TokenApi
|
|
ApiConfig
|
|
LoginConfig
|
|
}
|
|
|
|
func (c *Config) readConfig() error {
|
|
if err := godotenv.Load(); err != nil {
|
|
|
|
return errors.New("failed to load environment variables")
|
|
}
|
|
|
|
c.DbConfig = DbConfig{
|
|
DBHost: os.Getenv("DB_HOST"),
|
|
DBPort: os.Getenv("DB_PORT"),
|
|
DBName: os.Getenv("DB_NAME"),
|
|
DBUser: os.Getenv("DB_USER"),
|
|
DBPass: os.Getenv("DB_PASSWORD"),
|
|
}
|
|
|
|
c.ApiConfig = ApiConfig{
|
|
ApiPort: os.Getenv("API_PORT"),
|
|
}
|
|
|
|
c.TokenConfig = TokenConfig{
|
|
PassiveAssetIssuer: os.Getenv("PASSIVE_ASSET_ISSUER"),
|
|
JwtSignatureKey: []byte(os.Getenv("TOKEN_KEY")),
|
|
}
|
|
|
|
c.TokenApi = TokenApi{
|
|
TokenApiKey: os.Getenv("TOKEN_API_KEY"),
|
|
}
|
|
|
|
c.LoginConfig = LoginConfig{
|
|
LoginAPI: os.Getenv("API_LOGIN_URL"),
|
|
AuthMeAPI: os.Getenv("API_ME_URL"),
|
|
LogoutAPI: os.Getenv("API_LOGOUT_URL"),
|
|
}
|
|
|
|
if c.ApiConfig.ApiPort == "" {
|
|
return errors.New("failed to read environment variables")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func NewConfig() (*Config, error) {
|
|
config := &Config{}
|
|
err := config.readConfig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return config, nil
|
|
} |