micrach/config/config.go

78 lines
1.6 KiB
Go
Raw Normal View History

2021-08-26 16:16:50 +03:00
package config
import (
"fmt"
"log"
2021-08-26 16:16:50 +03:00
"os"
"strconv"
)
type AppConfig struct {
2021-09-08 18:54:46 +03:00
Env string
Port int
SeedDb bool
IsRateLimiterEnabled bool
ThreadsMaxCount int
2021-08-26 16:16:50 +03:00
}
type DbConfig struct {
Url string
}
func getValueOrDefaultBoolean(value string, defaultValue bool) bool {
if value == "" {
return defaultValue
2021-08-26 16:16:50 +03:00
}
return value == "true"
}
2021-08-26 16:16:50 +03:00
func getValueOrDefaultInt(value string, defaultValue int) int {
if value == "" {
return defaultValue
2021-08-26 16:16:50 +03:00
}
intValue, err := strconv.Atoi(value)
2021-08-26 16:16:50 +03:00
if err != nil {
log.Panicln(fmt.Sprintf("Could not parse %s to int", value))
2021-08-26 16:16:50 +03:00
}
return intValue
}
2021-08-26 16:16:50 +03:00
func getValueOrDefaultString(value string, defaultValue string) string {
if value == "" {
return defaultValue
}
return value
}
func getAppConfig() AppConfig {
env := getValueOrDefaultString(os.Getenv("ENV"), "release")
port := getValueOrDefaultInt(os.Getenv("PORT"), 3000)
seedDb := getValueOrDefaultBoolean(os.Getenv("SEED_DB"), false)
isRateLimiterEnabled := getValueOrDefaultBoolean(os.Getenv("IS_RATE_LIMITER_ENABLED"), true)
threadsMaxCount := getValueOrDefaultInt(os.Getenv("THREADS_MAX_COUNT"), 50)
2021-08-26 16:16:50 +03:00
return AppConfig{
2021-09-08 18:54:46 +03:00
Env: env,
Port: port,
SeedDb: seedDb,
IsRateLimiterEnabled: isRateLimiterEnabled,
ThreadsMaxCount: threadsMaxCount,
2021-08-26 16:16:50 +03:00
}
}
func getDbConfig() DbConfig {
url := getValueOrDefaultString(os.Getenv("POSTGRES_URL"), "postgresql://localhost/micrach")
2021-08-26 16:16:50 +03:00
return DbConfig{
Url: url,
}
}
var App AppConfig
var Db DbConfig
func Init() {
App = getAppConfig()
Db = getDbConfig()
}