aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/config/main.go
blob: 496adf123625d2511be0752520991d016eff186a (plain)
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
package config

import (
	"os"

	"github.com/go-playground/validator/v10"
	"github.com/pelletier/go-toml/v2"
)

type MainConfig struct {
	Listen struct {
		Address string `validate:"required,ip"`
		Port    uint16 `validate:"required"`
	}

	Command struct {
		Host   string
		Secret string

		API struct {
			Enable bool
		}

		Web struct {
			Enable bool
		}
	}
}

func ReadMainConfig(filePath string, dst *MainConfig) error {
	config, err := os.ReadFile(filePath)
	if err != nil {
		return err
	}

	if err := toml.Unmarshal(config, dst); err != nil {
		return err
	}
	return nil
}

func ValidateMainConfig(cfg *MainConfig) error {
	validate := validator.New(validator.WithRequiredStructEnabled())
	return validate.Struct(cfg)
}