first commit
This commit is contained in:
96
config/config.go
Normal file
96
config/config.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AppEnv string
|
||||
AppPort string
|
||||
AppBaseURL string
|
||||
|
||||
DBHost string
|
||||
DBPort string
|
||||
DBUser string
|
||||
DBPassword string
|
||||
DBName string
|
||||
DBSSLMode string
|
||||
|
||||
JWTSecret string
|
||||
JWTExpiresHours int
|
||||
JWTRefreshSecret string
|
||||
JWTRefreshExpiresHours int
|
||||
|
||||
GoogleClientID string
|
||||
|
||||
RateLimitRequestsPerSecond float64
|
||||
RateLimitBurst int
|
||||
}
|
||||
|
||||
var Cfg *Config
|
||||
|
||||
func Load() *Config {
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Println("info: file .env tidak ditemukan, menggunakan environment variable sistem")
|
||||
}
|
||||
|
||||
expiresHours, err := strconv.Atoi(getEnv("JWT_EXPIRES_HOURS", "1"))
|
||||
if err != nil {
|
||||
expiresHours = 1
|
||||
}
|
||||
|
||||
refreshExpiresHours, err := strconv.Atoi(getEnv("JWT_REFRESH_EXPIRES_HOURS", "168"))
|
||||
if err != nil {
|
||||
refreshExpiresHours = 168
|
||||
}
|
||||
|
||||
rateLimitRPS, err := strconv.ParseFloat(getEnv("RATE_LIMIT_RPS", "5"), 64)
|
||||
if err != nil {
|
||||
rateLimitRPS = 5
|
||||
}
|
||||
|
||||
rateLimitBurst, err := strconv.Atoi(getEnv("RATE_LIMIT_BURST", "10"))
|
||||
if err != nil {
|
||||
rateLimitBurst = 10
|
||||
}
|
||||
|
||||
appPort := getEnv("APP_PORT", "8080")
|
||||
defaultBaseURL := fmt.Sprintf("http://localhost:%s", appPort)
|
||||
|
||||
Cfg = &Config{
|
||||
AppEnv: getEnv("APP_ENV", "development"),
|
||||
AppPort: appPort,
|
||||
AppBaseURL: getEnv("APP_BASE_URL", defaultBaseURL),
|
||||
|
||||
DBHost: getEnv("DB_HOST", "localhost"),
|
||||
DBPort: getEnv("DB_PORT", "5432"),
|
||||
DBUser: getEnv("DB_USER", "postgres"),
|
||||
DBPassword: getEnv("DB_PASSWORD", "root"),
|
||||
DBName: getEnv("DB_NAME", "cardverse"),
|
||||
DBSSLMode: getEnv("DB_SSLMODE", "disable"),
|
||||
|
||||
JWTSecret: getEnv("JWT_SECRET", "secret"),
|
||||
JWTExpiresHours: expiresHours,
|
||||
JWTRefreshSecret: getEnv("JWT_REFRESH_SECRET", "refresh_secret"),
|
||||
JWTRefreshExpiresHours: refreshExpiresHours,
|
||||
|
||||
GoogleClientID: getEnv("GOOGLE_CLIENT_ID", ""),
|
||||
|
||||
RateLimitRequestsPerSecond: rateLimitRPS,
|
||||
RateLimitBurst: rateLimitBurst,
|
||||
}
|
||||
|
||||
return Cfg
|
||||
}
|
||||
|
||||
func getEnv(key, fallback string) string {
|
||||
if value, ok := os.LookupEnv(key); ok && value != "" {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
Reference in New Issue
Block a user