unmarshal config into struct with viper for consistent access

This commit is contained in:
Jakob Schrettenbrunner 2017-06-26 11:07:53 +02:00
parent b4b0c3f9e6
commit 06055d6265
10 changed files with 128 additions and 52 deletions

26
Gopkg.lock generated
View File

@ -13,6 +13,12 @@
revision = "ba1b36c82c5e05c4f912a88eab0dcd91a171688f" revision = "ba1b36c82c5e05c4f912a88eab0dcd91a171688f"
version = "v0.11.5" version = "v0.11.5"
[[projects]]
name = "github.com/davecgh/go-spew"
packages = ["spew"]
revision = "346938d642f2ec3594ed81d874461961cd0faa76"
version = "v1.1.0"
[[projects]] [[projects]]
name = "github.com/fsnotify/fsnotify" name = "github.com/fsnotify/fsnotify"
packages = ["."] packages = ["."]
@ -55,18 +61,18 @@
revision = "13d49d4606eb801b8f01ae542b4afc4c6ee3d84a" revision = "13d49d4606eb801b8f01ae542b4afc4c6ee3d84a"
version = "v0.5.0" version = "v0.5.0"
[[projects]]
name = "github.com/pmezard/go-difflib"
packages = ["difflib"]
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
version = "v1.0.0"
[[projects]] [[projects]]
name = "github.com/rifflock/lfshook" name = "github.com/rifflock/lfshook"
packages = ["."] packages = ["."]
revision = "2adb3e0c4ddd8778c4adde609d2dfd4fbe6096ea" revision = "2adb3e0c4ddd8778c4adde609d2dfd4fbe6096ea"
version = "1.6" version = "1.6"
[[projects]]
branch = "schrej-wip"
name = "github.com/schrej/pterodactyld"
packages = ["config","environments","tools"]
revision = "f244cda20faac1b62e0b5ff586a1b0918787c992"
[[projects]] [[projects]]
branch = "master" branch = "master"
name = "github.com/spf13/afero" name = "github.com/spf13/afero"
@ -96,6 +102,12 @@
packages = ["."] packages = ["."]
revision = "0967fc9" revision = "0967fc9"
[[projects]]
name = "github.com/stretchr/testify"
packages = ["assert"]
revision = "69483b4bd14f5845b5a1e55bca19e954e827f1d0"
version = "v1.1.4"
[[projects]] [[projects]]
branch = "master" branch = "master"
name = "golang.org/x/sys" name = "golang.org/x/sys"
@ -117,6 +129,6 @@
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "f24a85167f15a18e90cc0ea139dcc2c80fe6272cfe7c8dba6ecc2e2afb24e61d" inputs-digest = "e16ef683ca7989700b7906f19c244a648f891f0c9998b6ce8e2a7664a581e915"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View File

@ -73,3 +73,7 @@
[[constraint]] [[constraint]]
revision = "0967fc9" revision = "0967fc9"
name = "github.com/spf13/viper" name = "github.com/spf13/viper"
[[constraint]]
version = "1.1.4"
name = "github.com/stretchr/testify"

View File

@ -1 +1,32 @@
package api 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))
}

1
api/auth.go Normal file
View File

@ -0,0 +1 @@
package api

11
api/auth_test.go Normal file
View File

@ -0,0 +1,11 @@
package api
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFunction(t *testing.T) {
assert.Equal(t, 1, 1)
}

View File

@ -1,35 +1,35 @@
{ {
"web": { "web": {
"host": "0.0.0.0", "host": "0.0.0.0",
"listen": 8080, "port": 8080,
"ssl": { "ssl": {
"enabled": false, "enabled": false,
"generateLetsEncrypt": true, "generateLetsEncrypt": true,
"certificate": "/etc/letsencrypt/live/pterodactyl.app/fullchain.pem", "certificate": "/etc/letsencrypt/live/pterodactyl.app/fullchain.pem",
"key": "/etc/letsencrypt/live/pterodactyl.app/privkey.pem" "key": "/etc/letsencrypt/live/pterodactyl.app/privkey.pem"
},
"uploads": {
"maximumSize": 150000000
} }
}, },
"docker": { "docker": {
"socket": "/var/run/docker.sock", "socket": "/var/run/docker.sock",
"autoupdate_images": true, "autoupdateImages": true,
"interface": "172.18.0.1", "networkInterface": "172.18.0.1",
"timezone_path": "/etc/timezone" "timezonePath": "/etc/timezone"
}, },
"sftp": { "sftp": {
"path": "/srv/daemon-data", "path": "/srv/daemon-data",
"port": 2022 "port": 2022
}, },
"query": { "query": {
"kill_on_fail": false, "killOnFail": false,
"fail_limit": 5 "failLimit": 5
},
"logger": {
"path": "logs/",
"level": "debug"
}, },
"remote": "https://pterodactyl.app", "remote": "https://pterodactyl.app",
"uploads": { "logger": {
"maximumSize": 150000000, "path": "logs/",
"size_limit": "100" "level": "debug",
"deleteAfterDays": 100
} }
} }

View File

@ -9,72 +9,74 @@ type Config struct {
// Web contains the settings of the api webserver // Web contains the settings of the api webserver
Web struct { Web struct {
// ListenAddress is the address to bind the api webserver to // ListenHost is the host address to bind the api webserver to
ListenAddress string `json:"address"` ListenHost string `mapstructure:"host"`
// ListenPort is the port to bind the api webserver to // 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 contains https configuration for the api webserver
SSL struct { SSL struct {
// Enabled allows to enable or disable ssl // Enabled allows to enable or disable ssl
Enabled bool `json:"enabled"` Enabled bool `mapstructure:"enabled"`
// GenerateLetsEncrypt // GenerateLetsEncrypt
GenerateLetsEncrypt bool `json:"GenerateLetsEncrypt"` GenerateLetsEncrypt bool `mapstructure:"GenerateLetsEncrypt"`
// Certificate is the certificate file path to use // 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 is the path to the private key for the certificate
Key string `json:"key"` Key string `mapstructure:"key"`
} `json:"ssl"` } `mapstructure:"ssl"`
// Uploads contains file upload configuration // Uploads contains file upload configuration
Uploads struct { Uploads struct {
// MaximumSize is the maximum file upload size // MaximumSize is the maximum file upload size
MaximumSize int64 `json:"maximumSize"` MaximumSize int64 `mapstructure:"maximumSize"`
} `json:"uploads"` } `mapstructure:"uploads"`
} `json:"web"` } `mapstructure:"web"`
// Docker contains docker related configuration // Docker contains docker related configuration
Docker struct { Docker struct {
// Socket is the path to the docker control socket // 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 allows to disable automatic Image updates
AutoupdateImages bool `json:"autoupdateImages"` AutoupdateImages bool `mapstructure:"autoupdateImages"`
// NetworkInterface is the interface for the pterodactyl network // 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 is the path to the timezone file to mount in the containers
TimezonePath string `json:"timezonePath"` TimezonePath string `mapstructure:"timezonePath"`
} `json:"docker"` } `mapstructure:"docker"`
// Sftp contains information on the integrated sftp server // Sftp contains information on the integrated sftp server
Sftp struct { Sftp struct {
// Path is the base path of the sftp server // 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 is the port to bind the sftp server to
Port int16 `json:"port"` Port int16 `mapstructure:"port"`
} `json:"sftp"` } `mapstructure:"sftp"`
// Query contains parameters related to queriying of running gameservers // Query contains parameters related to queriying of running gameservers
Query struct { Query struct {
KillOnFail bool `json:"killOnFail"` KillOnFail bool `mapstructure:"killOnFail"`
FailLimit bool `json:"failLimit"` FailLimit bool `mapstructure:"failLimit"`
} `json:"query"` } `mapstructure:"query"`
// Remote is the url of the panel // Remote is the url of the panel
Remote string `json:"remote"` Remote string `mapstructure:"remote"`
// Log contains configuration related to logging // Log contains configuration related to logging
Log struct { Log struct {
// Path is the folder where logfiles should be stored // Path is the folder where logfiles should be stored
Path string `json:"path"` Path string `mapstructure:"path"`
// Level is the preferred log level // 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 // DeleteAfterDays is the time in days after which logfiles are deleted
// If set to <= 0 logs are kept forever // If set to <= 0 logs are kept forever
DeleteAfterDays int `json:"deleteAfterDays"` DeleteAfterDays int `mapstructure:"deleteAfterDays"`
} `json:"log"` } `mapstructure:"log"`
} }
var config *Config
// LoadConfiguration loads the configuration from the disk. // LoadConfiguration loads the configuration from the disk.
func LoadConfiguration() error { func LoadConfiguration() error {
viper.SetConfigName("config") viper.SetConfigName("config")
@ -84,9 +86,19 @@ func LoadConfiguration() error {
return err return err
} }
config = new(Config)
if err := viper.Unmarshal(config); err != nil {
return err
}
return nil return nil
} }
// Get returns the configuration
func Get() *Config {
return config
}
func setDefaults() { func setDefaults() {
} }

11
main.go
View File

@ -2,8 +2,9 @@ package main
import ( import (
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/schrej/pterodactyld/config" "github.com/schrej/wings.go/api"
"github.com/schrej/pterodactyld/tools" "github.com/schrej/wings.go/config"
"github.com/schrej/wings.go/tools"
) )
const ( const (
@ -14,11 +15,15 @@ const (
func main() { func main() {
tools.ConfigureLogging() tools.ConfigureLogging()
log.Info("Starting pterodactyld version ", Version) log.Info("Starting wings.go version ", Version)
// Load configuration // Load configuration
log.Info("Loading configuration...") log.Info("Loading configuration...")
if err := config.LoadConfiguration(); err != nil { if err := config.LoadConfiguration(); err != nil {
log.WithError(err).Fatal("Failed to find configuration file") log.WithError(err).Fatal("Failed to find configuration file")
} }
log.Info("Starting api webserver")
api := api.NewAPI()
api.Listen()
} }

View File

@ -1,6 +1,6 @@
package services package services
import "github.com/schrej/pterodactyld/environments" import "github.com/schrej/wings.go/environments"
type Service struct { type Service struct {
Environment environments.Environment Environment environments.Environment

View File

@ -3,9 +3,9 @@ package tools
import ( import (
"time" "time"
log "github.com/Sirupsen/logrus"
rotatelogs "github.com/lestrrat/go-file-rotatelogs" rotatelogs "github.com/lestrrat/go-file-rotatelogs"
"github.com/rifflock/lfshook" "github.com/rifflock/lfshook"
log "github.com/Sirupsen/logrus"
) )
func ConfigureLogging() { func ConfigureLogging() {