Files
updns/internal/config/config.go
Dominik Polakovics adae58b7bc feat: Implement configuration management and DNS provider integration
- Added configuration management using Viper in internal/config/config.go
- Implemented ClientConfig, ServerConfig, TLSConfig, HetznerConfig, UpstreamConfig, and main Config struct.
- Created LoadConfig function to read and validate configuration files.
- Developed Hetzner DNS provider in internal/provider/hetzner/hetzner.go with methods for updating DNS records.
- Added comprehensive unit tests for configuration loading and Hetzner provider functionality.
- Established HTTP server with metrics and update endpoint in internal/server/server.go.
- Implemented request handling, authorization, and error management in the server.
- Created integration tests for the Hetzner provider API interactions.
- Removed legacy dynamic DNS integration tests in favor of the new API-based approach.
2025-04-21 00:45:38 +02:00

63 lines
1.5 KiB
Go

package config
import (
"fmt"
"github.com/spf13/viper"
)
type ClientConfig struct {
Secret string `mapstructure:"secret"`
Exact []string `mapstructure:"exact"`
Wildcard []string `mapstructure:"wildcard"`
}
type ServerConfig struct {
BindAddress string `mapstructure:"bind_address"`
TLS TLSConfig `mapstructure:"tls"`
}
type TLSConfig struct {
Enabled bool `mapstructure:"enabled"`
CertFile string `mapstructure:"cert_file"`
KeyFile string `mapstructure:"key_file"`
}
type HetznerConfig struct {
APIToken string `mapstructure:"api_token"`
}
type UpstreamConfig struct {
Provider string `mapstructure:"provider"`
Hetzner HetznerConfig `mapstructure:"hetzner"`
}
type Config struct {
Server ServerConfig `mapstructure:"server"`
Upstream UpstreamConfig `mapstructure:"upstream"`
Clients map[string]ClientConfig `mapstructure:"clients"`
}
// LoadConfig reads the file at path (yaml, json, toml) into Config and validates it.
func LoadConfig(path string) (*Config, error) {
v := viper.New()
v.SetConfigFile(path)
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("reading config: %w", err)
}
var cfg Config
if err := v.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("parsing config: %w", err)
}
for name, client := range cfg.Clients {
if len(client.Exact) == 0 && len(client.Wildcard) == 0 {
return nil, fmt.Errorf("client %q must have at least one of exact or wildcard", name)
}
}
return &cfg, nil
}