Nik Afiq c581e79434
All checks were successful
CI / test (push) Successful in 5s
CI / build-ha-gateway (push) Successful in 48s
CI / build-discord-bot (push) Successful in 40s
feat: add mTLS support and TLS directory configuration for ha-gateway and discord-bot
2026-04-09 22:34:22 +09:00

49 lines
1.1 KiB
Go

package config
import (
"errors"
"os"
)
// Config holds runtime configuration for the Discord bot process.
type Config struct {
DiscordToken string
GuildID string
HAGatewayAddr string
TLSDir string
OTELEndpoint string
LogLevel string
LogFormat string
}
// Load reads Discord bot configuration from environment variables.
func Load() (*Config, error) {
token := os.Getenv("DISCORD_TOKEN")
if token == "" {
return nil, errors.New("DISCORD_TOKEN is required but not set")
}
addr := os.Getenv("HA_GATEWAY_ADDR")
if addr == "" {
return nil, errors.New("HA_GATEWAY_ADDR is required but not set")
}
return &Config{
DiscordToken: token,
GuildID: os.Getenv("GUILD_ID"),
HAGatewayAddr: addr,
TLSDir: os.Getenv("TLS_DIR"),
OTELEndpoint: os.Getenv("OTEL_ENDPOINT"),
LogLevel: getenvDefault("LOG_LEVEL", "info"),
LogFormat: getenvDefault("LOG_FORMAT", "json"),
}, nil
}
// getenvDefault keeps config loading concise for optional variables with defaults.
func getenvDefault(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}