2020-02-09 10:31:52 +00:00
package commands
import (
2020-04-04 07:18:10 +00:00
"context"
2020-02-09 10:31:52 +00:00
"fmt"
"os"
2020-04-04 07:18:10 +00:00
"os/signal"
"syscall"
2020-02-09 10:31:52 +00:00
"github.com/ledgerwatch/turbo-geth/cmd/restapi/rest"
2020-04-04 07:18:10 +00:00
"github.com/ledgerwatch/turbo-geth/log"
"github.com/spf13/cobra"
2020-02-09 10:31:52 +00:00
)
var (
2020-07-27 12:15:48 +00:00
rpcAddr string
chaindata string
addr string
2020-02-09 10:31:52 +00:00
)
func init ( ) {
2020-07-27 12:15:48 +00:00
rootCmd . Flags ( ) . StringVar ( & rpcAddr , "private.rpc.addr" , "127.0.0.1:9090" , "binary RPC network address, for example: 127.0.0.1:9090, empty string means not to start the listener. do not expose to public network. serves remote database interface" )
rootCmd . Flags ( ) . StringVar ( & addr , "http.addr" , "127.0.0.1:8080" , "REST server listening host" )
2020-06-28 06:10:27 +00:00
rootCmd . Flags ( ) . StringVar ( & chaindata , "chaindata" , "" , "path to the database" )
2020-02-09 10:31:52 +00:00
}
var rootCmd = & cobra . Command {
Use : "restapi" ,
Short : "restapi exposes read-only blockchain APIs through REST (requires running turbo-geth node)" ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2020-07-27 12:15:48 +00:00
return rest . ServeREST ( cmd . Context ( ) , addr , rpcAddr , chaindata )
2020-02-09 10:31:52 +00:00
} ,
}
func Execute ( ) {
2020-04-04 07:18:10 +00:00
if err := rootCmd . ExecuteContext ( rootContext ( ) ) ; err != nil {
2020-02-09 10:31:52 +00:00
fmt . Println ( err )
os . Exit ( 1 )
}
}
2020-04-04 07:18:10 +00:00
func rootContext ( ) context . Context {
ctx , cancel := context . WithCancel ( context . Background ( ) )
go func ( ) {
ch := make ( chan os . Signal , 1 )
signal . Notify ( ch , os . Interrupt , syscall . SIGTERM )
defer signal . Stop ( ch )
select {
case <- ch :
log . Info ( "Got interrupt, shutting down..." )
case <- ctx . Done ( ) :
}
cancel ( )
} ( )
return ctx
}