107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
DatabaseURL string
|
|
Port string
|
|
StorageDir string
|
|
MaxFileSize int64
|
|
RateLimit float64
|
|
CORSOrigins []string
|
|
GzipLevel int
|
|
MaxConnections int32
|
|
MinConnections int32
|
|
ConnLifetime time.Duration
|
|
ConnIdleTime time.Duration
|
|
HealthCheckPeriod time.Duration
|
|
DefaultStorageLimit int64
|
|
PricePerGBUSD float64
|
|
BitcoinMasterAddress string
|
|
BitcoinNetwork string
|
|
}
|
|
|
|
func Load() *Config {
|
|
return &Config{
|
|
DatabaseURL: getEnv("DATABASE_URL", ""),
|
|
Port: getEnv("PORT", "8080"),
|
|
StorageDir: getEnv("STORAGE_DIR", "storage"),
|
|
MaxFileSize: getEnvInt64("MAX_FILE_SIZE_MB", 100) * 1024 * 1024,
|
|
RateLimit: getEnvFloat64("RATE_LIMIT", 20.0),
|
|
CORSOrigins: getEnvSlice("CORS_ORIGINS", []string{"*"}),
|
|
GzipLevel: getEnvInt("GZIP_LEVEL", 5),
|
|
MaxConnections: getEnvInt32("DB_MAX_CONNECTIONS", 100),
|
|
MinConnections: getEnvInt32("DB_MIN_CONNECTIONS", 10),
|
|
ConnLifetime: getEnvDuration("DB_CONN_LIFETIME", time.Hour),
|
|
ConnIdleTime: getEnvDuration("DB_CONN_IDLE_TIME", time.Minute),
|
|
HealthCheckPeriod: getEnvDuration("DB_HEALTH_CHECK_PERIOD", 5*time.Second),
|
|
DefaultStorageLimit: getEnvInt64("DEFAULT_STORAGE_LIMIT_GB", 1) * 1024 * 1024 * 1024,
|
|
PricePerGBUSD: getEnvFloat64("PRICE_PER_GB_USD", 0.50),
|
|
BitcoinMasterAddress: getEnv("BITCOIN_MASTER_ADDRESS", ""),
|
|
BitcoinNetwork: getEnv("BITCOIN_NETWORK", "mainnet"),
|
|
}
|
|
}
|
|
|
|
func getEnv(key, defaultValue string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvInt(key string, defaultValue int) int {
|
|
if value := os.Getenv(key); value != "" {
|
|
if intValue, err := strconv.Atoi(value); err == nil {
|
|
return intValue
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvInt32(key string, defaultValue int32) int32 {
|
|
if value := os.Getenv(key); value != "" {
|
|
if intValue, err := strconv.ParseInt(value, 10, 32); err == nil {
|
|
return int32(intValue)
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvInt64(key string, defaultValue int64) int64 {
|
|
if value := os.Getenv(key); value != "" {
|
|
if intValue, err := strconv.ParseInt(value, 10, 64); err == nil {
|
|
return intValue
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvFloat64(key string, defaultValue float64) float64 {
|
|
if value := os.Getenv(key); value != "" {
|
|
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
|
|
return floatValue
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvDuration(key string, defaultValue time.Duration) time.Duration {
|
|
if value := os.Getenv(key); value != "" {
|
|
if duration, err := time.ParseDuration(value); err == nil {
|
|
return duration
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func getEnvSlice(key string, defaultValue []string) []string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return []string{value}
|
|
}
|
|
return defaultValue
|
|
}
|