All checks were successful
Docker Build / build-and-push-image (push) Successful in 1m8s
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
Bots map[string]BotConfig `yaml:"bots"`
|
|
}
|
|
|
|
type BotConfig struct {
|
|
Handler string `yaml:"handler"`
|
|
Token string `yaml:"-"`
|
|
Secret string `yaml:"-"`
|
|
}
|
|
|
|
// LoadConfig reads the YAML routing config and merges per-bot secrets pulled
|
|
// from the process environment. Per-bot env keys are derived from the bot
|
|
// slug uppercased: BOT_<UPPER_SLUG>_TOKEN, BOT_<UPPER_SLUG>_SECRET.
|
|
func LoadConfig(path string) (*Config, error) {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read config %s: %w", path, err)
|
|
}
|
|
var cfg Config
|
|
if err := yaml.Unmarshal(raw, &cfg); err != nil {
|
|
return nil, fmt.Errorf("parse yaml: %w", err)
|
|
}
|
|
if len(cfg.Bots) == 0 {
|
|
return nil, fmt.Errorf("no bots in %s", path)
|
|
}
|
|
for slug, b := range cfg.Bots {
|
|
envSlug := strings.ToUpper(strings.ReplaceAll(slug, "-", "_"))
|
|
b.Token = os.Getenv("BOT_" + envSlug + "_TOKEN")
|
|
b.Secret = os.Getenv("BOT_" + envSlug + "_SECRET")
|
|
cfg.Bots[slug] = b
|
|
}
|
|
return &cfg, nil
|
|
}
|