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/*
config.json
*.pem
+21 -5
View File
@@ -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"
}
]
}
+3 -3
View File
@@ -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,
+8 -10
View File
@@ -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
+10 -34
View File
@@ -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))
}