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
+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))
}