From 342dd2e1a56894c63b48aafa4f6b44ab23c82ab9 Mon Sep 17 00:00:00 2001
From: Benjamin Bollen <ben@erisindustries.com>
Date: Tue, 14 Jun 2016 16:36:23 +0200
Subject: [PATCH] load ServerConfig into e-db servers structs; correct uint16;
 connect to cmd/serve

---
 cmd/serve.go         | 11 ++++++++++-
 core/config.go       | 13 +++++++++++++
 core/core.go         | 26 ++++++++++++++++++++++----
 core/json_service.go |  4 ++--
 server/websocket.go  | 10 +++++-----
 server_config.toml   |  1 -
 6 files changed, 52 insertions(+), 13 deletions(-)

diff --git a/cmd/serve.go b/cmd/serve.go
index b99ae99c..9c4c293e 100644
--- a/cmd/serve.go
+++ b/cmd/serve.go
@@ -124,10 +124,19 @@ func Serve(cmd *cobra.Command, args []string) {
     "consensusModule": consensusConfig.Version,
     "applicationManager": managerConfig.Version,
   }).Debug("Modules configured")
-  _, err = core.NewCore(do.ChainId, consensusConfig, managerConfig)
+
+  newCore, err := core.NewCore(do.ChainId, consensusConfig, managerConfig)
   if err != nil {
     log.Fatalf("Failed to load core: %s", err)
   }
+
+  serverConfig, err := core.LoadServerConfig(do)
+  if err != nil {
+    log.Fatalf("Failed to load server configuration: %s.", err)
+    os.Exit(1)
+  }
+  _, err = newCore.NewGateway(serverConfig)
+
 }
 
 //------------------------------------------------------------------------------
diff --git a/core/config.go b/core/config.go
index 9dbbe622..4ad07559 100644
--- a/core/config.go
+++ b/core/config.go
@@ -28,6 +28,7 @@ import (
   config      "github.com/eris-ltd/eris-db/config"
   definitions "github.com/eris-ltd/eris-db/definitions"
   manager     "github.com/eris-ltd/eris-db/manager"
+  server      "github.com/eris-ltd/eris-db/server"
   util        "github.com/eris-ltd/eris-db/util"
   version     "github.com/eris-ltd/eris-db/version"
 )
@@ -89,6 +90,18 @@ func loadModuleConfig(do *definitions.Do, module string) (*config.ModuleConfig,
   }, nil
 }
 
+// LoadServerModuleConfig wraps specifically for the servers run by core
+func LoadServerConfig(do *definitions.Do) (*server.ServerConfig, error) {
+  // load configuration subtree for servers
+  subConfig := do.Config.Sub("servers")
+  if subConfig == nil {
+    return nil,
+      fmt.Errorf("Failed to read configuration section for servers")
+  }
+  return server.ReadServerConfig(subConfig)
+}
+
+
 //------------------------------------------------------------------------------
 // Helper functions
 
diff --git a/core/core.go b/core/core.go
index 3f79d1fb..8ecde83d 100644
--- a/core/core.go
+++ b/core/core.go
@@ -26,8 +26,10 @@ import (
 
   config      "github.com/eris-ltd/eris-db/config"
   consensus   "github.com/eris-ltd/eris-db/consensus"
+  core_types  "github.com/eris-ltd/eris-db/core/types"
   definitions "github.com/eris-ltd/eris-db/definitions"
   manager     "github.com/eris-ltd/eris-db/manager"
+  server      "github.com/eris-ltd/eris-db/server"
 )
 
 // Core is the high-level structure
@@ -72,7 +74,23 @@ func NewCore(chainId string, consensusConfig *config.ModuleConfig,
 // NOTE: [ben] in phase 0 we exactly take over the full server architecture
 // from Eris-DB and Tendermint; This is a draft and will be overhauled.
 
-// func (core *Core) StartGateway() {
-//   codec := &TCodec{}
-//   eventSubscriptions :=
-// }
+func (core *Core) NewGateway(config *server.ServerConfig) (*server.ServeProcess,
+  error) {
+  codec := &core_types.TCodec{}
+  eventSubscriptions := NewEventSubscriptions(core.pipe.Events())
+  // The services.
+  tmwss := NewErisDbWsService(codec, core.pipe)
+  tmjs := NewErisDbJsonService(codec, core.pipe, eventSubscriptions)
+  // The servers.
+	jsonServer := NewJsonRpcServer(tmjs)
+	restServer := NewRestServer(codec, core.pipe, eventSubscriptions)
+	wsServer := server.NewWebSocketServer(config.WebSocket.MaxWebSocketSessions,
+    tmwss)
+	// Create a server process.
+	proc, err := server.NewServeProcess(config, jsonServer, restServer, wsServer)
+  if err != nil {
+    return nil, fmt.Errorf("Failed to load gateway: %v", err)
+  }
+
+	return proc, nil
+}
diff --git a/core/json_service.go b/core/json_service.go
index e385870f..9d10ff95 100644
--- a/core/json_service.go
+++ b/core/json_service.go
@@ -26,8 +26,8 @@ func NewJsonRpcServer(service server.HttpService) *JsonRpcServer {
 }
 
 // Start adds the rpc path to the router.
-func (this *JsonRpcServer) Start(jsonRpcEndpoint string, router *gin.Engine) {
-	router.POST(jsonRpcEndpoint, this.handleFunc)
+func (this *JsonRpcServer) Start(config *server.ServerConfig, router *gin.Engine) {
+	router.POST(config.HTTP.JsonRpcEndpoint, this.handleFunc)
 	this.running = true
 }
 
diff --git a/server/websocket.go b/server/websocket.go
index 766eeef5..a5767141 100644
--- a/server/websocket.go
+++ b/server/websocket.go
@@ -38,7 +38,7 @@ type WebSocketService interface {
 type WebSocketServer struct {
 	upgrader       websocket.Upgrader
 	running        bool
-	maxSessions    uint
+	maxSessions    uint16
 	sessionManager *SessionManager
 	config         *ServerConfig
 	allOrigins     bool
@@ -49,7 +49,7 @@ type WebSocketServer struct {
 // NOTE: This is not the total number of connections allowed - only those that are
 // upgraded to websockets. Requesting a websocket connection will fail with a 503 if
 // the server is at capacity.
-func NewWebSocketServer(maxSessions uint, service WebSocketService) *WebSocketServer {
+func NewWebSocketServer(maxSessions uint16, service WebSocketService) *WebSocketServer {
 	return &WebSocketServer{
 		maxSessions:    maxSessions,
 		sessionManager: NewSessionManager(maxSessions, service),
@@ -290,7 +290,7 @@ func (this *WSSession) writePump() {
 
 // Session manager handles the adding, tracking and removing of session objects.
 type SessionManager struct {
-	maxSessions     uint
+	maxSessions     uint16
 	activeSessions  map[uint]*WSSession
 	idPool          *IdPool
 	mtx             *sync.Mutex
@@ -300,11 +300,11 @@ type SessionManager struct {
 }
 
 // Create a new WebsocketManager.
-func NewSessionManager(maxSessions uint, wss WebSocketService) *SessionManager {
+func NewSessionManager(maxSessions uint16, wss WebSocketService) *SessionManager {
 	return &SessionManager{
 		maxSessions:     maxSessions,
 		activeSessions:  make(map[uint]*WSSession),
-		idPool:          NewIdPool(maxSessions),
+		idPool:          NewIdPool(uint(maxSessions)),
 		mtx:             &sync.Mutex{},
 		service:         wss,
 		openEventChans:  []chan *WSSession{},
diff --git a/server_config.toml b/server_config.toml
index 0c2793ac..c67274ac 100644
--- a/server_config.toml
+++ b/server_config.toml
@@ -88,7 +88,6 @@ genesis_file = "genesis.json"
   json_rpc_endpoint="/rpc"
 
   [servers.websocket]
-  enable = true
   endpoint = "/socketrpc"
   max_sessions = 50
   read_buffer_size = 4096
-- 
GitLab