~bigbes/huntsman

ref: 783841b91eafd678cb3895cfcc8dfd89f290ece7 huntsman/internal/config/config.go -rw-r--r-- 2.6 KiB
783841b9 — Eugene Blikh Initial commit: multi-provider search router 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Package config defines the application configuration shape and validation.
package config

import (
	"net/url"
	"time"

	"go.bigb.es/auxilia/culpa"

	"sourcecraft.dev/bigbes/huntsman/internal/domain/search"
)

// Config is the top-level configuration for huntsman.
type Config struct {
	Server ServerConfig `mapstructure:"server"`
	Log    LogConfig    `mapstructure:"log"`
	Search SearchConfig `mapstructure:"search"`
}

// ServerConfig configures the HTTP server.
type ServerConfig struct {
	Host         string        `mapstructure:"host"`
	Port         int           `mapstructure:"port"`
	ReadTimeout  time.Duration `mapstructure:"read_timeout"`
	WriteTimeout time.Duration `mapstructure:"write_timeout"`
	// PublicURL is the externally-visible base URL of this service.
	// It's embedded in OpenSearch description XML so browsers know where to
	// fetch the search template. Example: "https://search.example.com".
	PublicURL string `mapstructure:"public_url"`
}

// LogConfig configures structured logging.
type LogConfig struct {
	Level  string `mapstructure:"level"`
	Format string `mapstructure:"format"`
}

// SearchConfig controls search-routing behavior.
type SearchConfig struct {
	// DefaultProvider is the provider used when no prefix matches.
	// Must be one of the registered provider IDs (see search.AllProviders).
	DefaultProvider string `mapstructure:"default_provider"`
}

// Validate runs hand-written checks against the loaded config. Failures
// carry an "INVALID_CONFIG" code so call sites can distinguish them.
func (c *Config) Validate() error {
	if c.Server.Host == "" {
		return invalid("server.host is required")
	}
	if c.Server.Port < 1 || c.Server.Port > 65535 {
		return invalid("server.port must be between 1 and 65535")
	}
	if c.Server.PublicURL == "" {
		return invalid("server.public_url is required")
	}
	if u, err := url.Parse(c.Server.PublicURL); err != nil || u.Scheme == "" || u.Host == "" {
		return invalid("server.public_url must be an absolute URL")
	}

	switch c.Log.Level {
	case "debug", "info", "warn", "error":
	default:
		return invalid("log.level must be one of debug|info|warn|error")
	}
	switch c.Log.Format {
	case "human", "json":
	default:
		return invalid("log.format must be one of human|json")
	}

	if !knownProvider(c.Search.DefaultProvider) {
		return invalid("search.default_provider must be a known provider id")
	}
	return nil
}

func invalid(msg string) error {
	return culpa.WithCode(culpa.New(msg), "INVALID_CONFIG")
}

func knownProvider(id string) bool {
	for _, p := range search.AllProviders() {
		if p.ID == id {
			return true
		}
	}
	return false
}