Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fever-ch
GitHub Repository: fever-ch/go-google-sites-proxy
Path: blob/master/common/config/yamlconfig.go
509 views
1
package config
2
3
import (
4
"gopkg.in/yaml.v2"
5
"io/ioutil"
6
"strings"
7
)
8
9
// ConfigurationYaml is a structure that will be automatically populated with the value of the configuration
10
type ConfigurationYaml struct {
11
PortField uint16 `yaml:"port"`
12
SitesField []*SiteYaml `yaml:"sites"`
13
IndexField bool `yaml:"index"`
14
}
15
16
// SiteYaml is a structure that will be automatically populated with the value of the configuration
17
type SiteYaml struct {
18
RefField string `yaml:"ref"`
19
HostField string `yaml:"host"`
20
DescriptionField string `yaml:"description"`
21
RedirectsField []string `yaml:"redirects"`
22
LanguageField string `yaml:"language"`
23
KeepLinksField bool `yaml:"keeplinks"`
24
FaviconPathField string `yaml:"faviconpath"`
25
FrontProxyField *FrontProxyYaml `yaml:"frontproxy"`
26
}
27
28
// FrontProxyYaml is a structure that will be automatically populated with the value of the configuration
29
type FrontProxyYaml struct {
30
ForceSSL bool `yaml:"forcessl"`
31
IPHeader string `yaml:"ipheader"`
32
}
33
34
// Port return the port to which the daemon will listen to
35
func (config *ConfigurationYaml) Port() uint16 { return config.PortField }
36
37
// Sites returns the list of sites
38
func (config *ConfigurationYaml) Sites() []Site {
39
sites := make([]Site, len(config.SitesField))
40
41
for i := range config.SitesField {
42
sites[i] = config.SitesField[i]
43
}
44
45
return sites
46
}
47
48
// Index returns true if an index should be displayed when neither a host or a redirect was found
49
func (config *ConfigurationYaml) Index() bool { return config.IndexField }
50
51
// Ref returns the Google Sites reference of the websites
52
func (site *SiteYaml) Ref() string { return site.RefField }
53
54
// Host returns the host that is supposed to handle respond for this site
55
func (site *SiteYaml) Host() string { return site.HostField }
56
57
// Description returns a string describing the content of the site
58
func (site *SiteYaml) Description() string { return site.DescriptionField }
59
60
// Redirects returns a list of hostname that should redirect their traffic to the host
61
func (site *SiteYaml) Redirects() []string { return site.RedirectsField }
62
63
// Language returns the HTTP header used to query Google servers
64
func (site *SiteYaml) Language() string { return site.LanguageField }
65
66
// KeepLinks returns true if links should be kept
67
func (site *SiteYaml) KeepLinks() bool { return site.KeepLinksField }
68
69
// FaviconPath returns the path to the favicon file. No file is assumed if the string is empty
70
func (site *SiteYaml) FaviconPath() string { return site.FaviconPathField }
71
72
// GRef returns a consistent reference to the Google Sites instance
73
func (site *SiteYaml) GRef() string {
74
if !strings.Contains(site.Ref(), "/") {
75
return "view/" + site.Ref()
76
}
77
return site.Ref()
78
}
79
80
// IPHeader returns the HTTP header containing the ip of the requester, if empty this information will
81
// be taken from the connection directly
82
func (site *SiteYaml) IPHeader() string {
83
if site.FrontProxyField != nil {
84
return site.FrontProxyField.IPHeader
85
}
86
return ""
87
}
88
89
// NewYamlConfigLoader returns a loader for a given YAML configuration file
90
func NewYamlConfigLoader(filename string) ConfigLoader {
91
return func() (Configuration, error) {
92
bytes, err := ioutil.ReadFile(filename)
93
if err != nil {
94
return nil, err
95
}
96
97
c := &ConfigurationYaml{}
98
err = yaml.Unmarshal(bytes, c)
99
if err != nil {
100
return nil, err
101
}
102
return c, nil
103
}
104
}
105
106