forked from Cliffbreak/tsviewer
84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
User struct {
|
|
Nickname string
|
|
Name string
|
|
Password string
|
|
}
|
|
ServerTS struct {
|
|
IP string
|
|
PortServer uint16
|
|
PortQuery uint16
|
|
}
|
|
ServerWeb struct {
|
|
Port uint16
|
|
}
|
|
}
|
|
|
|
const FileName = "config.json"
|
|
|
|
func New() (*Config, error) {
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigFile("yaml")
|
|
viper.AddConfigPath(".")
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
setDefaults()
|
|
|
|
var config Config
|
|
err := viper.Unmarshal(&config)
|
|
|
|
config.overrideWithEnvironmentVars()
|
|
|
|
return &config, err
|
|
}
|
|
|
|
func setDefaults() {
|
|
viper.SetDefault("User.Nickname", "serveradmin")
|
|
viper.SetDefault("User.Name", "serveradmin")
|
|
viper.SetDefault("User.Password", "<changeMe>")
|
|
viper.SetDefault("ServerTS.IP", "127.0.0.1")
|
|
viper.SetDefault("ServerTS.PortServer", "9987")
|
|
viper.SetDefault("ServerTS.PortQuery", "10011")
|
|
viper.SetDefault("ServerWeb.Port", "80")
|
|
}
|
|
|
|
func (config *Config) overrideWithEnvironmentVars() {
|
|
if nickname := os.Getenv("TS3_NICKNAME"); nickname != "" {
|
|
config.User.Nickname = nickname
|
|
}
|
|
|
|
if username := os.Getenv("TS3_NAME"); username != "" {
|
|
config.User.Name = username
|
|
}
|
|
|
|
if password := os.Getenv("TS3_PW"); password != "" {
|
|
config.User.Password = password
|
|
}
|
|
|
|
if tsIP := os.Getenv("TS3_IP"); tsIP != "" {
|
|
config.ServerTS.IP = tsIP
|
|
}
|
|
|
|
if tsPort, err := strconv.Atoi(os.Getenv("TS3_PORT")); tsPort <= 65535 && tsPort >= 0 && err == nil {
|
|
config.ServerTS.PortServer = uint16(tsPort)
|
|
}
|
|
|
|
if tsQuery, err := strconv.Atoi(os.Getenv("TS3_QUERY")); tsQuery <= 65535 && tsQuery >= 0 && err == nil {
|
|
config.ServerTS.PortQuery = uint16(tsQuery)
|
|
}
|
|
|
|
if webPort, err := strconv.Atoi(os.Getenv("WEB_PORT")); webPort <= 65535 && webPort >= 0 && err == nil {
|
|
config.ServerWeb.Port = uint16(webPort)
|
|
}
|
|
}
|