gogmagog/config/config.go
2020-12-27 19:33:54 -06:00

47 lines
977 B
Go

package config
import (
"log"
"os"
"github.com/spf13/viper"
)
// Key represents a particular config flag.
type Key string
// These constants are the list of keys that can be used.
const (
Environment Key = `app.environment`
Port Key = `app.port`
DBType Key = `db.type`
DBHost Key = `db.host`
DBPort Key = `db.port`
DBUser Key = `db.user`
DBPassword Key = `db.password`
DBDatabase Key = `db.database`
)
// GetString returns the string value of the provided config key.
func (k Key) GetString() string {
return viper.GetString(string(k))
}
// Init sets all of the viper config parameters.
func Init() {
viper.SetDefault(string(Environment), "local")
viper.SetDefault(string(Port), "8080")
log.Print("Initialising config...")
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
log.Fatal("Could not read config file: \n", err)
os.Exit(1)
}
}