65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
Port string
|
|
Secret string
|
|
Env string
|
|
DatabaseURL string
|
|
AdminEmail string
|
|
AdminPassword string
|
|
AdminName string
|
|
PostgresHost string
|
|
PostgresPort string
|
|
PostgresDB string
|
|
PostgresUser string
|
|
PostgresPass string
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
_ = godotenv.Load()
|
|
|
|
cfg := &Config{
|
|
Port: getEnv("APP_PORT", "8080"),
|
|
Secret: getEnv("APP_SECRET", "dev-secret-change-me"),
|
|
Env: getEnv("APP_ENV", "development"),
|
|
DatabaseURL: os.Getenv("DATABASE_URL"),
|
|
AdminEmail: getEnv("ADMIN_EMAIL", "admin@panel.local"),
|
|
AdminPassword: getEnv("ADMIN_PASSWORD", "Admin123!ChangeMe"),
|
|
AdminName: getEnv("ADMIN_NAME", "Administrator"),
|
|
PostgresHost: getEnv("POSTGRES_HOST", "localhost"),
|
|
PostgresPort: getEnv("POSTGRES_PORT", "5432"),
|
|
PostgresDB: getEnv("POSTGRES_DB", "vpn_panel"),
|
|
PostgresUser: getEnv("POSTGRES_USER", "vpn_admin"),
|
|
PostgresPass: getEnv("POSTGRES_PASSWORD", "VpnDb_Secure_Pass_17"),
|
|
}
|
|
|
|
if cfg.DatabaseURL == "" {
|
|
cfg.DatabaseURL = "postgres://" + cfg.PostgresUser + ":" + cfg.PostgresPass +
|
|
"@" + cfg.PostgresHost + ":" + cfg.PostgresPort + "/" + cfg.PostgresDB + "?sslmode=disable"
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func getEnv(key, fallback string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func (c *Config) PortInt() int {
|
|
n, err := strconv.Atoi(c.Port)
|
|
if err != nil {
|
|
return 8080
|
|
}
|
|
return n
|
|
}
|