69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package config
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
ServerPort string
|
|
BaseURL string
|
|
DBHost string
|
|
DBPort string
|
|
DBUser string
|
|
DBPassword string
|
|
DBName string
|
|
DBSSLMode string
|
|
RedisAddr string
|
|
RedisPassword string
|
|
RedisDB int
|
|
DefaultExpiryHrs int
|
|
APIKey string
|
|
}
|
|
|
|
func Load() *Config {
|
|
if err := godotenv.Load(); err != nil {
|
|
log.Println("No .env file found, using system env vars")
|
|
}
|
|
|
|
redisDB, _ := strconv.Atoi(getEnv("REDIS_DB", "0"))
|
|
expiryHrs, _ := strconv.Atoi(getEnv("DEFAULT_EXPIRY_HOURS", "720"))
|
|
|
|
return &Config{
|
|
ServerPort: getEnv("SERVER_PORT", "9000"),
|
|
BaseURL: getEnv("BASE_URL", "http://localhost:9000"),
|
|
DBHost: getEnv("DB_HOST", "localhost"),
|
|
DBPort: getEnv("DB_PORT", "5432"),
|
|
DBUser: getEnv("DB_USER", "postgres"),
|
|
DBPassword: getEnv("DB_PASSWORD", "SALEH@url-shortner"),
|
|
DBName: getEnv("DB_NAME", "url-shortner"),
|
|
DBSSLMode: getEnv("DB_SSLMODE", "disable"),
|
|
RedisAddr: getEnv("REDIS_ADDR", "localhost:6379"),
|
|
RedisPassword: getEnv("REDIS_PASSWORD", ""),
|
|
RedisDB: redisDB,
|
|
DefaultExpiryHrs: expiryHrs,
|
|
APIKey: getEnv("API_KEY", ""),
|
|
}
|
|
}
|
|
|
|
func getEnv(key, fallback string) string {
|
|
if val, exists := os.LookupEnv(key); exists {
|
|
return val
|
|
}
|
|
|
|
return fallback
|
|
}
|
|
|
|
func (c *Config) DNS() string {
|
|
return "host=" + c.DBHost +
|
|
" port=" + c.DBPort +
|
|
" user=" + c.DBUser +
|
|
" password=" + c.DBPassword +
|
|
" dbname=" + c.DBName +
|
|
" sslmode=" + c.DBSSLMode
|
|
|
|
}
|