76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
// Path: Backend/internal/config/config.go
|
|
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
AppName string
|
|
Environment string
|
|
LogLevel string
|
|
WebPort string
|
|
APIPort string
|
|
PostgresURL string
|
|
ValkeyURL string
|
|
ShutdownTimeout time.Duration
|
|
}
|
|
|
|
func Load() *Config {
|
|
return &Config{
|
|
AppName: getEnv("APP_NAME", "moku"),
|
|
Environment: getEnv("GO_ENV", "development"),
|
|
LogLevel: getEnv("LOG_LEVEL", "debug"),
|
|
WebPort: getEnv("BACKEND_WEB_PORT", "8080"),
|
|
APIPort: getEnv("BACKEND_API_PORT", "8081"),
|
|
PostgresURL: getEnv("DATABASE_URL", "postgres://moku:moku_dev_password@localhost:5432/moku?sslmode=disable"),
|
|
ValkeyURL: getEnv("VALKEY_URL", "redis://localhost:6379/0"),
|
|
ShutdownTimeout: getDurationEnv("BACKEND_SHUTDOWN_TIMEOUT", 10*time.Second),
|
|
}
|
|
}
|
|
|
|
func (c *Config) Address(serviceName string) string {
|
|
var port string
|
|
|
|
switch strings.ToLower(serviceName) {
|
|
case "web":
|
|
port = c.WebPort
|
|
case "api":
|
|
port = c.APIPort
|
|
default:
|
|
port = c.WebPort
|
|
}
|
|
|
|
return fmt.Sprintf(":%s", port)
|
|
}
|
|
|
|
func (c *Config) IsDevelopment() bool {
|
|
return strings.EqualFold(c.Environment, "development")
|
|
}
|
|
|
|
func getEnv(key, fallback string) string {
|
|
if value, exists := os.LookupEnv(key); exists {
|
|
return value
|
|
}
|
|
|
|
return fallback
|
|
}
|
|
|
|
func getDurationEnv(key string, fallback time.Duration) time.Duration {
|
|
value, exists := os.LookupEnv(key)
|
|
if !exists {
|
|
return fallback
|
|
}
|
|
|
|
parsed, err := time.ParseDuration(value)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
|
|
return parsed
|
|
}
|