config: use constants with config keys instead of struct as its easier to handle

This commit is contained in:
Jakob Schrettenbrunner 2017-08-31 20:13:01 +02:00
parent 5f0804ea53
commit ef28b61136
6 changed files with 106 additions and 24 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/Pterodactyl/wings/config"
)
@ -22,7 +23,7 @@ func NewAPI() API {
// Listen starts the api http server
func (api *API) Listen() {
if !config.Get().Debug {
if !viper.GetBool(config.Debug) {
gin.SetMode(gin.ReleaseMode)
}
@ -30,7 +31,7 @@ func (api *API) Listen() {
api.registerRoutes()
listenString := fmt.Sprintf("%s:%d", config.Get().Web.ListenHost, config.Get().Web.ListenPort)
listenString := fmt.Sprintf("%s:%d", viper.GetString(config.APIHost), viper.GetInt(config.APIPort))
api.router.Run(listenString)

View File

@ -45,14 +45,14 @@ func (a *authorizationManager) HasPermission(permission string) bool {
}
prefix := permission[:1]
if prefix == "c" {
return config.Get().ContainsAuthKey(a.token)
return config.ContainsAuthKey(a.token)
}
if a.server == nil {
log.WithField("permission", permission).Error("Auth: Server required but none found.")
return false
}
if prefix == "g" {
return config.Get().ContainsAuthKey(a.token)
return config.ContainsAuthKey(a.token)
}
if prefix == "s" {
return a.server.HasPermission(a.token, permission)

View File

@ -1,6 +1,7 @@
{
"debug": false,
"web": {
"dataPath": "/srv/daemon-data",
"api": {
"host": "0.0.0.0",
"port": 8080,
"ssl": {
@ -20,7 +21,7 @@
"timezonePath": "/etc/timezone"
},
"sftp": {
"path": "/srv/daemon-data",
"host": "0.0.0.0",
"port": 2022
},
"query": {

View File

@ -9,6 +9,8 @@ type Config struct {
// Debug enables debug mode
Debug bool `mapstructure:"debug"`
DataPath string `mapstructure:"dataPath"`
// Web contains the settings of the api webserver
Web struct {
// ListenHost is the host address to bind the api webserver to
@ -49,8 +51,6 @@ type Config struct {
// Sftp contains information on the integrated sftp server
Sftp struct {
// Path is the base path of the sftp server
Path string `mapstructure:"path"`
// Port is the port to bind the sftp server to
Port int16 `mapstructure:"port"`
} `mapstructure:"sftp"`
@ -80,6 +80,7 @@ type Config struct {
AuthKeys []string `mapstructure:"authKeys"`
}
var configPath string
var config *Config
// LoadConfiguration loads the configuration from a specified file
@ -96,26 +97,38 @@ func LoadConfiguration(path string) error {
return err
}
config = new(Config)
if err := viper.Unmarshal(config); err != nil {
return err
}
return nil
}
// StoreConfiguration stores the configuration to a specified file
func StoreConfiguration(path string) error {
// TODO: Implement
return nil
}
// Get returns the configuration
func Get() *Config {
return config
}
func setDefaults() {
viper.SetDefault(Debug, false)
viper.SetDefault(DataPath, "./data")
viper.SetDefault(APIHost, "0.0.0.0")
viper.SetDefault(APIPort, 8080)
viper.SetDefault(SSLEnabled, false)
viper.SetDefault(SSLGenerateLetsencrypt, false)
viper.SetDefault(UploadsMaximumSize, 100000)
viper.SetDefault(DockerSocket, "/var/run/docker.sock")
viper.SetDefault(DockerAutoupdateImages, true)
viper.SetDefault(DockerNetworkInterface, "127.18.0.0")
viper.SetDefault(DockerTimezonePath, "/etc/timezone")
viper.SetDefault(SftpHost, "0.0.0.0")
viper.SetDefault(SftpPort, "2202")
viper.SetDefault(LogPath, "./logs")
viper.SetDefault(LogLevel, "info")
viper.SetDefault(LogDeleteAfterDays, "30")
}
// ContainsAuthKey checks wether the config contains a specified authentication key
func (c *Config) ContainsAuthKey(key string) bool {
for _, k := range c.AuthKeys {
func ContainsAuthKey(key string) bool {
for _, k := range viper.GetStringSlice(AuthKeys) {
if k == key {
return true
}

66
config/keys.go Normal file
View File

@ -0,0 +1,66 @@
package config
const (
// Debug is a boolean value that enables debug mode
Debug = "debug"
// DataPath is a string containing the path where data should
// be stored on the system
DataPath = "datapath"
// APIHost is a string containing the interface ip address
// on what the api should listen on
APIHost = "api.host"
// APIPort is an integer containing the port the api should
// listen on
APIPort = "api.port"
// SSLEnabled is a boolean that states whether ssl should be enabled or not
SSLEnabled = "api.ssl.enabled"
// SSLGenerateLetsencrypt is a boolean that enables automatic SSL certificate
// creation with letsencrypt
SSLGenerateLetsencrypt = "api.ssl.letsencrypt"
// SSLCertificate is a string containing the location of
// a ssl certificate to use
SSLCertificate = "api.ssl.certificate"
// SSLKey is a string containing the location of the key
// for the ssl certificate
SSLKey = "api.ssl.key"
// UploadsMaximumSize is an integer that sets the maximum size for
// file uploads through the api in Kilobytes
UploadsMaximumSize = "api.uploads.maximumSize"
// DockerSocket is a string containing the path to the docker socket
DockerSocket = "docker.socket"
// DockerAutoupdateImages is a boolean that enables automatic
// docker image updates on every server install
DockerAutoupdateImages = "docker.autoupdateImages"
// DockerNetworkInterface is a string containing the network interface
// to use for the wings docker network
DockerNetworkInterface = "docker.networkInterface"
// DockerTimezonePath is a string containing the path to the timezone
// file that should be mounted into all containers
DockerTimezonePath = "docker.timezonePath"
// SftpHost is a string containing the interface ip address on
// which the sftp server should be listening
SftpHost = "sftp.host"
// SftpPort is an integer containing the port the sftp servers hould
// be listening on
SftpPort = "sftp.port"
// Remote is a string containing the url to the Pterodactyl panel
// wings should send updates to
Remote = "remote"
// LogPath is a string containing the path where logfiles should be
// stored
LogPath = "log.path"
// LogLevel is a string containing the log level
LogLevel = "log.level"
// LogDeleteAfterDays is an integer containing the amounts of days
// logs should be stored. They will be deleted after. If set to 0
// logs will be stored indefinitely.
LogDeleteAfterDays = "log.deleteAfterDays"
// AuthKeys contains an array of auth keys that will be replaced by something better
AuthKeys = "authkeys"
)

View File

@ -6,6 +6,7 @@ import (
rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/Pterodactyl/wings/config"
)
@ -21,11 +22,11 @@ func InitLogging() {
// ConfigureLogging configures logrus to our needs
func ConfigureLogging() error {
path := config.Get().Log.Path
path := viper.GetString(config.LogPath)
writer := rotatelogs.New(
path+"wings.%Y%m%d-%H%M.log",
rotatelogs.WithLinkName(path),
rotatelogs.WithMaxAge(time.Duration(config.Get().Log.DeleteAfterDays)*time.Hour*24),
rotatelogs.WithMaxAge(time.Duration(viper.GetInt(config.LogDeleteAfterDays))*time.Hour*24),
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
)
@ -37,10 +38,10 @@ func ConfigureLogging() error {
log.FatalLevel: writer,
}))
level := config.Get().Log.Level
level := viper.GetString(config.LogLevel)
// In debug mode the log level is always debug
if config.Get().Debug {
if viper.GetBool(config.Debug) {
level = "debug"
}