simplFT/server/config/config.go

51 lines
1.5 KiB
Go
Raw Normal View History

// This file contains the configuration settings for the server.
package config
import (
"log"
2017-11-24 20:50:23 +00:00
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
// loadConfigFromFile tries to load the configuration file from the disk.
func loadConfigFromFile(configName string) error {
viper.SetConfigName(configName)
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
}
// setDefaultConfiguration will set the default configuration settings.
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)
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)
viper.SetDefault("pic.color", false)
2017-11-26 21:03:15 +00:00
viper.SetDefault("upload.enabled", false)
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-26 21:03:15 +00:00
// InitializeConfiguration initializes the configuration for the application.
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
}
func ChangeCallback(cb func(event fsnotify.Event)) {
2017-11-26 21:03:15 +00:00
viper.OnConfigChange(cb)
}