1
0
justguestbook/internal/configuration/configuration.go

46 lines
1.1 KiB
Go
Raw Normal View History

2022-10-19 03:25:43 +04:00
package configuration
import (
"os"
"strings"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
type Configuration struct {
ListenOn string `yaml:"listen_on"`
Owner struct {
Name string `yaml:"name"`
Password string `yaml:"password"`
} `yaml:"owner"`
AnonymousPosterName string `yaml:"anonymous_poster_name"`
PageSize int64 `yaml:"page_size"`
DBPath string `yaml:"db_path"`
CaptchaAddr string `yaml:"captcha_addr"`
}
func LoadConfiguration(path string) (*Configuration, error) {
configFile, err := os.Open(path)
if err != nil {
return nil, errors.Wrap(err, "failed to open configuration file")
}
defer configFile.Close()
config := &Configuration{}
if err := yaml.NewDecoder(configFile).Decode(config); err != nil {
return nil, errors.Wrap(err, "failed to parse configuration file")
}
return config, nil
}
// SplitNetworkAddress splits ListenOn option and returns as two strings
// network type (e.g. tcp, unix, udp) and address:port or /path/to/prog.socket
// to listen on.
func (c *Configuration) SplitNetworkAddress() (string, string) {
s := strings.Split(c.ListenOn, " ")
return s[0], s[1]
}