2018-04-09 14:12:54 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"fmt"
|
2018-04-09 15:24:57 +02:00
|
|
|
"os"
|
|
|
|
"log"
|
2018-04-09 14:12:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config represents the configuration of the server application.
|
|
|
|
type Config struct {
|
|
|
|
Address string
|
2018-04-09 15:24:57 +02:00
|
|
|
Port string
|
|
|
|
TLS *TLS
|
|
|
|
}
|
|
|
|
|
|
|
|
type TLS struct {
|
|
|
|
CertFile string
|
|
|
|
KeyFile string
|
2018-04-09 14:12:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the application configuration an sets defaults.
|
|
|
|
func ParseConfig() Config {
|
|
|
|
var cfg Config
|
|
|
|
|
|
|
|
setDefaults();
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.AddConfigPath("./config")
|
|
|
|
viper.AddConfigPath("$HOME/.swd")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
2018-04-09 15:24:57 +02:00
|
|
|
log.Fatal(fmt.Errorf("Fatal error config file: %s", err))
|
2018-04-09 14:12:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = viper.Unmarshal(&cfg)
|
|
|
|
if err != nil {
|
2018-04-09 15:24:57 +02:00
|
|
|
log.Fatal(fmt.Errorf("Fatal error parsing config file: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.TLS != nil {
|
|
|
|
if _, err := os.Stat(cfg.TLS.KeyFile); err != nil {
|
|
|
|
log.Fatal(fmt.Errorf("TLS keyFile doesn't exist: %s", err))
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(cfg.TLS.CertFile); err != nil {
|
|
|
|
log.Fatal(fmt.Errorf("TLS certFile doesn't exist: %s", err))
|
|
|
|
}
|
2018-04-09 14:12:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// setDefaults adds some default values for the configuration
|
|
|
|
func setDefaults() {
|
|
|
|
viper.SetDefault("Address", "127.0.0.1")
|
|
|
|
viper.SetDefault("Port", "8000")
|
2018-04-09 15:24:57 +02:00
|
|
|
viper.SetDefault("TLS", nil)
|
2018-04-09 14:12:54 +02:00
|
|
|
}
|