- Add TestMode, RateLimitEnabled, RequestsPerMinute to Config - Add getEnvBool() and getEnvInt() helper functions - Update rate limiter to support enabled/disabled state - Pass test environment variables through docker-compose - Configure rate limiter dynamically in main.go This allows disabling rate limiting for integration testing while maintaining security in production environments.
70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
type Config struct {
|
|
ServerPort string
|
|
JWTSecret string
|
|
UploadPath string
|
|
DatabaseHost string
|
|
DatabasePort string
|
|
DatabaseUser string
|
|
DatabasePassword string
|
|
DatabaseName string
|
|
TestMode bool
|
|
RateLimitEnabled bool
|
|
RequestsPerMinute int
|
|
}
|
|
|
|
func LoadConfig() *Config {
|
|
return &Config{
|
|
ServerPort: getEnv("SERVER_PORT", "8080"),
|
|
DatabaseHost: getEnv("DATABASE_HOST", "localhost"),
|
|
DatabasePort: getEnv("DATABASE_PORT", "5432"),
|
|
DatabaseUser: getEnv("DATABASE_USER", "postgres"),
|
|
DatabasePassword: getEnv("DATABASE_PASSWORD", "password"),
|
|
DatabaseName: getEnv("DATABASE_NAME", "bookmann"),
|
|
JWTSecret: getEnv("JWT_SECRET", "your-secret-key"),
|
|
UploadPath: getEnv("UPLOAD_PATH", "./uploads"),
|
|
TestMode: getEnvBool("TEST_MODE", false),
|
|
RateLimitEnabled: getEnvBool("RATE_LIMIT_ENABLED", true),
|
|
RequestsPerMinute: getEnvInt("REQUESTS_PER_MINUTE", 10),
|
|
}
|
|
}
|
|
|
|
func (c *Config) DatabaseURL() string {
|
|
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
|
|
c.DatabaseUser, c.DatabasePassword, c.DatabaseHost, c.DatabasePort, c.DatabaseName)
|
|
}
|
|
|
|
func getEnv(key, defaultValue string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvBool(key string, defaultValue bool) bool {
|
|
if value := os.Getenv(key); value != "" {
|
|
boolVal, err := strconv.ParseBool(value)
|
|
if err == nil {
|
|
return boolVal
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvInt(key string, defaultValue int) int {
|
|
if value := os.Getenv(key); value != "" {
|
|
intVal, err := strconv.Atoi(value)
|
|
if err == nil {
|
|
return intVal
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|