chore: merge backend repo history into monorepo at backend/

git-subtree-dir: backend
git-subtree-mainline: dc11190527
git-subtree-split: acae976622
This commit is contained in:
2026-09-27 10:30:16 +03:30
67 changed files with 6835 additions and 0 deletions
+101
View File
@@ -0,0 +1,101 @@
package config
import (
"os"
"strconv"
"time"
"github.com/joho/godotenv"
)
// Config تمام تنظیمات قابل‌پیکربندی برنامه از طریق متغیرهای محیطی
type Config struct {
AppEnv string
AppPort string
DBHost string
DBPort string
DBUser string
DBPassword string
DBName string
DBSSLMode string
// تنظیمات connection pool؛ زیر بار بالا این‌ها از باز شدن بی‌رویه‌ی اتصال
// (که خود Postgres رو زمین می‌زنه) و از نشتی اتصالات راکد جلوگیری می‌کنن.
DBMaxOpenConns int
DBMaxIdleConns int
DBConnMaxLifetimeMins int
DBConnMaxIdleTimeMins int
JWTAccessSecret string
JWTRefreshSecret string
JWTAccessTTL time.Duration
JWTRefreshTTL time.Duration
OTPExpirySeconds int
OTPLength int
OTPHashSecret string
// اگر پر باشد، در اولین اجرا یک کاربر ادمین با این شماره ساخته می‌شود
AdminBootstrapPhone string
}
// Load فایل .env را (در صورت وجود) می‌خواند و Config را برمی‌گرداند.
// نبودن فایل .env خطا محسوب نمی‌شود (مثلاً در پروداکشن env مستقیم ست می‌شود).
func Load() *Config {
_ = godotenv.Load()
return &Config{
AppEnv: getEnv("APP_ENV", "development"),
AppPort: getEnv("APP_PORT", "8080"),
DBHost: getEnv("DB_HOST", "localhost"),
DBPort: getEnv("DB_PORT", "5432"),
DBUser: getEnv("DB_USER", "postgres"),
DBPassword: getEnv("DB_PASSWORD", "postgres"),
DBName: getEnv("DB_NAME", "dating_app"),
DBSSLMode: getEnv("DB_SSLMODE", "disable"),
DBMaxOpenConns: getEnvInt("DB_MAX_OPEN_CONNS", 25),
DBMaxIdleConns: getEnvInt("DB_MAX_IDLE_CONNS", 10),
DBConnMaxLifetimeMins: getEnvInt("DB_CONN_MAX_LIFETIME_MINUTES", 30),
DBConnMaxIdleTimeMins: getEnvInt("DB_CONN_MAX_IDLE_TIME_MINUTES", 5),
JWTAccessSecret: getEnv("JWT_ACCESS_SECRET", "change-me-access-secret"),
JWTRefreshSecret: getEnv("JWT_REFRESH_SECRET", "change-me-refresh-secret"),
JWTAccessTTL: time.Duration(getEnvInt("JWT_ACCESS_TTL_MINUTES", 60*24)) * time.Minute,
JWTRefreshTTL: time.Duration(getEnvInt("JWT_REFRESH_TTL_DAYS", 30)) * 24 * time.Hour,
OTPExpirySeconds: getEnvInt("OTP_EXPIRY_SECONDS", 180),
OTPLength: getEnvInt("OTP_LENGTH", 4),
OTPHashSecret: getEnv("OTP_HASH_SECRET", "change-me-otp-secret"),
AdminBootstrapPhone: getEnv("ADMIN_BOOTSTRAP_PHONE", ""),
}
}
func (c *Config) DSN() string {
return "host=" + c.DBHost +
" user=" + c.DBUser +
" password=" + c.DBPassword +
" dbname=" + c.DBName +
" port=" + c.DBPort +
" sslmode=" + c.DBSSLMode +
" TimeZone=Asia/Tehran"
}
func getEnv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func getEnvInt(key string, fallback int) int {
if v := os.Getenv(key); v != "" {
if n, err := strconv.Atoi(v); err == nil {
return n
}
}
return fallback
}