Files
Datarush/internal/migrate/config/config.go
T
ITQ 6cfa8d0e69 Merge branch 'feature/competition'
* feature/competition:
  add competition service to compose
  remove generated shit from competitions
  lint competition service
  use auth service for validating user in competitions service
  use auth service for validating user in competitions service
  add redis cache to competitions
  rewrite competition service: remove http related things
  rewrite competition service: remove http related things
  fix proto files
  fix issues
  add competition service (it`s broken, I will fix it later)
2025-12-17 15:40:46 +03:00

83 lines
2.3 KiB
Go

package config
import (
"fmt"
"log"
"net"
"os"
"strconv"
"github.com/joho/godotenv"
)
type Config struct {
GRPCPort int
GRPCEnableReflection bool
EnableHTTPHandler bool
HTTPPort int
LogLevel string
DBHost string
DBPort int
DBUser string
DBPassword string
DBName string
RedisURI string
AuthGRPCAddr string
CacheEnabled bool
}
func Load() (*Config, error) {
_ = godotenv.Load()
return &Config{
GRPCPort: mustGetInt("GRPC_PORT", 50051), //nolint:mnd // false-positive
GRPCEnableReflection: mustGetBool("GRPC_ENABLE_REFLECTION", false),
EnableHTTPHandler: mustGetBool("HTTP_HANDLER_ENABLE", false),
HTTPPort: mustGetInt("HTTP_PORT", 8080), //nolint:mnd // false-positive
LogLevel: getEnv("LOG_LEVEL", "info"),
DBHost: getEnv("POSTGRES_HOST", "localhost"),
DBPort: mustGetInt("POSTGRES_PORT", 5432), //nolint:mnd // false-positive
DBUser: getEnv("POSTGRES_USERNAME", "postgres"),
DBPassword: getEnv("POSTGRES_PASSWORD", "postgres"),
DBName: getEnv("POSTGRES_DATABASE", "postgres"),
RedisURI: getEnv("REDIS_URI", "redis://localhost:6379"),
AuthGRPCAddr: getEnv("AUTH_GRPC_ADDR", "localhost:50052"),
CacheEnabled: mustGetBool("CACHE_ENABLED", true),
}, nil
}
func getEnv(key, def string) string {
if val := os.Getenv(key); val != "" {
return val
}
return def
}
func mustGetInt(key string, def int) int {
val := getEnv(key, strconv.Itoa(def))
n, err := strconv.Atoi(val)
if err != nil {
log.Fatalf("invalid int for %s: %v", key, err)
}
return n
}
func mustGetBool(key string, def bool) bool {
val := getEnv(key, strconv.FormatBool(def))
b, err := strconv.ParseBool(val)
if err != nil {
log.Fatalf("invalid bool for %s: %v", key, err)
}
return b
}
func (c Config) BuildPostgresConnStr() string {
return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
c.DBHost, c.DBPort, c.DBUser, c.DBPassword, c.DBName)
}
func (c Config) BuildPostgresDSN() string {
return fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable",
c.DBUser, c.DBPassword, net.JoinHostPort(c.DBHost, strconv.Itoa(c.DBPort)), c.DBName)
}