91 lines
1.7 KiB
Go
91 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig
|
|
Database DatabaseConfig
|
|
Redis RedisConfig
|
|
JWT JWTConfig
|
|
Google GoogleConfig
|
|
Email EmailConfig
|
|
AWS AWSConfig
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Port string `mapstructure:"port"`
|
|
Mode string `mapstructure:"mode"` // e.g., "debug", "release"
|
|
}
|
|
|
|
type DatabaseConfig struct {
|
|
DSN string
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Addr string
|
|
Password string
|
|
DB int
|
|
}
|
|
|
|
type JWTConfig struct {
|
|
Secret string
|
|
}
|
|
|
|
type GoogleConfig struct {
|
|
ClientID string `mapstructure:"client_id"`
|
|
ClientSecret string `mapstructure:"client_secret"`
|
|
RedirectURL string `mapstructure:"redirect_url"`
|
|
}
|
|
|
|
type EmailConfig struct {
|
|
From string
|
|
// Add SMTP settings here later
|
|
}
|
|
|
|
type AWSConfig struct {
|
|
Region string
|
|
Bucket string
|
|
AccessKey string
|
|
SecretKey string
|
|
Endpoint string // Optional: for MinIO or other S3 compatible
|
|
ForcePathStyle bool
|
|
}
|
|
|
|
func LoadConfig() (*Config, error) {
|
|
v := viper.New()
|
|
|
|
// Set defaults
|
|
v.SetDefault("server.port", "8080")
|
|
v.SetDefault("server.mode", "debug")
|
|
v.SetDefault("redis.db", 0)
|
|
|
|
// Environment variable settings
|
|
v.SetEnvPrefix("APP")
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
v.AutomaticEnv()
|
|
|
|
// Config file settings (optional)
|
|
v.SetConfigName("config")
|
|
v.SetConfigType("yaml")
|
|
v.AddConfigPath(".")
|
|
v.AddConfigPath("./config")
|
|
|
|
if err := v.ReadInConfig(); err != nil {
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
|
return nil, err
|
|
}
|
|
// Config file not found is fine, we rely on env vars or defaults
|
|
}
|
|
|
|
var cfg Config
|
|
if err := v.Unmarshal(&cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|