fix: add defults for threads max count, also add some helpful method to handle default config values

This commit is contained in:
Yanislav Igonin 2021-11-20 19:32:05 +02:00
parent 783e577311
commit 8c448d11a1
2 changed files with 35 additions and 29 deletions

View File

@ -19,32 +19,37 @@ type DbConfig struct {
Url string Url string
} }
func getValueOrDefaultBoolean(value string, defaultValue bool) bool {
if value == "" {
return defaultValue
}
return value == "true"
}
func getValueOrDefaultInt(value string, defaultValue int) int {
if value == "" {
return defaultValue
}
intValue, err := strconv.Atoi(value)
if err != nil {
log.Panicln(fmt.Sprintf("Could not parse %s to int", value))
}
return intValue
}
func getValueOrDefaultString(value string, defaultValue string) string {
if value == "" {
return defaultValue
}
return value
}
func getAppConfig() AppConfig { func getAppConfig() AppConfig {
env := os.Getenv("ENV") env := getValueOrDefaultString(os.Getenv("ENV"), "release")
if env == "" { port := getValueOrDefaultInt(os.Getenv("PORT"), 3000)
env = "release" seedDb := getValueOrDefaultBoolean(os.Getenv("SEED_DB"), false)
} isRateLimiterEnabled := getValueOrDefaultBoolean(os.Getenv("IS_RATE_LIMITER_ENABLED"), true)
threadsMaxCount := getValueOrDefaultInt(os.Getenv("THREADS_MAX_COUNT"), 50)
portString := os.Getenv("PORT")
if portString == "" {
portString = "3000"
}
port, err := strconv.Atoi(portString)
if err != nil {
log.Panicln(fmt.Sprintf("Could not parse %s to int", portString))
}
seedDbString := os.Getenv("SEED_DB")
seedDb := seedDbString == "true"
isRateLimiterEnabledString := os.Getenv("IS_RATE_LIMITER_ENABLED")
isRateLimiterEnabled := isRateLimiterEnabledString == "true"
threadsMaxCountString := os.Getenv("THREADS_MAX_COUNT")
threadsMaxCount, err := strconv.Atoi(threadsMaxCountString)
if err != nil {
log.Panicln(fmt.Sprintf("Could not parse %s to int", threadsMaxCountString))
}
return AppConfig{ return AppConfig{
Env: env, Env: env,
@ -56,10 +61,8 @@ func getAppConfig() AppConfig {
} }
func getDbConfig() DbConfig { func getDbConfig() DbConfig {
url := os.Getenv("POSTGRES_URL") url := getValueOrDefaultString(os.Getenv("POSTGRES_URL"), "postgresql://localhost/micrach")
if url == "" {
url = "postgresql://localhost/micrach"
}
return DbConfig{ return DbConfig{
Url: url, Url: url,
} }

View File

@ -8,6 +8,9 @@ services:
environment: environment:
ENV: release ENV: release
PORT: ${PORT} PORT: ${PORT}
SEED_DB: ${SEED_DB}
IS_RATE_LIMITER_ENABLED: ${IS_RATE_LIMITER_ENABLED}
THREADS_MAX_COUNT: ${THREADS_MAX_COUNT}
POSTGRES_URL: ${POSTGRES_URL} POSTGRES_URL: ${POSTGRES_URL}
volumes: volumes:
- /root/micrach-go/uploads:/app/uploads - /root/micrach-go/uploads:/app/uploads