RPC server commands
This commit is contained in:
parent
7e669ec6cf
commit
7cb1f2bcd3
@ -1,5 +1,5 @@
|
|||||||
// VulcanizeDB
|
// VulcanizeDB
|
||||||
// Copyright © 2021 Vulcanize
|
// Copyright © 2022 Vulcanize
|
||||||
|
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// 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
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -18,9 +18,10 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
82
cmd/serve.go
82
cmd/serve.go
@ -1,5 +1,5 @@
|
|||||||
// VulcanizeDB
|
// VulcanizeDB
|
||||||
// Copyright © 2021 Vulcanize
|
// Copyright © 2022 Vulcanize
|
||||||
|
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// 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
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -17,26 +17,88 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"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
|
// serveCmd represents the serve command
|
||||||
var serveCmd = &cobra.Command{
|
var serveCmd = &cobra.Command{
|
||||||
Use: "serve",
|
Use: "serve",
|
||||||
Short: "A brief description of your command",
|
Short: "RPC Server for LevelDB eth.Database",
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
Long: `This service exposes a remote RPC server interface for a local levelDB backed ethdb.Database`,
|
||||||
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.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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() {
|
func init() {
|
||||||
rootCmd.AddCommand(serveCmd)
|
rootCmd.AddCommand(serveCmd)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user