From f3dba811b71642f59240050d4c7f5696c2667942 Mon Sep 17 00:00:00 2001 From: claudemiro Date: Thu, 3 Mar 2016 21:01:21 -0300 Subject: [PATCH] Improved Handling SSL * Panic is betten than using channels in this case * Changed the names to a more recognized names * Improved the README with a full config file example --- .gitignore | 1 + README.md | 26 +++++++++++++++++++----- ipe/config-example.json | 6 +++--- ipe/config.go | 18 ++++++++--------- ipe/ipe.go | 44 ++++++++++------------------------------- 5 files changed, 43 insertions(+), 52 deletions(-) diff --git a/.gitignore b/.gitignore index ff27f52..fa1bd42 100644 --- a/.gitignore +++ b/.gitignore @@ -155,4 +155,5 @@ flymake* ignore_http/* config.json +*.pem diff --git a/README.md b/README.md index a1a3a81..6c8519a 100644 --- a/README.md +++ b/README.md @@ -44,16 +44,32 @@ $ go install github.com/dimiro1/ipe ```json { "Host": ":8080", + "SSL": false, + "SSLHost": ":8090", + "SSLKeyFile": "key.pem", + "SSLCertFile": "cert.pem", "Apps": [ { "ApplicationDisabled": false, - "Secret": "APP_SECRET", - "Key": "APP_KEY", - "Name": "APP_NAME", - "AppID": "APP_ID", + "Secret": "7ad3753142a6693b25b9", + "Key": "278d525bdf162c739803", + "OnlySSL": false, + "Name": "App 1", + "AppID": "321", "UserEvents": true, "WebHooks": true, - "URLWebHook": "http://localhost:4567/php/hook.php" + "URLWebHook": "http://127.0.0.1:4567/php/hook.php" + }, + { + "ApplicationDisabled": false, + "Secret": "d6824d2fa32888931504", + "Key": "c8b30f611ffb13202976", + "OnlySSL": false, + "Name": "App 2", + "AppID": "123", + "UserEvents": true, + "WebHooks": false, + "URLWebHook": "http://127.0.0.1:4567/php/hook.php" } ] } diff --git a/ipe/config-example.json b/ipe/config-example.json index 434e599..873cce9 100644 --- a/ipe/config-example.json +++ b/ipe/config-example.json @@ -1,9 +1,9 @@ { "Host": ":8080", - "Encrypted": true, + "SSL": false, "SSLHost": ":8090", - "SSLPrivateKey": "/home/ssl.key", - "SSLPublicKey": "/home/ssl.crt", + "SSLKeyFile": "key.pem", + "SSLCertFile": "cert.pem", "Apps": [ { "ApplicationDisabled": false, diff --git a/ipe/config.go b/ipe/config.go index 974ff23..b0d5b14 100644 --- a/ipe/config.go +++ b/ipe/config.go @@ -11,17 +11,15 @@ import ( // The config file type configFile struct { - Host string // The host, eg: :8080 will start on 0.0.0.0:8080 - User string - Password string + Host string // The host, eg: :8080 will start on 0.0.0.0:8080 + User string + Password string + SSL bool + SSLHost string + SSLKeyFile string + SSLCertFile string - // SSL Configurations - Encrypted bool - SSLHost string - SSLPrivateKey string - SSLPublicKey string - - Apps []*app + Apps []*app } // Initialize Apps diff --git a/ipe/ipe.go b/ipe/ipe.go index a26d941..e699455 100644 --- a/ipe/ipe.go +++ b/ipe/ipe.go @@ -7,45 +7,18 @@ package ipe import ( "encoding/json" "io/ioutil" - "net/http" "math/rand" + "net/http" "time" - "github.com/gorilla/mux" log "github.com/golang/glog" ) // Conf holds the global configuration state var conf configFile -func Run(conf configFile, router *mux.Router) chan error { - - errs := make(chan error) - - // Starting HTTP server - go func() { - log.Infof("Staring HTTP service on %s ...", conf.Host) - - if err := http.ListenAndServe(conf.Host, router); err != nil { - errs <- err - } - - }() - - if conf.Encrypted { - // Starting HTTPS server - go func() { - log.Infof("Staring HTTPS service on %s ...", conf.SSLHost) - if err := http.ListenAndServeTLS(conf.SSLHost, conf.SSLPublicKey, conf.SSLPrivateKey, router); err != nil { - errs <- err - } - }() - } - - return errs -} - // Start Parse the configuration file and starts the ipe server +// It Panic if could not start the HTTP or HTTPS server func Start(configfile string) { rand.Seed(time.Now().Unix()) file, err := ioutil.ReadFile(configfile) @@ -61,10 +34,13 @@ func Start(configfile string) { conf.Init() router := newRouter() - errs := Run(conf, router) + if conf.SSL { + go func() { + log.Infof("Starting HTTPS service on %s ...", conf.SSLHost) + log.Fatal(http.ListenAndServeTLS(conf.SSLHost, conf.SSLCertFile, conf.SSLKeyFile, router)) + }() + } - select { - case err := <-errs: - log.Errorf("Could not start serving service due to (error: %s)", err) - } + log.Infof("Starting HTTP service on %s ...", conf.Host) + log.Fatal(http.ListenAndServe(conf.Host, router)) }