1
0
dwelling-radio/internal/configuration/configuration.go

45 lines
1.2 KiB
Go

package configuration
import (
"os"
"strings"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
type Configuration struct {
ListenOn string `yaml:"listen_on"`
Icecast struct {
URL string `yaml:"url"`
Playlist string `yaml:"playlist_path"`
} `yaml:"icecast"`
FilelistPath string `yaml:"filelist_path"`
MostListenedSongPath string `yaml:"most_listened_song_file_path"`
ListLastNSongs int `yaml:"list_last_n_songs"`
}
// Load reads a YAML file that stores configuration of a service.
func Load(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 into network type (e.g. tcp, unix,
// udp) and address:port or /path/to/service.socket to listen on.
func (c *Configuration) SplitNetworkAddress() (string, string) {
s := strings.Split(c.ListenOn, " ")
return s[0], s[1]
}