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
This commit is contained in:
claudemiro
2016-03-03 21:01:21 -03:00
parent 55a7a0b96c
commit f3dba811b7
5 changed files with 43 additions and 52 deletions
+1
View File
@@ -155,4 +155,5 @@ flymake*
ignore_http/* ignore_http/*
config.json config.json
*.pem
+21 -5
View File
@@ -44,16 +44,32 @@ $ go install github.com/dimiro1/ipe
```json ```json
{ {
"Host": ":8080", "Host": ":8080",
"SSL": false,
"SSLHost": ":8090",
"SSLKeyFile": "key.pem",
"SSLCertFile": "cert.pem",
"Apps": [ "Apps": [
{ {
"ApplicationDisabled": false, "ApplicationDisabled": false,
"Secret": "APP_SECRET", "Secret": "7ad3753142a6693b25b9",
"Key": "APP_KEY", "Key": "278d525bdf162c739803",
"Name": "APP_NAME", "OnlySSL": false,
"AppID": "APP_ID", "Name": "App 1",
"AppID": "321",
"UserEvents": true, "UserEvents": true,
"WebHooks": 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"
} }
] ]
} }
+3 -3
View File
@@ -1,9 +1,9 @@
{ {
"Host": ":8080", "Host": ":8080",
"Encrypted": true, "SSL": false,
"SSLHost": ":8090", "SSLHost": ":8090",
"SSLPrivateKey": "/home/ssl.key", "SSLKeyFile": "key.pem",
"SSLPublicKey": "/home/ssl.crt", "SSLCertFile": "cert.pem",
"Apps": [ "Apps": [
{ {
"ApplicationDisabled": false, "ApplicationDisabled": false,
+8 -10
View File
@@ -11,17 +11,15 @@ import (
// The config file // The config file
type configFile struct { type configFile struct {
Host string // The host, eg: :8080 will start on 0.0.0.0:8080 Host string // The host, eg: :8080 will start on 0.0.0.0:8080
User string User string
Password string Password string
SSL bool
SSLHost string
SSLKeyFile string
SSLCertFile string
// SSL Configurations Apps []*app
Encrypted bool
SSLHost string
SSLPrivateKey string
SSLPublicKey string
Apps []*app
} }
// Initialize Apps // Initialize Apps
+10 -34
View File
@@ -7,45 +7,18 @@ package ipe
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/http"
"math/rand" "math/rand"
"net/http"
"time" "time"
"github.com/gorilla/mux"
log "github.com/golang/glog" log "github.com/golang/glog"
) )
// Conf holds the global configuration state // Conf holds the global configuration state
var conf configFile 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 // 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) { func Start(configfile string) {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
file, err := ioutil.ReadFile(configfile) file, err := ioutil.ReadFile(configfile)
@@ -61,10 +34,13 @@ func Start(configfile string) {
conf.Init() conf.Init()
router := newRouter() 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 { log.Infof("Starting HTTP service on %s ...", conf.Host)
case err := <-errs: log.Fatal(http.ListenAndServe(conf.Host, router))
log.Errorf("Could not start serving service due to (error: %s)", err)
}
} }