Both server running. SSL and Non SSL

This commit is contained in:
Welington Sampaio
2016-03-03 17:12:51 -03:00
parent e93145efe7
commit 03e94887a7
2 changed files with 39 additions and 22 deletions
+38 -18
View File
@@ -10,41 +10,61 @@ import (
"net/http" "net/http"
"math/rand" "math/rand"
"time" "time"
"github.com/gorilla/mux"
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
func Start(configfile string) error { func Start(configfile string) {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
file, err := ioutil.ReadFile(configfile) file, err := ioutil.ReadFile(configfile)
if err != nil { if err != nil {
return err log.Fatal(err)
} }
if err := json.Unmarshal(file, &conf); err != nil { if err := json.Unmarshal(file, &conf); err != nil {
return err log.Fatal(err)
} }
conf.Init() conf.Init()
router := newRouter() router := newRouter()
if conf.Encrypted { errs := Run(conf, router)
go func () {
if err := http.ListenAndServe(conf.Host, router); err != nil {
return err
}
}
if err := http.ListenAndServeTLS(conf.SSLHost, conf.SSLPublicKey, conf.SSLPrivateKey, router); err != nil {
return err
}
}else{
if err := http.ListenAndServe(conf.Host, router); err != nil {
return err
}
}
return nil select {
case err := <-errs:
log.Errorf("Could not start serving service due to (error: %s)", err)
}
} }
+1 -4
View File
@@ -9,7 +9,6 @@ import (
"fmt" "fmt"
"github.com/dimiro1/ipe/ipe" "github.com/dimiro1/ipe/ipe"
log "github.com/golang/glog"
) )
// Main function, initialze the system // Main function, initialze the system
@@ -19,9 +18,7 @@ func main() {
printBanner() printBanner()
if err := ipe.Start(*filename); err != nil { ipe.Start(*filename)
log.Fatal(err)
}
} }
// Print a beautifull banner // Print a beautifull banner