micrach/config/config.go

75 lines
1.4 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 getAppConfig() AppConfig {
env := os.Getenv("ENV")
if env == "" {
env = "release"
}
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))
2021-08-26 16:16:50 +03:00
}
2021-09-08 15:36:13 +03:00
seedDbString := os.Getenv("SEED_DB")
seedDb := seedDbString == "true"
2021-09-08 18:54:46 +03:00
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))
}
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 := os.Getenv("POSTGRES_URL")
if url == "" {
2021-08-26 17:32:19 +03:00
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()
}