46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// Configuration is the app configuration stored in a json file
|
|
type Configuration struct {
|
|
ServerRoot string `json:"serverRoot"`
|
|
Port int `json:"port"`
|
|
DBType string `json:"dbtype"`
|
|
DBConfigString string `json:"dbconfig"`
|
|
UseSSL bool `json:"useSSL"`
|
|
WebPath string `json:"webpath"`
|
|
FilesPath string `json:"filespath"`
|
|
}
|
|
|
|
func readConfigFile() (*Configuration, error) {
|
|
viper.SetConfigName("config") // name of config file (without extension)
|
|
viper.SetConfigType("json") // REQUIRED if the config file does not have the extension in the name
|
|
viper.AddConfigPath(".") // optionally look for config in the working directory
|
|
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
|
|
}
|
|
|
|
configuration := Configuration{}
|
|
err = viper.Unmarshal(&configuration)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Println("readConfigFile")
|
|
log.Printf("%+v", configuration)
|
|
|
|
return &configuration, nil
|
|
}
|