aboutsummaryrefslogtreecommitdiff
path: root/internal/config/config.go
blob: 5e6e467adefb81a075bb0564fbe2c2a42098c229 (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package config

import (
	"errors"
	"fmt"
	"os"
	"runtime"
	"strconv"
	"strings"
	"time"

	"gopkg.in/yaml.v3"
)

const (
	ModeResolver = "resolver"
	ModeMirror   = "mirror"

	VerifyFull      = "full"
	VerifyLazy      = "lazy"
	VerifyStateOnly = "state-only"
)

type Config struct {
	Mode                 string        `yaml:"mode"`
	DataDir              string        `yaml:"data_dir"`
	PLCSource            string        `yaml:"plc_source"`
	VerifyPolicy         string        `yaml:"verify"`
	ZstdLevel            int           `yaml:"zstd_level"`
	BlockSizeMB          int           `yaml:"block_size_mb"`
	CheckpointInterval   uint64        `yaml:"checkpoint_interval"`
	CommitBatchSize      int           `yaml:"commit_batch_size"`
	VerifyWorkers        int           `yaml:"verify_workers"`
	ListenAddr           string        `yaml:"listen_addr"`
	MirrorPrivateKeyPath string        `yaml:"mirror_private_key_path"`
	PollInterval         time.Duration `yaml:"poll_interval"`
}

func Default() Config {
	return Config{
		Mode:                 ModeMirror,
		DataDir:              "./data",
		PLCSource:            "https://plc.directory",
		VerifyPolicy:         VerifyFull,
		ZstdLevel:            9,
		BlockSizeMB:          8,
		CheckpointInterval:   100000,
		CommitBatchSize:      128,
		VerifyWorkers:        runtime.NumCPU(),
		ListenAddr:           ":8080",
		MirrorPrivateKeyPath: "./mirror.key",
		PollInterval:         5 * time.Second,
	}
}

func Load(path string) (Config, error) {
	cfg := Default()
	if path != "" {
		b, err := os.ReadFile(path)
		if err != nil {
			return Config{}, fmt.Errorf("read config: %w", err)
		}
		if err := yaml.Unmarshal(b, &cfg); err != nil {
			return Config{}, fmt.Errorf("parse config: %w", err)
		}
	}
	applyEnv(&cfg)
	if err := cfg.Validate(); err != nil {
		return Config{}, err
	}
	return cfg, nil
}

func applyEnv(cfg *Config) {
	setString := func(key string, dst *string) {
		if v := strings.TrimSpace(os.Getenv(key)); v != "" {
			*dst = v
		}
	}
	setInt := func(key string, dst *int) {
		if v := strings.TrimSpace(os.Getenv(key)); v != "" {
			if n, err := strconv.Atoi(v); err == nil {
				*dst = n
			}
		}
	}
	setUint64 := func(key string, dst *uint64) {
		if v := strings.TrimSpace(os.Getenv(key)); v != "" {
			if n, err := strconv.ParseUint(v, 10, 64); err == nil {
				*dst = n
			}
		}
	}
	setDuration := func(key string, dst *time.Duration) {
		if v := strings.TrimSpace(os.Getenv(key)); v != "" {
			if d, err := time.ParseDuration(v); err == nil {
				*dst = d
			}
		}
	}

	setString("PLUTIA_MODE", &cfg.Mode)
	setString("PLUTIA_DATA_DIR", &cfg.DataDir)
	setString("PLUTIA_PLC_SOURCE", &cfg.PLCSource)
	setString("PLUTIA_VERIFY", &cfg.VerifyPolicy)
	setInt("PLUTIA_ZSTD_LEVEL", &cfg.ZstdLevel)
	setInt("PLUTIA_BLOCK_SIZE_MB", &cfg.BlockSizeMB)
	setInt("PLUTIA_COMMIT_BATCH_SIZE", &cfg.CommitBatchSize)
	setInt("PLUTIA_VERIFY_WORKERS", &cfg.VerifyWorkers)
	setUint64("PLUTIA_CHECKPOINT_INTERVAL", &cfg.CheckpointInterval)
	setString("PLUTIA_LISTEN_ADDR", &cfg.ListenAddr)
	setString("PLUTIA_MIRROR_PRIVATE_KEY_PATH", &cfg.MirrorPrivateKeyPath)
	setDuration("PLUTIA_POLL_INTERVAL", &cfg.PollInterval)
}

func (c Config) Validate() error {
	if c.Mode != ModeResolver && c.Mode != ModeMirror {
		return fmt.Errorf("invalid mode %q", c.Mode)
	}
	switch c.VerifyPolicy {
	case VerifyFull, VerifyLazy, VerifyStateOnly:
	default:
		return fmt.Errorf("invalid verify policy %q", c.VerifyPolicy)
	}
	if c.DataDir == "" {
		return errors.New("data_dir is required")
	}
	if c.PLCSource == "" {
		return errors.New("plc_source is required")
	}
	if c.ZstdLevel < 1 || c.ZstdLevel > 22 {
		return fmt.Errorf("zstd_level must be between 1 and 22, got %d", c.ZstdLevel)
	}
	if c.BlockSizeMB < 4 || c.BlockSizeMB > 16 {
		return fmt.Errorf("block_size_mb must be between 4 and 16, got %d", c.BlockSizeMB)
	}
	if c.CheckpointInterval == 0 {
		return errors.New("checkpoint_interval must be > 0")
	}
	if c.CommitBatchSize <= 0 || c.CommitBatchSize > 4096 {
		return fmt.Errorf("commit_batch_size must be between 1 and 4096, got %d", c.CommitBatchSize)
	}
	if c.VerifyWorkers <= 0 || c.VerifyWorkers > 1024 {
		return fmt.Errorf("verify_workers must be between 1 and 1024, got %d", c.VerifyWorkers)
	}
	if c.ListenAddr == "" {
		return errors.New("listen_addr is required")
	}
	if c.PollInterval <= 0 {
		return errors.New("poll_interval must be > 0")
	}
	return nil
}