@@ -41,20 +41,24 @@ type File struct {
4141 ShopProtocol string `mapstructure:"protocol"`
4242 ShopPort int `mapstructure:"port"`
4343 Debug debug `mapstructure:"debug"`
44- AllSources repository.Sources `mapstructure:"sources"`
44+ AllSources repository.ConfigSources `mapstructure:"sources"`
4545 Name string `mapstructure:"name"`
4646 Security security `mapstructure:"security"`
4747 CustomTitleDB map [string ]repository.TitleDBEntry `mapstructure:"customTitledb"`
4848 NSP nsp `mapsstructure:"nsp"`
4949 shopTemplateData repository.ShopTemplate
50+
51+ allHooks []func (repository.Config )
52+ beforeAllHooks []func (repository.Config )
5053}
5154
52- var serverConfig File
53- var allHooks []func (repository.Config )
54- var beforeAllHooks []func (repository.Config )
55+ // New returns a new configuration
56+ func New () repository.Config {
57+ return & File {}
58+ }
5559
5660// LoadConfig handles viper under the hood
57- func LoadConfig () {
61+ func ( cfg * File ) LoadConfig () {
5862 viper .SetConfigName ("config" ) // name of config file (without extension)
5963 viper .SetConfigType ("yaml" ) // REQUIRED if the config file does not have the extension in the name
6064 viper .AddConfigPath ("." ) // optionally look for config in the working directory
@@ -72,42 +76,48 @@ func LoadConfig() {
7276
7377 viper .OnConfigChange (func (e fsnotify.Event ) {
7478 log .Println ("Config file changed, update new configuration..." )
75- configChange ()
79+ cfg . configChange ()
7680 })
7781 viper .WatchConfig ()
7882
79- configChange ()
83+ cfg . configChange ()
8084}
8185
82- func configChange () {
86+ func ( cfg * File ) configChange () {
8387 // Call all before hooks
84- for _ , hook := range beforeAllHooks {
85- hook (& serverConfig )
88+ for _ , hook := range cfg . beforeAllHooks {
89+ hook (cfg )
8690 }
8791
88- serverConfig = loadAndCompute ()
92+ newConfig := loadAndCompute ()
93+ cfg .rootShop = newConfig .rootShop
94+ cfg .ShopHost = newConfig .ShopHost
95+ cfg .ShopProtocol = newConfig .ShopProtocol
96+ cfg .ShopPort = newConfig .ShopPort
97+ cfg .Debug = newConfig .Debug
98+ cfg .AllSources = newConfig .AllSources
99+ cfg .Name = newConfig .Name
100+ cfg .Security = newConfig .Security
101+ cfg .CustomTitleDB = newConfig .CustomTitleDB
102+ cfg .NSP = newConfig .NSP
103+ cfg .shopTemplateData = newConfig .shopTemplateData
89104
90105 // Call all hooks
91- for _ , hook := range allHooks {
92- hook (& serverConfig )
106+ for _ , hook := range cfg . allHooks {
107+ hook (cfg )
93108 }
94109}
95110
96- // GetConfig returns the current configuration
97- func GetConfig () repository.Config {
98- return & serverConfig
99- }
100-
101- func loadAndCompute () File {
102- serverConfig = File {}
103- err := viper .Unmarshal (& serverConfig )
111+ func loadAndCompute () * File {
112+ var loadedConfig = & File {}
113+ err := viper .Unmarshal (& loadedConfig )
104114
105115 if err != nil {
106116 log .Fatalln (err )
107117 }
108- ComputeDefaultValues (& serverConfig )
118+ ComputeDefaultValues (loadedConfig )
109119
110- return serverConfig
120+ return loadedConfig
111121}
112122
113123// ComputeDefaultValues change the value taken from the config file
@@ -153,13 +163,13 @@ func ComputeDefaultValues(config repository.Config) repository.Config {
153163}
154164
155165// AddHook Add hook function on change config
156- func AddHook (f func (repository.Config )) {
157- allHooks = append (allHooks , f )
166+ func ( cfg * File ) AddHook (f func (repository.Config )) {
167+ cfg . allHooks = append (cfg . allHooks , f )
158168}
159169
160170// AddBeforeHook Add hook function before on change config
161- func AddBeforeHook (f func (repository.Config )) {
162- beforeAllHooks = append (beforeAllHooks , f )
171+ func ( cfg * File ) AddBeforeHook (f func (repository.Config )) {
172+ cfg . beforeAllHooks = append (cfg . beforeAllHooks , f )
163173}
164174
165175// SetRootShop allow to change the root url of the shop
@@ -218,7 +228,7 @@ func (cfg *File) NfsShares() []string {
218228}
219229
220230// Sources returns all available sources
221- func (cfg * File ) Sources () repository.Sources {
231+ func (cfg * File ) Sources () repository.ConfigSources {
222232 return cfg .AllSources
223233}
224234
0 commit comments