From 06055d62659b785c2b580a493237d571ad2b5561 Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Mon, 26 Jun 2017 11:07:53 +0200 Subject: [PATCH] unmarshal config into struct with viper for consistent access --- Gopkg.lock | 26 +++++++++++++----- Gopkg.toml | 4 +++ api/api.go | 31 +++++++++++++++++++++ api/auth.go | 1 + api/auth_test.go | 11 ++++++++ config.example.json | 26 +++++++++--------- config/config.go | 66 ++++++++++++++++++++++++++------------------- main.go | 11 +++++--- services/service.go | 2 +- tools/logging.go | 2 +- 10 files changed, 128 insertions(+), 52 deletions(-) create mode 100644 api/auth.go create mode 100644 api/auth_test.go diff --git a/Gopkg.lock b/Gopkg.lock index 583b670..e1947df 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -13,6 +13,12 @@ revision = "ba1b36c82c5e05c4f912a88eab0dcd91a171688f" version = "v0.11.5" +[[projects]] + name = "github.com/davecgh/go-spew" + packages = ["spew"] + revision = "346938d642f2ec3594ed81d874461961cd0faa76" + version = "v1.1.0" + [[projects]] name = "github.com/fsnotify/fsnotify" packages = ["."] @@ -55,18 +61,18 @@ revision = "13d49d4606eb801b8f01ae542b4afc4c6ee3d84a" version = "v0.5.0" +[[projects]] + name = "github.com/pmezard/go-difflib" + packages = ["difflib"] + revision = "792786c7400a136282c1664665ae0a8db921c6c2" + version = "v1.0.0" + [[projects]] name = "github.com/rifflock/lfshook" packages = ["."] revision = "2adb3e0c4ddd8778c4adde609d2dfd4fbe6096ea" version = "1.6" -[[projects]] - branch = "schrej-wip" - name = "github.com/schrej/pterodactyld" - packages = ["config","environments","tools"] - revision = "f244cda20faac1b62e0b5ff586a1b0918787c992" - [[projects]] branch = "master" name = "github.com/spf13/afero" @@ -96,6 +102,12 @@ packages = ["."] revision = "0967fc9" +[[projects]] + name = "github.com/stretchr/testify" + packages = ["assert"] + revision = "69483b4bd14f5845b5a1e55bca19e954e827f1d0" + version = "v1.1.4" + [[projects]] branch = "master" name = "golang.org/x/sys" @@ -117,6 +129,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "f24a85167f15a18e90cc0ea139dcc2c80fe6272cfe7c8dba6ecc2e2afb24e61d" + inputs-digest = "e16ef683ca7989700b7906f19c244a648f891f0c9998b6ce8e2a7664a581e915" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 5a74936..e148a73 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -73,3 +73,7 @@ [[constraint]] revision = "0967fc9" name = "github.com/spf13/viper" + +[[constraint]] + version = "1.1.4" + name = "github.com/stretchr/testify" diff --git a/api/api.go b/api/api.go index 778f64e..bc02fb4 100644 --- a/api/api.go +++ b/api/api.go @@ -1 +1,32 @@ package api + +import ( + "fmt" + "html" + "net/http" + + log "github.com/Sirupsen/logrus" + + "github.com/schrej/wings.go/config" +) + +// API is a grouping struct for the api +type API struct { +} + +// NewAPI creates a new Api object +func NewAPI() API { + return API{} +} + +// Listen starts the api http server +func (api *API) Listen() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) + }) + + listenString := fmt.Sprintf("%s:%d", config.Get().Web.ListenHost, config.Get().Web.ListenPort) + + log.Info("Now listening on %s", listenString) + log.Fatal(http.ListenAndServe(listenString, nil)) +} diff --git a/api/auth.go b/api/auth.go new file mode 100644 index 0000000..778f64e --- /dev/null +++ b/api/auth.go @@ -0,0 +1 @@ +package api diff --git a/api/auth_test.go b/api/auth_test.go new file mode 100644 index 0000000..9e4bd93 --- /dev/null +++ b/api/auth_test.go @@ -0,0 +1,11 @@ +package api + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFunction(t *testing.T) { + assert.Equal(t, 1, 1) +} diff --git a/config.example.json b/config.example.json index 15dc9f9..71ac08d 100644 --- a/config.example.json +++ b/config.example.json @@ -1,35 +1,35 @@ { "web": { "host": "0.0.0.0", - "listen": 8080, + "port": 8080, "ssl": { "enabled": false, "generateLetsEncrypt": true, "certificate": "/etc/letsencrypt/live/pterodactyl.app/fullchain.pem", "key": "/etc/letsencrypt/live/pterodactyl.app/privkey.pem" + }, + "uploads": { + "maximumSize": 150000000 } }, "docker": { "socket": "/var/run/docker.sock", - "autoupdate_images": true, - "interface": "172.18.0.1", - "timezone_path": "/etc/timezone" + "autoupdateImages": true, + "networkInterface": "172.18.0.1", + "timezonePath": "/etc/timezone" }, "sftp": { "path": "/srv/daemon-data", "port": 2022 }, "query": { - "kill_on_fail": false, - "fail_limit": 5 - }, - "logger": { - "path": "logs/", - "level": "debug" + "killOnFail": false, + "failLimit": 5 }, "remote": "https://pterodactyl.app", - "uploads": { - "maximumSize": 150000000, - "size_limit": "100" + "logger": { + "path": "logs/", + "level": "debug", + "deleteAfterDays": 100 } } diff --git a/config/config.go b/config/config.go index e632601..dc94600 100644 --- a/config/config.go +++ b/config/config.go @@ -9,72 +9,74 @@ type Config struct { // Web contains the settings of the api webserver Web struct { - // ListenAddress is the address to bind the api webserver to - ListenAddress string `json:"address"` + // ListenHost is the host address to bind the api webserver to + ListenHost string `mapstructure:"host"` // ListenPort is the port to bind the api webserver to - ListenPort int16 `json:"port"` + ListenPort int16 `mapstructure:"port"` // SSL contains https configuration for the api webserver SSL struct { // Enabled allows to enable or disable ssl - Enabled bool `json:"enabled"` + Enabled bool `mapstructure:"enabled"` // GenerateLetsEncrypt - GenerateLetsEncrypt bool `json:"GenerateLetsEncrypt"` + GenerateLetsEncrypt bool `mapstructure:"GenerateLetsEncrypt"` // Certificate is the certificate file path to use - Certificate string `json:"certificate"` + Certificate string `mapstructure:"certificate"` // Key is the path to the private key for the certificate - Key string `json:"key"` - } `json:"ssl"` + Key string `mapstructure:"key"` + } `mapstructure:"ssl"` // Uploads contains file upload configuration Uploads struct { // MaximumSize is the maximum file upload size - MaximumSize int64 `json:"maximumSize"` - } `json:"uploads"` - } `json:"web"` + MaximumSize int64 `mapstructure:"maximumSize"` + } `mapstructure:"uploads"` + } `mapstructure:"web"` // Docker contains docker related configuration Docker struct { // Socket is the path to the docker control socket - Socket string `json:"socket"` + Socket string `mapstructure:"socket"` // AutoupdateImages allows to disable automatic Image updates - AutoupdateImages bool `json:"autoupdateImages"` + AutoupdateImages bool `mapstructure:"autoupdateImages"` // NetworkInterface is the interface for the pterodactyl network - NetworkInterface string `json:"networkInterface"` + NetworkInterface string `mapstructure:"networkInterface"` // TimezonePath is the path to the timezone file to mount in the containers - TimezonePath string `json:"timezonePath"` - } `json:"docker"` + TimezonePath string `mapstructure:"timezonePath"` + } `mapstructure:"docker"` // Sftp contains information on the integrated sftp server Sftp struct { // Path is the base path of the sftp server - Path string `json:"path"` + Path string `mapstructure:"path"` // Port is the port to bind the sftp server to - Port int16 `json:"port"` - } `json:"sftp"` + Port int16 `mapstructure:"port"` + } `mapstructure:"sftp"` // Query contains parameters related to queriying of running gameservers Query struct { - KillOnFail bool `json:"killOnFail"` - FailLimit bool `json:"failLimit"` - } `json:"query"` + KillOnFail bool `mapstructure:"killOnFail"` + FailLimit bool `mapstructure:"failLimit"` + } `mapstructure:"query"` // Remote is the url of the panel - Remote string `json:"remote"` + Remote string `mapstructure:"remote"` // Log contains configuration related to logging Log struct { // Path is the folder where logfiles should be stored - Path string `json:"path"` + Path string `mapstructure:"path"` // Level is the preferred log level - Level string `json:"level"` + Level string `mapstructure:"level"` // DeleteAfterDays is the time in days after which logfiles are deleted // If set to <= 0 logs are kept forever - DeleteAfterDays int `json:"deleteAfterDays"` - } `json:"log"` + DeleteAfterDays int `mapstructure:"deleteAfterDays"` + } `mapstructure:"log"` } +var config *Config + // LoadConfiguration loads the configuration from the disk. func LoadConfiguration() error { viper.SetConfigName("config") @@ -84,9 +86,19 @@ func LoadConfiguration() error { return err } + config = new(Config) + if err := viper.Unmarshal(config); err != nil { + return err + } + return nil } +// Get returns the configuration +func Get() *Config { + return config +} + func setDefaults() { } diff --git a/main.go b/main.go index f4c9af7..cdd25cd 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,9 @@ package main import ( log "github.com/Sirupsen/logrus" - "github.com/schrej/pterodactyld/config" - "github.com/schrej/pterodactyld/tools" + "github.com/schrej/wings.go/api" + "github.com/schrej/wings.go/config" + "github.com/schrej/wings.go/tools" ) const ( @@ -14,11 +15,15 @@ const ( func main() { tools.ConfigureLogging() - log.Info("Starting pterodactyld version ", Version) + log.Info("Starting wings.go version ", Version) // Load configuration log.Info("Loading configuration...") if err := config.LoadConfiguration(); err != nil { log.WithError(err).Fatal("Failed to find configuration file") } + + log.Info("Starting api webserver") + api := api.NewAPI() + api.Listen() } diff --git a/services/service.go b/services/service.go index dc1d1c3..53a4a90 100644 --- a/services/service.go +++ b/services/service.go @@ -1,6 +1,6 @@ package services -import "github.com/schrej/pterodactyld/environments" +import "github.com/schrej/wings.go/environments" type Service struct { Environment environments.Environment diff --git a/tools/logging.go b/tools/logging.go index e5a1d8d..721e1e8 100644 --- a/tools/logging.go +++ b/tools/logging.go @@ -3,9 +3,9 @@ package tools import ( "time" - log "github.com/Sirupsen/logrus" rotatelogs "github.com/lestrrat/go-file-rotatelogs" "github.com/rifflock/lfshook" + log "github.com/Sirupsen/logrus" ) func ConfigureLogging() {