diff --git a/cmd/serve.go b/cmd/serve.go index b99ae99c21c295b8b819df7bf4b566bce9874b56..9c4c293ebc06d1774feeee412c84e102dab6ceed 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 9dbbe622f9083f9784c025c0a58905861b3eb75e..4ad075592371b5310636ff6ea977929f4574e4f4 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 3f79d1fbbfc2bb2a2129c4413e88c9aa53d508e7..8ecde83da7fd8a2989de6906562a1a04356e3965 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 e385870fbbd9deb65f63cfcf75826354bf6b91a9..9d10ff952925362d4b1818e4fae1b40db24b418d 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 766eeef5307cc2dc25f26e91d74624256e5f4530..a5767141c8710a3b854f6d3efc35daf2791eb4e5 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 0c2793ac2654721a7ab50d071d3adddc5162c12d..c67274ace3692e09e7d2d0c8cb6247b86e27ce96 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