Fix #102
This commit is contained in:
@@ -18,10 +18,22 @@ package control
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Run Starts the control server listening in bindAddr
|
||||
func Run(bindAddr string) {
|
||||
log.Fatal(http.ListenAndServe(bindAddr, configRouter()))
|
||||
func Run(bindAddr string, wg *sync.WaitGroup) {
|
||||
|
||||
listener, err := net.Listen("tcp", bindAddr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Signal startup
|
||||
log.Printf("ControlServer listening at %s\n", bindAddr)
|
||||
wg.Done()
|
||||
|
||||
log.Fatal(http.Serve(listener, configRouter()))
|
||||
}
|
||||
|
||||
@@ -18,7 +18,9 @@ package data
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/httperror"
|
||||
"github.com/gorilla/mux"
|
||||
@@ -43,7 +45,7 @@ func configRouter(rs []routeSpec) (r *mux.Router) {
|
||||
return r
|
||||
}
|
||||
|
||||
func Run(bindAddr string) {
|
||||
func Run(bindAddr string, wg *sync.WaitGroup) {
|
||||
rs := []routeSpec{
|
||||
// request
|
||||
{"/handlers/{handlerID}/request/method", "GET", getRequestMethod},
|
||||
@@ -65,5 +67,15 @@ func Run(bindAddr string) {
|
||||
{"/handlers/{handlerID}/response/body", "PUT", lockResponseWriter(setResponseBody)},
|
||||
{"/handlers/{handlerID}/response/stream", "PUT", lockResponseWriter(setResponseBody)},
|
||||
}
|
||||
log.Fatal(http.ListenAndServe(bindAddr, configRouter(rs)))
|
||||
|
||||
listener, err := net.Listen("tcp", bindAddr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Signal startup
|
||||
log.Printf("DataServer listening at %s\n", bindAddr)
|
||||
wg.Done()
|
||||
|
||||
log.Fatal(http.Serve(listener, configRouter(rs)))
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/control"
|
||||
"github.com/BBVA/kapow/internal/server/data"
|
||||
"github.com/BBVA/kapow/internal/server/user"
|
||||
@@ -35,10 +37,12 @@ type ServerConfig struct {
|
||||
|
||||
// StartServer Starts one instance of each server in a goroutine and remains listening on a channel for trace events generated by them
|
||||
func StartServer(config ServerConfig) {
|
||||
go control.Run(config.ControlBindAddr)
|
||||
go data.Run(config.DataBindAddr)
|
||||
go user.Run(config.UserBindAddr, config.CertFile, config.KeyFile, config.ClientCaFile, config.ClientAuth)
|
||||
var wg = sync.WaitGroup{}
|
||||
wg.Add(3)
|
||||
go control.Run(config.ControlBindAddr, &wg)
|
||||
go data.Run(config.DataBindAddr, &wg)
|
||||
go user.Run(config.UserBindAddr, &wg, config.CertFile, config.KeyFile, config.ClientCaFile, config.ClientAuth)
|
||||
|
||||
// Wait for ever
|
||||
select {}
|
||||
// Wait for servers signals in order to return
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
@@ -22,7 +22,9 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/BBVA/kapow/internal/server/user/mux"
|
||||
)
|
||||
@@ -33,12 +35,17 @@ var Server = http.Server{
|
||||
}
|
||||
|
||||
// Run finishes configuring Server and runs ListenAndServe on it
|
||||
func Run(bindAddr, certFile, keyFile, cliCaFile string, cliAuth bool) {
|
||||
func Run(bindAddr string, wg *sync.WaitGroup, certFile, keyFile, cliCaFile string, cliAuth bool) {
|
||||
Server = http.Server{
|
||||
Addr: bindAddr,
|
||||
Handler: mux.New(),
|
||||
}
|
||||
|
||||
listener, err := net.Listen("tcp", bindAddr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if (certFile != "") && (keyFile != "") {
|
||||
if cliAuth {
|
||||
if Server.TLSConfig == nil {
|
||||
@@ -59,13 +66,17 @@ func Run(bindAddr, certFile, keyFile, cliCaFile string, cliAuth bool) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := Server.ListenAndServeTLS(certFile, keyFile); err != http.ErrServerClosed {
|
||||
log.Fatalf("UserServer failed: %s", err)
|
||||
}
|
||||
// Signal startup
|
||||
log.Printf("UserServer listening at %s\n", bindAddr)
|
||||
wg.Done()
|
||||
|
||||
log.Fatal(Server.ServeTLS(listener, certFile, keyFile))
|
||||
} else {
|
||||
if err := Server.ListenAndServe(); err != http.ErrServerClosed {
|
||||
log.Fatalf("UserServer failed: %s", err)
|
||||
}
|
||||
// Signal startup
|
||||
log.Printf("UserServer listening at %s\n", bindAddr)
|
||||
wg.Done()
|
||||
|
||||
log.Fatal(Server.Serve(listener))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user