Path: blob/master/common/config/yamlconfig.go
509 views
package config12import (3"gopkg.in/yaml.v2"4"io/ioutil"5"strings"6)78// ConfigurationYaml is a structure that will be automatically populated with the value of the configuration9type ConfigurationYaml struct {10PortField uint16 `yaml:"port"`11SitesField []*SiteYaml `yaml:"sites"`12IndexField bool `yaml:"index"`13}1415// SiteYaml is a structure that will be automatically populated with the value of the configuration16type SiteYaml struct {17RefField string `yaml:"ref"`18HostField string `yaml:"host"`19DescriptionField string `yaml:"description"`20RedirectsField []string `yaml:"redirects"`21LanguageField string `yaml:"language"`22KeepLinksField bool `yaml:"keeplinks"`23FaviconPathField string `yaml:"faviconpath"`24FrontProxyField *FrontProxyYaml `yaml:"frontproxy"`25}2627// FrontProxyYaml is a structure that will be automatically populated with the value of the configuration28type FrontProxyYaml struct {29ForceSSL bool `yaml:"forcessl"`30IPHeader string `yaml:"ipheader"`31}3233// Port return the port to which the daemon will listen to34func (config *ConfigurationYaml) Port() uint16 { return config.PortField }3536// Sites returns the list of sites37func (config *ConfigurationYaml) Sites() []Site {38sites := make([]Site, len(config.SitesField))3940for i := range config.SitesField {41sites[i] = config.SitesField[i]42}4344return sites45}4647// Index returns true if an index should be displayed when neither a host or a redirect was found48func (config *ConfigurationYaml) Index() bool { return config.IndexField }4950// Ref returns the Google Sites reference of the websites51func (site *SiteYaml) Ref() string { return site.RefField }5253// Host returns the host that is supposed to handle respond for this site54func (site *SiteYaml) Host() string { return site.HostField }5556// Description returns a string describing the content of the site57func (site *SiteYaml) Description() string { return site.DescriptionField }5859// Redirects returns a list of hostname that should redirect their traffic to the host60func (site *SiteYaml) Redirects() []string { return site.RedirectsField }6162// Language returns the HTTP header used to query Google servers63func (site *SiteYaml) Language() string { return site.LanguageField }6465// KeepLinks returns true if links should be kept66func (site *SiteYaml) KeepLinks() bool { return site.KeepLinksField }6768// FaviconPath returns the path to the favicon file. No file is assumed if the string is empty69func (site *SiteYaml) FaviconPath() string { return site.FaviconPathField }7071// GRef returns a consistent reference to the Google Sites instance72func (site *SiteYaml) GRef() string {73if !strings.Contains(site.Ref(), "/") {74return "view/" + site.Ref()75}76return site.Ref()77}7879// IPHeader returns the HTTP header containing the ip of the requester, if empty this information will80// be taken from the connection directly81func (site *SiteYaml) IPHeader() string {82if site.FrontProxyField != nil {83return site.FrontProxyField.IPHeader84}85return ""86}8788// NewYamlConfigLoader returns a loader for a given YAML configuration file89func NewYamlConfigLoader(filename string) ConfigLoader {90return func() (Configuration, error) {91bytes, err := ioutil.ReadFile(filename)92if err != nil {93return nil, err94}9596c := &ConfigurationYaml{}97err = yaml.Unmarshal(bytes, c)98if err != nil {99return nil, err100}101return c, nil102}103}104105106