2021-08-26 16:16:50 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-11-20 20:13:05 +03:00
|
|
|
"log"
|
2021-08-26 16:16:50 +03:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2022-02-28 10:18:44 +03:00
|
|
|
type GatewayConfig struct {
|
|
|
|
Url string
|
|
|
|
ApiKey string
|
|
|
|
BoardId string
|
|
|
|
BoardUrl string
|
|
|
|
BoardDescription string
|
|
|
|
}
|
|
|
|
|
2021-08-26 16:16:50 +03:00
|
|
|
type AppConfig struct {
|
2021-09-08 18:54:46 +03:00
|
|
|
Env string
|
|
|
|
Port int
|
2022-02-06 20:37:20 +03:00
|
|
|
IsDbSeeded bool
|
2021-09-08 18:54:46 +03:00
|
|
|
IsRateLimiterEnabled bool
|
2021-11-20 20:13:05 +03:00
|
|
|
ThreadsMaxCount int
|
2022-01-27 20:23:51 +03:00
|
|
|
ThreadBumpLimit int
|
|
|
|
IsCaptchaActive bool
|
2022-02-28 10:18:44 +03:00
|
|
|
Gateway GatewayConfig
|
2021-08-26 16:16:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type DbConfig struct {
|
|
|
|
Url string
|
|
|
|
}
|
|
|
|
|
2021-11-20 20:32:05 +03:00
|
|
|
func getValueOrDefaultBoolean(value string, defaultValue bool) bool {
|
|
|
|
if value == "" {
|
|
|
|
return defaultValue
|
2021-08-26 16:16:50 +03:00
|
|
|
}
|
2021-11-20 20:32:05 +03:00
|
|
|
return value == "true"
|
|
|
|
}
|
2021-08-26 16:16:50 +03:00
|
|
|
|
2021-11-20 20:32:05 +03:00
|
|
|
func getValueOrDefaultInt(value string, defaultValue int) int {
|
|
|
|
if value == "" {
|
|
|
|
return defaultValue
|
2021-08-26 16:16:50 +03:00
|
|
|
}
|
2021-11-20 20:32:05 +03:00
|
|
|
intValue, err := strconv.Atoi(value)
|
2021-08-26 16:16:50 +03:00
|
|
|
if err != nil {
|
2021-11-20 20:32:05 +03:00
|
|
|
log.Panicln(fmt.Sprintf("Could not parse %s to int", value))
|
2021-08-26 16:16:50 +03:00
|
|
|
}
|
2021-11-20 20:32:05 +03:00
|
|
|
return intValue
|
|
|
|
}
|
2021-08-26 16:16:50 +03:00
|
|
|
|
2021-11-20 20:32:05 +03:00
|
|
|
func getValueOrDefaultString(value string, defaultValue string) string {
|
|
|
|
if value == "" {
|
|
|
|
return defaultValue
|
2021-11-20 20:13:05 +03:00
|
|
|
}
|
2021-11-20 20:32:05 +03:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:18:44 +03:00
|
|
|
func getGatewayConfig() GatewayConfig {
|
|
|
|
url := os.Getenv("GATEWAY_URL")
|
|
|
|
apiKey := os.Getenv("GATEWAY_API_KEY")
|
|
|
|
boardId := os.Getenv("GATEWAY_BOARD_ID")
|
|
|
|
description := os.Getenv("GATEWAY_BOARD_DESCRIPTION")
|
|
|
|
boardUrl := os.Getenv("GATEWAY_BOARD_URL")
|
|
|
|
|
|
|
|
return GatewayConfig{
|
|
|
|
Url: url,
|
|
|
|
ApiKey: apiKey,
|
|
|
|
BoardId: boardId,
|
|
|
|
BoardUrl: boardUrl,
|
|
|
|
BoardDescription: description,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 20:32:05 +03:00
|
|
|
func getAppConfig() AppConfig {
|
|
|
|
env := getValueOrDefaultString(os.Getenv("ENV"), "release")
|
|
|
|
port := getValueOrDefaultInt(os.Getenv("PORT"), 3000)
|
2022-02-06 20:37:20 +03:00
|
|
|
isDbSeeded := getValueOrDefaultBoolean(os.Getenv("IS_DB_SEEDED"), false)
|
2021-11-20 20:32:05 +03:00
|
|
|
isRateLimiterEnabled := getValueOrDefaultBoolean(os.Getenv("IS_RATE_LIMITER_ENABLED"), true)
|
|
|
|
threadsMaxCount := getValueOrDefaultInt(os.Getenv("THREADS_MAX_COUNT"), 50)
|
2022-01-27 20:23:51 +03:00
|
|
|
threadBumpLimit := getValueOrDefaultInt(os.Getenv("THREAD_BUMP_LIMIT"), 500)
|
|
|
|
isCaptchaActive := getValueOrDefaultBoolean(os.Getenv("IS_CAPTCHA_ACTIVE"), true)
|
2022-02-28 10:18:44 +03:00
|
|
|
gateway := getGatewayConfig()
|
2021-11-20 20:13:05 +03:00
|
|
|
|
2021-08-26 16:16:50 +03:00
|
|
|
return AppConfig{
|
2021-09-08 18:54:46 +03:00
|
|
|
Env: env,
|
|
|
|
Port: port,
|
2022-02-06 20:37:20 +03:00
|
|
|
IsDbSeeded: isDbSeeded,
|
2021-09-08 18:54:46 +03:00
|
|
|
IsRateLimiterEnabled: isRateLimiterEnabled,
|
2021-11-20 20:13:05 +03:00
|
|
|
ThreadsMaxCount: threadsMaxCount,
|
2022-01-27 20:23:51 +03:00
|
|
|
ThreadBumpLimit: threadBumpLimit,
|
|
|
|
IsCaptchaActive: isCaptchaActive,
|
2022-02-28 10:18:44 +03:00
|
|
|
Gateway: gateway,
|
2021-08-26 16:16:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDbConfig() DbConfig {
|
2021-11-20 20:32:05 +03:00
|
|
|
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()
|
|
|
|
}
|