Files
api_tmpl/internal/config/config.go
2026-01-08 01:15:07 +00:00

66 lines
2.3 KiB
Go

package config
import (
"time"
"github.com/spf13/viper"
)
// Config stores all configuration of the application.
// The values are read from environment variables or a config file.
type Config struct {
// Server settings
ServiceName string `mapstructure:"SERVICE_NAME"`
FrontendURL string `mapstructure:"FRONTEND_URL"`
AllowedOrigins []string `mapstructure:"ALLOWED_ORIGINS"`
DBAddr string `mapstructure:"DB_ADDR"`
RedisAddr string `mapstructure:"REDIS_ADDR"`
RedisPassword string `mapstructure:"REDIS_PASSWORD"`
RedisDB int `mapstructure:"REDIS_DB"`
MigrationURL string `mapstructure:"MIGRATIONS_URL"`
HTTPServerAddr string `mapstructure:"HTTP_SERVER_ADDR"`
GrpcServerAddr string `mapstructure:"GRPC_SERVER_ADDR"`
AuthTokenExp time.Duration `mapstructure:"AUTH_TOKEN_EXP"`
RefreshTokenExp time.Duration `mapstructure:"REFRESH_TOKEN_EXP"`
MinTokenSize int `mapstructure:"AUTH_TOKEN_MIN_KEY_SIZE"`
TokenKey string `mapstructure:"AUTH_TOKEN_SECRET"`
TokenISS string `mapstructure:"AUTH_TOKEN_ISS"`
TokenAUD string `mapstructure:"AUTH_TOKEN_AUD"`
EmailSender string `mapstructure:"EMAIL_FROM"`
EmailSenderName string `mapstructure:"EMAIL_FROM_NAME"`
EmailPassword string `mapstructure:"EMAIL_SMTP_PASSWORD"`
Environment string `mapstructure:"ENVIRONMENT"`
}
// LoadConfig reads configuration from file or environment variables.
func LoadConfig(path string) (config Config, err error) {
viper.AddConfigPath(path)
viper.SetConfigName("api")
viper.SetConfigType("env") // could be json, toml, yaml, env, etc.
viper.AutomaticEnv() // read in environment variables that match
err = viper.ReadInConfig()
if err != nil {
return
}
err = viper.Unmarshal(&config)
return
}
// LoadTestConfig reads test configuration from file or environment variables.
func LoadTestConfig(path string) (config Config, err error) {
viper.AddConfigPath(path)
viper.SetConfigName("api_test")
viper.SetConfigType("env") // could be json, toml, yaml, env, etc.
viper.AutomaticEnv() // read in environment variables that match
err = viper.ReadInConfig()
if err != nil {
return
}
err = viper.Unmarshal(&config)
return
}