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"
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

View File

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

View File

@ -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))
}

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": {
"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
}
}

View File

@ -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() {
}

11
main.go
View File

@ -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()
}

View File

@ -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

View File

@ -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() {