diff --git a/cmd/root.go b/cmd/root.go index d69d435..203d807 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,5 +1,5 @@ // VulcanizeDB -// Copyright © 2021 Vulcanize +// 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 @@ -18,9 +18,10 @@ package cmd import ( "fmt" - "github.com/spf13/cobra" "os" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) diff --git a/cmd/serve.go b/cmd/serve.go index 77f195c..800f306 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -1,5 +1,5 @@ // VulcanizeDB -// Copyright © 2021 Vulcanize +// 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 @@ -17,26 +17,88 @@ package cmd import ( - "fmt" + "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: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + 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) { - fmt.Println("serve called") + 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)