Add environment config loading and logging setup

This commit is contained in:
2026-09-21 18:47:58 +02:00
parent abb674aff1
commit 1c9e8afd78
2 changed files with 66 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
package config
import (
"errors"
"os"
)
type Config struct {
DiscordToken string
LavalinkNodeName string
LavalinkAddress string
LavalinkPassword string
LavalinkSecure bool
Debug bool
}
func Load() (Config, error) {
token := os.Getenv("DISCORD_TOKEN")
if token == "" {
return Config{}, errors.New("DISCORD_TOKEN environment variable is required")
}
address := os.Getenv("LAVALINK_ADDRESS")
if address == "" {
return Config{}, errors.New("LAVALINK_ADDRESS environment variable is required")
}
password := os.Getenv("LAVALINK_PASSWORD")
if password == "" {
return Config{}, errors.New("LAVALINK_PASSWORD environment variable is required")
}
cfg := Config{
DiscordToken: token,
LavalinkNodeName: envOrDefault("LAVALINK_NODE_NAME", "main"),
LavalinkAddress: address,
LavalinkPassword: password,
LavalinkSecure: os.Getenv("LAVALINK_SECURE") == "true",
Debug: os.Getenv("DEBUG") == "true",
}
return cfg, nil
}
func envOrDefault(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
+16
View File
@@ -0,0 +1,16 @@
package logging
import (
"log/slog"
"os"
)
func New(debug bool) *slog.Logger {
level := slog.LevelInfo
if debug {
level = slog.LevelDebug
}
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level})
return slog.New(handler)
}