diff --git a/erisdb/json_service.go b/erisdb/json_service.go index 97a7df78c571e45788c20c29ee66225474d5d393..fec59e473a18060132d05b87259837d1593066d5 100644 --- a/erisdb/json_service.go +++ b/erisdb/json_service.go @@ -2,11 +2,14 @@ package erisdb import ( "encoding/json" - "github.com/gin-gonic/gin" + "net/http" + + cfg "github.com/eris-ltd/eris-db/config" ep "github.com/eris-ltd/eris-db/erisdb/pipe" rpc "github.com/eris-ltd/eris-db/rpc" "github.com/eris-ltd/eris-db/server" - "net/http" + + "github.com/gin-gonic/gin" ) // Server used to handle JSON-RPC 2.0 requests. Implements server.Server @@ -21,7 +24,7 @@ func NewJsonRpcServer(service server.HttpService) *JsonRpcServer { } // Start adds the rpc path to the router. -func (this *JsonRpcServer) Start(config *server.ServerConfig, router *gin.Engine) { +func (this *JsonRpcServer) Start(config *cfg.ServerConfig, router *gin.Engine) { router.POST(config.HTTP.JsonRpcEndpoint, this.handleFunc) this.running = true } diff --git a/erisdb/restServer.go b/erisdb/restServer.go index 181edbba15ffdb7164d60b90adc14f7f4ba24684..5b0feee845139a3be10602c4cf33a1265d3412b7 100644 --- a/erisdb/restServer.go +++ b/erisdb/restServer.go @@ -8,9 +8,9 @@ import ( "github.com/gin-gonic/gin" + cfg "github.com/eris-ltd/eris-db/config" ep "github.com/eris-ltd/eris-db/erisdb/pipe" rpc "github.com/eris-ltd/eris-db/rpc" - "github.com/eris-ltd/eris-db/server" "github.com/eris-ltd/eris-db/txs" "github.com/eris-ltd/eris-db/util" ) @@ -31,7 +31,7 @@ func NewRestServer(codec rpc.Codec, pipe ep.Pipe, eventSubs *EventSubscriptions) } // Starting the server means registering all the handlers with the router. -func (this *RestServer) Start(config *server.ServerConfig, router *gin.Engine) { +func (this *RestServer) Start(config *cfg.ServerConfig, router *gin.Engine) { // Accounts router.GET("/accounts", parseSearchQuery, this.handleAccounts) router.GET("/accounts/:address", addressParam, this.handleAccount) diff --git a/erisdb/serve.go b/erisdb/serve.go index 8b3ebe5ee5e639244705a09e71dfc0f371e3f17f..6379432609cad8ab25318f949f34248e03311224 100644 --- a/erisdb/serve.go +++ b/erisdb/serve.go @@ -24,12 +24,14 @@ import ( tmspcli "github.com/tendermint/tmsp/client" tmsp "github.com/tendermint/tmsp/server" - tmcfg "github.com/eris-ltd/eris-db/config/tendermint" + edbcfg "github.com/eris-ltd/eris-db/config" ep "github.com/eris-ltd/eris-db/erisdb/pipe" "github.com/eris-ltd/eris-db/server" sm "github.com/eris-ltd/eris-db/state" stypes "github.com/eris-ltd/eris-db/state/types" edbapp "github.com/eris-ltd/eris-db/tmsp" + + tmcfg "github.com/tendermint/tendermint/config/tendermint" // for inproc only! ) const ERISDB_VERSION = "0.11.5" @@ -59,19 +61,19 @@ func ServeErisDB(workDir string, inProc bool) (*server.ServeProcess, error) { // but cfg mgmt across projects probably often is! // Get an erisdb configuration - var sConf *server.ServerConfig - sConfPath := path.Join(workDir, "config.toml") - if !FileExists(sConfPath) { + var edbConf *edbcfg.ErisDBConfig + edbConfPath := path.Join(workDir, "config.toml") + if !FileExists(edbConfPath) { log.Info("No server configuration, using default.") - log.Info("Writing to: " + sConfPath) - sConf = server.DefaultServerConfig() - errW := server.WriteServerConfig(sConfPath, sConf) + log.Info("Writing to: " + edbConfPath) + edbConf = edbcfg.DefaultErisDBConfig() + errW := edbcfg.WriteErisDBConfig(edbConfPath, edbConf) if errW != nil { panic(errW) } } else { var errRSC error - sConf, errRSC = server.ReadServerConfig(sConfPath) + edbConf, errRSC = edbcfg.ReadErisDBConfig(edbConfPath) if errRSC != nil { log.Error("Server config file error.", "error", errRSC.Error()) } @@ -86,11 +88,11 @@ func ServeErisDB(workDir string, inProc bool) (*server.ServeProcess, error) { // The app state used to be managed by tendermint node, // but is now managed by ErisDB. // The tendermint core only stores the blockchain (history of txs) - stateDB := dbm.GetDB("app_state", config.GetString("erisdb.db_backend"), config.GetString("erisdb.db_dir")) + stateDB := dbm.GetDB("app_state", edbConf.DB.Backend, workDir+"/data") state := sm.LoadState(stateDB) var genDoc *stypes.GenesisDoc if state == nil { - genDoc, state = sm.MakeGenesisStateFromFile(stateDB, config.GetString("erisdb.genesis_file")) + genDoc, state = sm.MakeGenesisStateFromFile(stateDB, workDir+"/genesis.json") state.Save() buf, n, err := new(bytes.Buffer), new(int), new(error) wire.WriteJSON(genDoc, buf, n, err) @@ -121,7 +123,7 @@ func ServeErisDB(workDir string, inProc bool) (*server.ServeProcess, error) { app := edbapp.NewErisDBApp(state, evsw) // so we know where to find the consensus host (for eg. blockchain/consensus rpcs) - app.SetHostAddress(sConf.Consensus.TendermintHost) + app.SetHostAddress(edbConf.Tendermint.Host) if inProc { fmt.Println("Starting tm node in proc") startTMNode(app) @@ -130,7 +132,7 @@ func ServeErisDB(workDir string, inProc bool) (*server.ServeProcess, error) { // Start the tmsp listener for state update commands go func() { // TODO config - _, err := tmsp.NewServer(sConf.Consensus.TMSPListener, app) + _, err := tmsp.NewServer(edbConf.TMSP.Listener, app) if err != nil { // TODO: play nice Exit(err.Error()) @@ -151,9 +153,9 @@ func ServeErisDB(workDir string, inProc bool) (*server.ServeProcess, error) { // The servers. jsonServer := NewJsonRpcServer(tmjs) restServer := NewRestServer(codec, pipe, evtSubs) - wsServer := server.NewWebSocketServer(sConf.WebSocket.MaxWebSocketSessions, tmwss) + wsServer := server.NewWebSocketServer(edbConf.Server.WebSocket.MaxWebSocketSessions, tmwss) // Create a server process. - proc := server.NewServeProcess(sConf, jsonServer, restServer, wsServer) + proc := server.NewServeProcess(&edbConf.Server, jsonServer, restServer, wsServer) return proc, nil } diff --git a/server/config.go b/server/config.go deleted file mode 100644 index 24b948926c9f153defb2f361e9850ad8512a2ce6..0000000000000000000000000000000000000000 --- a/server/config.go +++ /dev/null @@ -1,119 +0,0 @@ -package server - -import ( - "github.com/eris-ltd/eris-db/files" - "github.com/naoina/toml" -) - -// Standard configuration file for the server. -type ( - ServerConfig struct { - Bind Bind `toml:"bind"` - TLS TLS `toml:"TLS"` - CORS CORS `toml:"CORS"` - HTTP HTTP `toml:"HTTP"` - WebSocket WebSocket `toml:"web_socket"` - Logging Logging `toml:"logging"` - Consensus Consensus `toml:"consensus"` - } - - Bind struct { - Address string `toml:"address"` - Port uint16 `toml:"port"` - } - - TLS struct { - TLS bool `toml:"tls"` - CertPath string `toml:"cert_path"` - KeyPath string `toml:"key_path"` - } - - // Options stores configurations - CORS struct { - Enable bool `toml:"enable"` - AllowOrigins []string `toml:"allow_origins"` - AllowCredentials bool `toml:"allow_credentials"` - AllowMethods []string `toml:"allow_methods"` - AllowHeaders []string `toml:"allow_headers"` - ExposeHeaders []string `toml:"expose_headers"` - MaxAge uint64 `toml:"max_age"` - } - - HTTP struct { - JsonRpcEndpoint string `toml:"json_rpc_endpoint"` - } - - WebSocket struct { - WebSocketEndpoint string `toml:"websocket_endpoint"` - MaxWebSocketSessions uint `toml:"max_websocket_sessions"` - ReadBufferSize uint `toml:"read_buffer_size"` - WriteBufferSize uint `toml:"write_buffer_size"` - } - - Logging struct { - ConsoleLogLevel string `toml:"console_log_level"` - FileLogLevel string `toml:"file_log_level"` - LogFile string `toml:"log_file"` - VMLog bool `toml:"vm_log"` - } - - Consensus struct { - TendermintHost string `toml:"tendermint_host"` - TMSPListener string `toml:"tmsp_listener"` - } -) - -func DefaultServerConfig() *ServerConfig { - cp := "" - kp := "" - return &ServerConfig{ - Bind: Bind{ - Address: "", - Port: 1337, - }, - TLS: TLS{TLS: false, - CertPath: cp, - KeyPath: kp, - }, - CORS: CORS{}, - HTTP: HTTP{JsonRpcEndpoint: "/rpc"}, - WebSocket: WebSocket{ - WebSocketEndpoint: "/socketrpc", - MaxWebSocketSessions: 50, - ReadBufferSize: 4096, - WriteBufferSize: 4096, - }, - Logging: Logging{ - ConsoleLogLevel: "info", - FileLogLevel: "warn", - LogFile: "", - }, - Consensus: Consensus{ - TendermintHost: "0.0.0.0:46657", - TMSPListener: "tcp://0.0.0.0:46658", - }, - } -} - -// Read a TOML server configuration file. -func ReadServerConfig(filePath string) (*ServerConfig, error) { - bts, err := files.ReadFile(filePath) - if err != nil { - return nil, err - } - cfg := &ServerConfig{} - err2 := toml.Unmarshal(bts, cfg) - if err2 != nil { - return nil, err2 - } - return cfg, nil -} - -// Write a server configuration file. -func WriteServerConfig(filePath string, cfg *ServerConfig) error { - bts, err := toml.Marshal(*cfg) - if err != nil { - return err - } - return files.WriteAndBackup(filePath, bts) -} diff --git a/server/logging.go b/server/logging.go index a5a70031af162f749641075bd1dd01e32087946e..7c8b105d891b539dfcf3b8d3812519a4259064d4 100644 --- a/server/logging.go +++ b/server/logging.go @@ -2,15 +2,18 @@ package server import ( "fmt" - "github.com/tendermint/log15" "os" + + "github.com/tendermint/log15" + + cfg "github.com/eris-ltd/eris-db/config" ) var rootHandler log15.Handler // This is basically the same code as in tendermint. Initialize root // and maybe later also track the loggers here. -func InitLogger(config *ServerConfig) { +func InitLogger(config *cfg.ServerConfig) { consoleLogLevel := config.Logging.ConsoleLogLevel diff --git a/server/server.go b/server/server.go index e6bc35dd44ff41a187084a9f21002001172b3bda..c7e3dc465364f66c7bf848f1c8e931d4b8c5e0fc 100644 --- a/server/server.go +++ b/server/server.go @@ -3,12 +3,15 @@ package server import ( "crypto/tls" "fmt" - "github.com/gin-gonic/gin" - cors "github.com/tommy351/gin-cors" - "gopkg.in/tylerb/graceful.v1" "net" "net/http" "time" + + cfg "github.com/eris-ltd/eris-db/config" + + "github.com/gin-gonic/gin" + cors "github.com/tommy351/gin-cors" + "gopkg.in/tylerb/graceful.v1" ) var ( @@ -21,7 +24,7 @@ type HttpService interface { // A server serves a number of different http calls. type Server interface { - Start(*ServerConfig, *gin.Engine) + Start(*cfg.ServerConfig, *gin.Engine) Running() bool ShutDown() } @@ -35,7 +38,7 @@ type Server interface { // 'Start()'. Stop event listeners can be added up to the point where // the server is stopped and the event is fired. type ServeProcess struct { - config *ServerConfig + config *cfg.ServerConfig servers []Server stopChan chan struct{} stoppedChan chan struct{} @@ -183,18 +186,18 @@ func (this *ServeProcess) StopEventChannel() <-chan struct{} { } // Creates a new serve process. -func NewServeProcess(config *ServerConfig, servers ...Server) *ServeProcess { - var cfg *ServerConfig +func NewServeProcess(config *cfg.ServerConfig, servers ...Server) *ServeProcess { + var scfg cfg.ServerConfig if config == nil { - cfg = DefaultServerConfig() + scfg = cfg.DefaultServerConfig() } else { - cfg = config + scfg = *config } stopChan := make(chan struct{}, 1) stoppedChan := make(chan struct{}, 1) startListeners := make([]chan struct{}, 0) stopListeners := make([]chan struct{}, 0) - sp := &ServeProcess{cfg, servers, stopChan, stoppedChan, startListeners, stopListeners, nil} + sp := &ServeProcess{&scfg, servers, stopChan, stoppedChan, startListeners, stopListeners, nil} return sp } @@ -216,7 +219,7 @@ func logHandler(c *gin.Context) { } -func NewCORSMiddleware(options CORS) gin.HandlerFunc { +func NewCORSMiddleware(options cfg.CORS) gin.HandlerFunc { o := cors.Options{ AllowCredentials: options.AllowCredentials, AllowHeaders: options.AllowHeaders, diff --git a/server/websocket.go b/server/websocket.go index 09d6a7b8e1f2c6f88c4e39c6e33d80b8aecb4794..2fde5ca59f2d75312edbc6d0838f26b505cdd2e1 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -2,11 +2,14 @@ package server import ( "fmt" - "github.com/gin-gonic/gin" - "github.com/gorilla/websocket" "net/http" "sync" "time" + + cfg "github.com/eris-ltd/eris-db/config" + + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" ) // TODO too much fluff. Should probably phase gorilla out and move closer @@ -39,7 +42,7 @@ type WebSocketServer struct { running bool maxSessions uint sessionManager *SessionManager - config *ServerConfig + config *cfg.ServerConfig allOrigins bool } @@ -56,7 +59,7 @@ func NewWebSocketServer(maxSessions uint, service WebSocketService) *WebSocketSe } // Start the server. Adds the handler to the router and sets everything up. -func (this *WebSocketServer) Start(config *ServerConfig, router *gin.Engine) { +func (this *WebSocketServer) Start(config *cfg.ServerConfig, router *gin.Engine) { this.config = config