2017-11-16 09:19:01 +00:00
|
|
|
// This file contains the configuration settings for the server.
|
2017-11-26 20:06:45 +00:00
|
|
|
package config
|
2017-11-16 09:19:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2017-11-24 20:50:23 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2017-11-16 09:19:01 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2017-11-23 19:23:55 +00:00
|
|
|
// loadConfigFromFile tries to load the configuration file from the disk.
|
2017-11-30 21:28:18 +00:00
|
|
|
func loadConfigFromFile(configName string) error {
|
|
|
|
viper.SetConfigName(configName)
|
2017-11-16 09:19:01 +00:00
|
|
|
viper.AddConfigPath(viper.GetString("ConfigPath"))
|
|
|
|
|
|
|
|
err := viper.ReadInConfig() // Find and read the config file
|
|
|
|
if err != nil { // Handle errors reading the config file
|
|
|
|
log.Printf("Fatal error reading config file: %s \n", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-23 19:23:55 +00:00
|
|
|
// setDefaultConfiguration will set the default configuration settings.
|
2017-11-30 21:28:18 +00:00
|
|
|
func setDefaultConfiguration(configPath string) {
|
2017-11-24 20:37:09 +00:00
|
|
|
viper.SetDefault("address", "localhost")
|
2017-11-26 21:03:15 +00:00
|
|
|
viper.SetDefault("port", 8080)
|
2017-11-30 21:28:18 +00:00
|
|
|
viper.SetDefault("configPath", configPath)
|
2017-11-24 20:37:09 +00:00
|
|
|
viper.SetDefault("maxDirDepth", 30)
|
|
|
|
viper.SetDefault("absoluteServePath", "./")
|
2017-11-24 20:50:23 +00:00
|
|
|
viper.SetDefault("pic.x", 0)
|
|
|
|
viper.SetDefault("pic.y", 0)
|
2017-11-25 13:53:54 +00:00
|
|
|
viper.SetDefault("pic.color", false)
|
2017-11-26 21:03:15 +00:00
|
|
|
viper.SetDefault("upload.enabled", false)
|
2017-11-30 21:28:18 +00:00
|
|
|
viper.SetDefault("upload.directory", "upload")
|
|
|
|
viper.SetDefault("upload.timeout", 3)
|
2017-11-26 21:03:15 +00:00
|
|
|
viper.SetDefault("upload.address", "localhost")
|
|
|
|
viper.SetDefault("upload.port", 8081)
|
2017-11-16 09:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 21:03:15 +00:00
|
|
|
// InitializeConfiguration initializes the configuration for the application.
|
2017-11-30 21:28:18 +00:00
|
|
|
func InitializeConfiguration(configName string, configPath string) {
|
|
|
|
setDefaultConfiguration(configPath)
|
|
|
|
loadConfigFromFile(configName)
|
2017-11-24 20:50:23 +00:00
|
|
|
|
|
|
|
viper.WatchConfig()
|
2017-11-26 21:03:15 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 21:28:18 +00:00
|
|
|
func ChangeCallback(cb func(event fsnotify.Event)) {
|
2017-11-26 21:03:15 +00:00
|
|
|
viper.OnConfigChange(cb)
|
2017-11-16 09:19:01 +00:00
|
|
|
}
|