Change config from json to yaml

- Added possibility to override config with env vars
- Reorganized config structures
This commit is contained in:
Claudemiro
2018-11-25 23:39:12 +01:00
parent 964df8a5dd
commit f4781ee483
14 changed files with 102 additions and 97 deletions
+1
View File
@@ -155,6 +155,7 @@ flymake*
ignore_http/*
config.json
config.yml
*.pem
build
.vscode/*
+1 -1
View File
@@ -106,7 +106,7 @@ func CheckAppDisabled(storage storage.Storage) func(http.Handler) http.Handler {
return
}
if currentApp.ApplicationDisabled {
if !currentApp.Enabled {
http.Error(w, "Application disabled", http.StatusForbidden)
return
}
+19 -19
View File
@@ -22,15 +22,15 @@ import (
type Application struct {
sync.Mutex
Name string
AppID string
Key string
Secret string
OnlySSL bool
ApplicationDisabled bool
UserEvents bool
WebHooks bool
URLWebHook string
Name string
AppID string
Key string
Secret string
OnlySSL bool
Enabled bool
UserEvents bool
WebHooks bool
URLWebHook string
channels map[string]*channel.Channel `json:"-"`
connections map[string]*connection.Connection `json:"-"`
@@ -44,22 +44,22 @@ func NewApplication(
key,
secret string,
onlySSL,
disabled,
enabled,
userEvents,
webHooks bool,
webHookURL string,
) *Application {
a := &Application{
Name: name,
AppID: appID,
Key: key,
Secret: secret,
OnlySSL: onlySSL,
ApplicationDisabled: disabled,
UserEvents: userEvents,
WebHooks: webHooks,
URLWebHook: webHookURL,
Name: name,
AppID: appID,
Key: key,
Secret: secret,
OnlySSL: onlySSL,
Enabled: enabled,
UserEvents: userEvents,
WebHooks: webHooks,
URLWebHook: webHookURL,
}
a.connections = make(map[string]*connection.Connection)
+1 -1
View File
@@ -21,7 +21,7 @@ var (
// Main function, initialize the system
func main() {
var filename = flag.String("config", "config.json", "Config file location")
var filename = flag.String("config", "config.yml", "Config file location")
flag.Parse()
printBanner()
-21
View File
@@ -1,21 +0,0 @@
{
"Host": ":8080",
"SSL": false,
"Profiling": false,
"SSLHost": ":4433",
"SSLKeyFile": "A key.pem file",
"SSLCertFile": "A cert.pem file",
"Apps": [
{
"ApplicationDisabled": false,
"Secret": "A really secret random string",
"Key": "A random Key string",
"OnlySSL": false,
"Name": "The app name",
"AppID": "The app ID",
"UserEvents": true,
"WebHooks": true,
"URLWebHook": "Some URL to send webhooks"
}
]
}
+19
View File
@@ -0,0 +1,19 @@
---
host: ":8080"
profiling: false
ssl:
enabled: false
host: ":4343"
key_file: "key.pem"
cert_file: "cert.pem"
apps:
- name: "Sample Application"
enabled: true
only_ssl: false
key: "278d525bdf162c739803"
secret: "${APP_SECRET}" # Expand env vars
app_id: "1"
user_events: true
webhooks:
enabled: true
url: "http://127.0.0.1:5000/hook"
+23 -17
View File
@@ -6,25 +6,31 @@ package config
// The config file
type File struct {
Host string // The host, eg: :8080 will start on 0.0.0.0:8080
User string
SSL bool
Profiling bool
SSLHost string
SSLKeyFile string
SSLCertFile string
Host string `yaml:"host"` // The host, eg: :8080 will start on 0.0.0.0:8080
SSL SSL `yaml:"ssl"`
Profiling bool `yaml:"profiling"`
Apps []Application `yaml:"apps"`
}
Apps []Application
type SSL struct {
Enabled bool `yaml:"enabled"`
Host string `yaml:"host"`
KeyFile string `yaml:"key_file"`
CertFile string `yaml:"cert_file"`
}
type Application struct {
Name string
AppID string
Key string
Secret string
OnlySSL bool
ApplicationDisabled bool
UserEvents bool
WebHooks bool
URLWebHook string
Name string `yaml:"name"`
AppID string `yaml:"app_id"`
Key string `yaml:"key"`
Secret string `yaml:"secret"`
OnlySSL bool `yaml:"only_ssl"`
Enabled bool `yaml:"enabled"`
UserEvents bool `yaml:"user_events"`
WebHooks Webhooks `yaml:"webhooks"`
}
type Webhooks struct {
Enabled bool `yaml:"enabled"`
URL string `yaml:"url"`
}
+1 -1
View File
@@ -1,2 +1,2 @@
client: go run client.go
server: go run ../cmd/main.go -config ./functional-config.json -alsologtostderr
server: go run ../cmd/main.go -config ./functional-config.yml -alsologtostderr
-21
View File
@@ -1,21 +0,0 @@
{
"Host": ":8080",
"SSL": false,
"Profiling": true,
"SSLHost": ":8090",
"SSLKeyFile": "key.pem",
"SSLCertFile": "cert.pem",
"Apps": [
{
"ApplicationDisabled": false,
"OnlySSL": false,
"Secret": "7ad3753142a6693b25b9",
"Key": "278d525bdf162c739803",
"Name": "App for Functional Test",
"AppID": "1",
"UserEvents": true,
"WebHooks": true,
"URLWebHook": "http://127.0.0.1:5000/hook"
}
]
}
+19
View File
@@ -0,0 +1,19 @@
---
host: ':8080'
profiling: false
ssl:
enabled: false
host: ':4343'
key_file: 'key.pem'
cert_file: 'cert.pem'
apps:
- name: 'Sample Application'
enabled: true
only_ssl: false
key: '278d525bdf162c739803'
secret: '7ad3753142a6693b25b9'
app_id: '1'
user_events: true
webhooks:
enabled: true # Default is false
url: 'http://127.0.0.1:5000/hook'
+1
View File
@@ -11,4 +11,5 @@ require (
github.com/pusher/pusher-http-go v1.3.0
github.com/stretchr/testify v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869 // indirect
gopkg.in/yaml.v2 v2.2.1
)
+3
View File
@@ -18,3 +18,6 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869 h1:kkXA53yGe04D0adEYJwEVQjeBppL01Exg+fnMjfUraU=
golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+13 -15
View File
@@ -5,7 +5,7 @@
package ipe
import (
"encoding/json"
"io/ioutil"
"math/rand"
"net/http"
"os"
@@ -14,6 +14,7 @@ import (
log "github.com/golang/glog"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"gopkg.in/yaml.v2"
"ipe/api"
"ipe/app"
@@ -28,21 +29,18 @@ func Start(filename string) {
var conf config.File
rand.Seed(time.Now().Unix())
file, err := os.Open(filename)
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Error(err)
return
}
defer func() {
if err := file.Close(); err != nil {
log.Error(err)
}
}()
// Expand env vars
data = []byte(os.ExpandEnv(string(data)))
// Reading config
if err := json.NewDecoder(file).Decode(&conf); err != nil {
// Decoding config
if err := yaml.UnmarshalStrict(data, &conf); err != nil {
log.Error(err)
return
}
@@ -58,10 +56,10 @@ func Start(filename string) {
a.Key,
a.Secret,
a.OnlySSL,
a.ApplicationDisabled,
a.Enabled,
a.UserEvents,
a.WebHooks,
a.URLWebHook,
a.WebHooks.Enabled,
a.WebHooks.URL,
)
if err := inMemoryStorage.AddApp(application); err != nil {
@@ -96,10 +94,10 @@ func Start(filename string) {
api.NewGetChannelUsers(inMemoryStorage),
)
if conf.SSL {
if conf.SSL.Enabled {
go func() {
log.Infof("Starting HTTPS service on %s ...", conf.SSLHost)
log.Fatal(http.ListenAndServeTLS(conf.SSLHost, conf.SSLCertFile, conf.SSLKeyFile, router))
log.Infof("Starting HTTPS service on %s ...", conf.SSL.Host)
log.Fatal(http.ListenAndServeTLS(conf.SSL.Host, conf.SSL.CertFile, conf.SSL.KeyFile, router))
}()
}
+1 -1
View File
@@ -152,7 +152,7 @@ func onOpen(conn *websocket.Conn, r *http.Request, sessionID string, app *app.Ap
return noProtocolVersionSupplied
case protocol != supportedProtocolVersion:
return unsupportedProtocolVersion
case app.ApplicationDisabled:
case !app.Enabled:
return applicationDisabled
case app.OnlySSL:
if r.TLS == nil {