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/* ignore_http/*
config.json config.json
config.yml
*.pem *.pem
build build
.vscode/* .vscode/*
+1 -1
View File
@@ -106,7 +106,7 @@ func CheckAppDisabled(storage storage.Storage) func(http.Handler) http.Handler {
return return
} }
if currentApp.ApplicationDisabled { if !currentApp.Enabled {
http.Error(w, "Application disabled", http.StatusForbidden) http.Error(w, "Application disabled", http.StatusForbidden)
return return
} }
+19 -19
View File
@@ -22,15 +22,15 @@ import (
type Application struct { type Application struct {
sync.Mutex sync.Mutex
Name string Name string
AppID string AppID string
Key string Key string
Secret string Secret string
OnlySSL bool OnlySSL bool
ApplicationDisabled bool Enabled bool
UserEvents bool UserEvents bool
WebHooks bool WebHooks bool
URLWebHook string URLWebHook string
channels map[string]*channel.Channel `json:"-"` channels map[string]*channel.Channel `json:"-"`
connections map[string]*connection.Connection `json:"-"` connections map[string]*connection.Connection `json:"-"`
@@ -44,22 +44,22 @@ func NewApplication(
key, key,
secret string, secret string,
onlySSL, onlySSL,
disabled, enabled,
userEvents, userEvents,
webHooks bool, webHooks bool,
webHookURL string, webHookURL string,
) *Application { ) *Application {
a := &Application{ a := &Application{
Name: name, Name: name,
AppID: appID, AppID: appID,
Key: key, Key: key,
Secret: secret, Secret: secret,
OnlySSL: onlySSL, OnlySSL: onlySSL,
ApplicationDisabled: disabled, Enabled: enabled,
UserEvents: userEvents, UserEvents: userEvents,
WebHooks: webHooks, WebHooks: webHooks,
URLWebHook: webHookURL, URLWebHook: webHookURL,
} }
a.connections = make(map[string]*connection.Connection) a.connections = make(map[string]*connection.Connection)
+1 -1
View File
@@ -21,7 +21,7 @@ var (
// Main function, initialize the system // Main function, initialize the system
func main() { func main() {
var filename = flag.String("config", "config.json", "Config file location") var filename = flag.String("config", "config.yml", "Config file location")
flag.Parse() flag.Parse()
printBanner() 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 // The config file
type File struct { type File struct {
Host string // The host, eg: :8080 will start on 0.0.0.0:8080 Host string `yaml:"host"` // The host, eg: :8080 will start on 0.0.0.0:8080
User string SSL SSL `yaml:"ssl"`
SSL bool Profiling bool `yaml:"profiling"`
Profiling bool Apps []Application `yaml:"apps"`
SSLHost string }
SSLKeyFile string
SSLCertFile string
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 { type Application struct {
Name string Name string `yaml:"name"`
AppID string AppID string `yaml:"app_id"`
Key string Key string `yaml:"key"`
Secret string Secret string `yaml:"secret"`
OnlySSL bool OnlySSL bool `yaml:"only_ssl"`
ApplicationDisabled bool Enabled bool `yaml:"enabled"`
UserEvents bool UserEvents bool `yaml:"user_events"`
WebHooks bool WebHooks Webhooks `yaml:"webhooks"`
URLWebHook string }
type Webhooks struct {
Enabled bool `yaml:"enabled"`
URL string `yaml:"url"`
} }
+1 -1
View File
@@ -1,2 +1,2 @@
client: go run client.go 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/pusher/pusher-http-go v1.3.0
github.com/stretchr/testify v1.2.2 // indirect github.com/stretchr/testify v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869 // 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= 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 h1:kkXA53yGe04D0adEYJwEVQjeBppL01Exg+fnMjfUraU=
golang.org/x/crypto v0.0.0-20181112202954-3d3f9f413869/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 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 package ipe
import ( import (
"encoding/json" "io/ioutil"
"math/rand" "math/rand"
"net/http" "net/http"
"os" "os"
@@ -14,6 +14,7 @@ import (
log "github.com/golang/glog" log "github.com/golang/glog"
"github.com/gorilla/handlers" "github.com/gorilla/handlers"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"gopkg.in/yaml.v2"
"ipe/api" "ipe/api"
"ipe/app" "ipe/app"
@@ -28,21 +29,18 @@ func Start(filename string) {
var conf config.File var conf config.File
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
file, err := os.Open(filename)
data, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return return
} }
defer func() { // Expand env vars
if err := file.Close(); err != nil { data = []byte(os.ExpandEnv(string(data)))
log.Error(err)
}
}()
// Reading config // Decoding config
if err := json.NewDecoder(file).Decode(&conf); err != nil { if err := yaml.UnmarshalStrict(data, &conf); err != nil {
log.Error(err) log.Error(err)
return return
} }
@@ -58,10 +56,10 @@ func Start(filename string) {
a.Key, a.Key,
a.Secret, a.Secret,
a.OnlySSL, a.OnlySSL,
a.ApplicationDisabled, a.Enabled,
a.UserEvents, a.UserEvents,
a.WebHooks, a.WebHooks.Enabled,
a.URLWebHook, a.WebHooks.URL,
) )
if err := inMemoryStorage.AddApp(application); err != nil { if err := inMemoryStorage.AddApp(application); err != nil {
@@ -96,10 +94,10 @@ func Start(filename string) {
api.NewGetChannelUsers(inMemoryStorage), api.NewGetChannelUsers(inMemoryStorage),
) )
if conf.SSL { if conf.SSL.Enabled {
go func() { go func() {
log.Infof("Starting HTTPS service on %s ...", conf.SSLHost) log.Infof("Starting HTTPS service on %s ...", conf.SSL.Host)
log.Fatal(http.ListenAndServeTLS(conf.SSLHost, conf.SSLCertFile, conf.SSLKeyFile, router)) 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 return noProtocolVersionSupplied
case protocol != supportedProtocolVersion: case protocol != supportedProtocolVersion:
return unsupportedProtocolVersion return unsupportedProtocolVersion
case app.ApplicationDisabled: case !app.Enabled:
return applicationDisabled return applicationDisabled
case app.OnlySSL: case app.OnlySSL:
if r.TLS == nil { if r.TLS == nil {