Files
gitea-notification-hub/internal/config/config.go
T

101 lines
2.2 KiB
Go

package config
import (
"fmt"
"os"
"time"
"gopkg.in/yaml.v3"
)
// Config holds all configuration for the application
type Config struct {
Server ServerConfig `yaml:"server"`
Identity IdentityConfig `yaml:"identity"`
Notification NotificationConfig `yaml:"notification"`
Database DatabaseConfig `yaml:"database"`
Rules RulesConfig `yaml:"rules"`
}
type ServerConfig struct {
Port int `yaml:"port"`
WebhookSecret string `yaml:"webhook_secret"`
}
type IdentityConfig struct {
Provider string `yaml:"provider"`
CacheTTL time.Duration `yaml:"cache_ttl"`
Gitea GiteaConfig `yaml:"gitea"`
}
type GiteaConfig struct {
URL string `yaml:"url"`
Token string `yaml:"token"`
}
type NotificationConfig struct {
Provider string `yaml:"provider"`
Slack SlackConfig `yaml:"slack"`
}
type SlackConfig struct {
BotToken string `yaml:"bot_token"`
DefaultChannel string `yaml:"default_channel"`
}
type DatabaseConfig struct {
Driver string `yaml:"driver"`
DSN string `yaml:"dsn"`
}
type RulesConfig struct {
PR PRRules `yaml:"pr"`
Issue IssueRules `yaml:"issue"`
Comment CommentRules `yaml:"comment"`
}
type PRRules struct {
NotifyOwner bool `yaml:"notify_owner"`
NotifyReviewers bool `yaml:"notify_reviewers"`
NotifyAssignees bool `yaml:"notify_assignees"`
}
type IssueRules struct {
NotifyAssignees bool `yaml:"notify_assignees"`
}
type CommentRules struct {
NotifyMentioned bool `yaml:"notify_mentioned"`
NotifyThreadOwner bool `yaml:"notify_thread_owner"`
NotifyReviewers bool `yaml:"notify_reviewers"`
}
// Load reads configuration from a YAML file
func Load(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading config file: %w", err)
}
// Expand environment variables
data = []byte(os.ExpandEnv(string(data)))
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parsing config file: %w", err)
}
// Set defaults
if cfg.Server.Port == 0 {
cfg.Server.Port = 8080
}
if cfg.Identity.CacheTTL == 0 {
cfg.Identity.CacheTTL = 24 * time.Hour
}
if cfg.Database.Driver == "" {
cfg.Database.Driver = "sqlite"
}
return &cfg, nil
}