115 lines
3.2 KiB
Go
115 lines
3.2 KiB
Go
// VulcanizeDB
|
|
// Copyright © 2022 Vulcanize
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/vulcanize/leveldb-ethdb-rpc/pkg"
|
|
srpc "github.com/vulcanize/leveldb-ethdb-rpc/pkg/rpc"
|
|
)
|
|
|
|
var (
|
|
subCommand string
|
|
logWithCommand log.Entry
|
|
)
|
|
|
|
// serveCmd represents the serve command
|
|
var serveCmd = &cobra.Command{
|
|
Use: "serve",
|
|
Short: "RPC Server for LevelDB eth.Database",
|
|
Long: `This service exposes a remote RPC server interface for a local levelDB backed ethdb.Database`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
subCommand = cmd.CalledAs()
|
|
logWithCommand = *log.WithField("SubCommand", subCommand)
|
|
serve()
|
|
},
|
|
}
|
|
|
|
func serve() {
|
|
logWithCommand.Infof("running ipld-eth-server version: %s", v.VersionWithMeta)
|
|
|
|
wg := new(sync.WaitGroup)
|
|
logWithCommand.Debug("loading server configuration variables")
|
|
serverConfig, err := pkg.NewConfig()
|
|
if err != nil {
|
|
logWithCommand.Fatal(err)
|
|
}
|
|
logWithCommand.Infof("server config: %+v", serverConfig)
|
|
logWithCommand.Debug("initializing new server service")
|
|
server, err := pkg.NewServer(serverConfig)
|
|
if err != nil {
|
|
logWithCommand.Fatal(err)
|
|
}
|
|
|
|
logWithCommand.Info("starting up servers")
|
|
server.Serve(wg)
|
|
if err := startServers(server, serverConfig); err != nil {
|
|
logWithCommand.Fatal(err)
|
|
}
|
|
|
|
shutdown := make(chan os.Signal, 1)
|
|
signal.Notify(shutdown, os.Interrupt)
|
|
<-shutdown
|
|
server.Stop()
|
|
wg.Wait()
|
|
}
|
|
|
|
func startServers(server pkg.Server, settings pkg.Config) error {
|
|
if settings.IPCEnabled {
|
|
logWithCommand.Info("starting up IPC server")
|
|
_, _, err := srpc.StartIPCEndpoint(settings.IPCEndpoint, server.APIs())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
logWithCommand.Info("IPC server is disabled")
|
|
}
|
|
|
|
if settings.HTTPEnabled {
|
|
logWithCommand.Info("starting up HTTP server")
|
|
_, err := srpc.StartHTTPEndpoint(settings.HTTPEndpoint, server.APIs(), []string{"leveldb"}, nil, []string{"*"}, rpc.HTTPTimeouts{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
logWithCommand.Info("HTTP server is disabled")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(serveCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|