Merge pull request #2 from mattermost/config-to-viper
Simplifying config using viper
This commit is contained in:
commit
30449fb1f3
2 changed files with 26 additions and 40 deletions
|
@ -1,9 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Configuration is the app configuration stored in a json file
|
// Configuration is the app configuration stored in a json file
|
||||||
|
@ -17,49 +17,30 @@ type Configuration struct {
|
||||||
FilesPath string `json:"filespath"`
|
FilesPath string `json:"filespath"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfigFile() Configuration {
|
func readConfigFile() (*Configuration, error) {
|
||||||
fileName := "config.json"
|
viper.SetConfigName("config") // name of config file (without extension)
|
||||||
if !fileExists(fileName) {
|
viper.SetConfigType("json") // REQUIRED if the config file does not have the extension in the name
|
||||||
log.Println(`config.json not found, using default settings`)
|
viper.AddConfigPath(".") // optionally look for config in the working directory
|
||||||
return Configuration{}
|
viper.SetDefault("ServerRoot", "http://localhost")
|
||||||
|
viper.SetDefault("Port", 8000)
|
||||||
|
viper.SetDefault("DBType", "sqlite3")
|
||||||
|
viper.SetDefault("DBConfigString", "./octo.db")
|
||||||
|
viper.SetDefault("WebPath", "./pack")
|
||||||
|
viper.SetDefault("FilesPath", "./files")
|
||||||
|
|
||||||
|
err := viper.ReadInConfig() // Find and read the config file
|
||||||
|
if err != nil { // Handle errors reading the config file
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
file, _ := os.Open(fileName)
|
|
||||||
defer file.Close()
|
|
||||||
decoder := json.NewDecoder(file)
|
|
||||||
configuration := Configuration{}
|
configuration := Configuration{}
|
||||||
err := decoder.Decode(&configuration)
|
err = viper.Unmarshal(&configuration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Invalid config.json", err)
|
return nil, err
|
||||||
}
|
|
||||||
|
|
||||||
// Apply defaults
|
|
||||||
if len(configuration.ServerRoot) < 1 {
|
|
||||||
configuration.ServerRoot = "http://localhost"
|
|
||||||
}
|
|
||||||
|
|
||||||
if configuration.Port == 0 {
|
|
||||||
configuration.Port = 8000
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(configuration.DBType) < 1 {
|
|
||||||
configuration.DBType = "sqlite3"
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(configuration.DBConfigString) < 1 {
|
|
||||||
configuration.DBConfigString = "./octo.db"
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(configuration.WebPath) < 1 {
|
|
||||||
configuration.WebPath = "./pack"
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(configuration.FilesPath) < 1 {
|
|
||||||
configuration.FilesPath = "./files"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("readConfigFile")
|
log.Println("readConfigFile")
|
||||||
log.Printf("%+v", configuration)
|
log.Printf("%+v", configuration)
|
||||||
|
|
||||||
return configuration
|
return &configuration, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ import (
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
var config Configuration
|
var config *Configuration
|
||||||
|
|
||||||
// WebsocketMsg is send on block changes
|
// WebsocketMsg is send on block changes
|
||||||
type WebsocketMsg struct {
|
type WebsocketMsg struct {
|
||||||
|
@ -383,7 +383,12 @@ func monitorPid(pid int) {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// config.json file
|
// config.json file
|
||||||
config = readConfigFile()
|
var err error
|
||||||
|
config, err = readConfigFile()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Unable to read the config file: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Command line args
|
// Command line args
|
||||||
pMonitorPid := flag.Int("monitorpid", -1, "a process ID")
|
pMonitorPid := flag.Int("monitorpid", -1, "a process ID")
|
||||||
|
|
Loading…
Reference in a new issue