diff --git a/.gitignore b/.gitignore index f181a5f..ab6852e 100644 --- a/.gitignore +++ b/.gitignore @@ -155,6 +155,7 @@ flymake* ignore_http/* config.json +config.yml *.pem build .vscode/* diff --git a/api/handlers.go b/api/handlers.go index 8992a59..5f47e75 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -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 } diff --git a/app/app.go b/app/app.go index 4b67e48..966d7c9 100644 --- a/app/app.go +++ b/app/app.go @@ -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) diff --git a/cmd/main.go b/cmd/main.go index 6b10d75..fffa51f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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() diff --git a/config-example.json b/config-example.json deleted file mode 100644 index 91e4694..0000000 --- a/config-example.json +++ /dev/null @@ -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" - } - ] -} diff --git a/config-example.yml b/config-example.yml new file mode 100644 index 0000000..98a05c1 --- /dev/null +++ b/config-example.yml @@ -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" \ No newline at end of file diff --git a/config/config.go b/config/config.go index 822c429..f5e54fc 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` } diff --git a/functional/Procfile b/functional/Procfile index 66048ed..eaca5c6 100644 --- a/functional/Procfile +++ b/functional/Procfile @@ -1,2 +1,2 @@ client: go run client.go -server: go run ../cmd/main.go -config ./functional-config.json -alsologtostderr \ No newline at end of file +server: go run ../cmd/main.go -config ./functional-config.yml -alsologtostderr \ No newline at end of file diff --git a/functional/functional-config.json b/functional/functional-config.json deleted file mode 100644 index c26e528..0000000 --- a/functional/functional-config.json +++ /dev/null @@ -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" - } - ] -} diff --git a/functional/functional-config.yml b/functional/functional-config.yml new file mode 100644 index 0000000..5d8d88b --- /dev/null +++ b/functional/functional-config.yml @@ -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' \ No newline at end of file diff --git a/go.mod b/go.mod index 1557571..7f077b3 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 9b3e19c..ecb9c21 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/ipe.go b/ipe.go index d1fd2a4..1ed5ee1 100644 --- a/ipe.go +++ b/ipe.go @@ -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)) }() } diff --git a/websockets/websocket.go b/websockets/websocket.go index 87ff180..ec74183 100644 --- a/websockets/websocket.go +++ b/websockets/websocket.go @@ -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 {