Refactored config system again
All checks were successful
Build (artifact) / build (push) Successful in 27s
All checks were successful
Build (artifact) / build (push) Successful in 27s
This commit is contained in:
124
app/config.go
124
app/config.go
@@ -10,7 +10,6 @@ import (
|
||||
"sync"
|
||||
|
||||
"git.nevets.tech/Steven/certman/common"
|
||||
pb "git.nevets.tech/Steven/certman/proto/v1"
|
||||
"github.com/google/uuid"
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"github.com/spf13/viper"
|
||||
@@ -21,42 +20,46 @@ var (
|
||||
ErrConfigNotFound = errors.New("config file not found")
|
||||
)
|
||||
|
||||
type DomainConfigStore struct {
|
||||
// ---------------------------------------------------------------------------
|
||||
// Generic domain config store
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type DomainConfigStore[T any] struct {
|
||||
mu sync.RWMutex
|
||||
configs map[string]*common.DomainConfig
|
||||
configs map[string]*T
|
||||
}
|
||||
|
||||
func NewDomainConfigStore() *DomainConfigStore {
|
||||
return &DomainConfigStore{
|
||||
configs: make(map[string]*common.DomainConfig),
|
||||
func NewDomainConfigStore[T any]() *DomainConfigStore[T] {
|
||||
return &DomainConfigStore[T]{
|
||||
configs: make(map[string]*T),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DomainConfigStore) Get(domain string) (*common.DomainConfig, bool) {
|
||||
func (s *DomainConfigStore[T]) Get(domain string) (*T, bool) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
v, ok := s.configs[domain]
|
||||
return v, ok
|
||||
}
|
||||
|
||||
func (s *DomainConfigStore) Set(domain string, v *common.DomainConfig) {
|
||||
func (s *DomainConfigStore[T]) Set(domain string, v *T) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.configs[domain] = v
|
||||
}
|
||||
|
||||
// Swap atomically replaces the entire config map (used during reload).
|
||||
func (s *DomainConfigStore) Swap(newConfigs map[string]*common.DomainConfig) {
|
||||
func (s *DomainConfigStore[T]) Swap(newConfigs map[string]*T) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.configs = newConfigs
|
||||
}
|
||||
|
||||
// Snapshot returns a shallow copy safe to iterate without holding the lock.
|
||||
func (s *DomainConfigStore) Snapshot() map[string]*common.DomainConfig {
|
||||
func (s *DomainConfigStore[T]) Snapshot() map[string]*T {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
snap := make(map[string]*common.DomainConfig, len(s.configs))
|
||||
snap := make(map[string]*T, len(s.configs))
|
||||
for k, v := range s.configs {
|
||||
snap[k] = v
|
||||
}
|
||||
@@ -68,9 +71,10 @@ func (s *DomainConfigStore) Snapshot() map[string]*common.DomainConfig {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
var (
|
||||
config *common.AppConfig
|
||||
configMu sync.RWMutex
|
||||
domainStore = NewDomainConfigStore()
|
||||
config *common.AppConfig
|
||||
configMu sync.RWMutex
|
||||
serverDomainStore = NewDomainConfigStore[common.ServerDomainConfig]()
|
||||
clientDomainStore = NewDomainConfigStore[common.ClientDomainConfig]()
|
||||
)
|
||||
|
||||
func Config() *common.AppConfig {
|
||||
@@ -79,8 +83,12 @@ func Config() *common.AppConfig {
|
||||
return config
|
||||
}
|
||||
|
||||
func DomainStore() *DomainConfigStore {
|
||||
return domainStore
|
||||
func ServerDomainStore() *DomainConfigStore[common.ServerDomainConfig] {
|
||||
return serverDomainStore
|
||||
}
|
||||
|
||||
func ClientDomainStore() *DomainConfigStore[common.ClientDomainConfig] {
|
||||
return clientDomainStore
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -110,15 +118,16 @@ func LoadConfig() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadDomainConfigs reads every .conf file in the domains directory.
|
||||
func LoadDomainConfigs() error {
|
||||
// loadDomainConfigs walks /etc/certman/domains and unmarshals every .conf file
|
||||
// into T, keyed by domain.domain_name.
|
||||
func loadDomainConfigs[T any]() (map[string]*T, error) {
|
||||
dir := "/etc/certman/domains/"
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading domain config dir: %w", err)
|
||||
return nil, fmt.Errorf("reading domain config dir: %w", err)
|
||||
}
|
||||
|
||||
temp := make(map[string]*common.DomainConfig)
|
||||
out := make(map[string]*T)
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || filepath.Ext(entry.Name()) != ".conf" {
|
||||
@@ -131,26 +140,47 @@ func LoadDomainConfigs() error {
|
||||
v.SetConfigType("toml")
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return fmt.Errorf("loading %s: %w", path, err)
|
||||
return nil, fmt.Errorf("loading %s: %w", path, err)
|
||||
}
|
||||
|
||||
domain := v.GetString("domain.domain_name")
|
||||
if domain == "" {
|
||||
return fmt.Errorf("%s: missing domain.domain_name", path)
|
||||
return nil, fmt.Errorf("%s: missing domain.domain_name", path)
|
||||
}
|
||||
|
||||
if _, exists := temp[domain]; exists {
|
||||
if _, exists := out[domain]; exists {
|
||||
fmt.Printf("Duplicate domain in %s, skipping...\n", path)
|
||||
continue
|
||||
}
|
||||
cfg := &common.DomainConfig{}
|
||||
cfg := new(T)
|
||||
if err = v.Unmarshal(cfg); err != nil {
|
||||
return fmt.Errorf("unmarshaling %s: %w", path, err)
|
||||
return nil, fmt.Errorf("unmarshaling %s: %w", path, err)
|
||||
}
|
||||
temp[domain] = cfg
|
||||
out[domain] = cfg
|
||||
}
|
||||
|
||||
domainStore.Swap(temp)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// LoadServerDomainConfigs reads every .conf file in the domains directory into
|
||||
// the server-typed store.
|
||||
func LoadServerDomainConfigs() error {
|
||||
configs, err := loadDomainConfigs[common.ServerDomainConfig]()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
serverDomainStore.Swap(configs)
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadClientDomainConfigs reads every .conf file in the domains directory into
|
||||
// the client-typed store.
|
||||
func LoadClientDomainConfigs() error {
|
||||
configs, err := loadDomainConfigs[common.ClientDomainConfig]()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
clientDomainStore.Swap(configs)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -170,40 +200,44 @@ func WriteConfig(filePath string, config *common.AppConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func WriteDomainConfig(config *common.DomainConfig) error {
|
||||
buf, err := toml.Marshal(config)
|
||||
func writeDomainConfigFile(domainName string, v any) error {
|
||||
buf, err := toml.Marshal(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshaling domain config: %w", err)
|
||||
}
|
||||
configPath := filepath.Join("/etc/certman/domains", config.Domain.DomainName+".conf")
|
||||
configPath := filepath.Join("/etc/certman/domains", domainName+".conf")
|
||||
if err = os.WriteFile(configPath, buf, 0640); err != nil {
|
||||
return fmt.Errorf("write config file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveDomainConfigs writes every loaded domain config back to disk.
|
||||
func SaveDomainConfigs() error {
|
||||
for _, v := range domainStore.Snapshot() {
|
||||
err := WriteDomainConfig(v)
|
||||
if err != nil {
|
||||
func WriteServerDomainConfig(config *common.ServerDomainConfig) error {
|
||||
return writeDomainConfigFile(config.Domain.DomainName, config)
|
||||
}
|
||||
|
||||
func WriteClientDomainConfig(config *common.ClientDomainConfig) error {
|
||||
return writeDomainConfigFile(config.Domain.DomainName, config)
|
||||
}
|
||||
|
||||
// SaveServerDomainConfigs writes every loaded server domain config back to disk.
|
||||
func SaveServerDomainConfigs() error {
|
||||
for _, v := range serverDomainStore.Snapshot() {
|
||||
if err := WriteServerDomainConfig(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Domain Specific Lookups
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func PostPullHooks(domain string) ([]*pb.Hook, error) {
|
||||
var hooks []*pb.Hook
|
||||
if err := viper.UnmarshalKey("Hooks.PostPull", hooks); err != nil {
|
||||
return nil, err
|
||||
// SaveClientDomainConfigs writes every loaded client domain config back to disk.
|
||||
func SaveClientDomainConfigs() error {
|
||||
for _, v := range clientDomainStore.Snapshot() {
|
||||
if err := WriteClientDomainConfig(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return hooks, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user