@@ -20,6 +20,7 @@ package utils
2020import (
2121 "crypto/ecdsa"
2222 "encoding/json"
23+ "errors"
2324 "fmt"
2425 "io/ioutil"
2526 "math/big"
@@ -45,6 +46,7 @@ import (
4546 "github.com/ethereum/go-ethereum/eth/gasprice"
4647 "github.com/ethereum/go-ethereum/ethdb"
4748 "github.com/ethereum/go-ethereum/ethstats"
49+ "github.com/ethereum/go-ethereum/graphql"
4850 "github.com/ethereum/go-ethereum/les"
4951 "github.com/ethereum/go-ethereum/log"
5052 "github.com/ethereum/go-ethereum/metrics"
@@ -57,6 +59,7 @@ import (
5759 "github.com/ethereum/go-ethereum/p2p/nat"
5860 "github.com/ethereum/go-ethereum/p2p/netutil"
5961 "github.com/ethereum/go-ethereum/params"
62+ "github.com/ethereum/go-ethereum/rpc"
6063 whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
6164 pcsclite "github.com/gballet/go-libpcsclite"
6265 cli "gopkg.in/urfave/cli.v1"
@@ -474,6 +477,14 @@ var (
474477 Usage : "Disables db compaction after import" ,
475478 }
476479 // RPC settings
480+ IPCDisabledFlag = cli.BoolFlag {
481+ Name : "ipcdisable" ,
482+ Usage : "Disable the IPC-RPC server" ,
483+ }
484+ IPCPathFlag = DirectoryFlag {
485+ Name : "ipcpath" ,
486+ Usage : "Filename for IPC socket/pipe within the datadir (explicit paths escape it)" ,
487+ }
477488 RPCEnabledFlag = cli.BoolFlag {
478489 Name : "rpc" ,
479490 Usage : "Enable the HTTP-RPC server" ,
@@ -488,30 +499,6 @@ var (
488499 Usage : "HTTP-RPC server listening port" ,
489500 Value : node .DefaultHTTPPort ,
490501 }
491- GraphQLEnabledFlag = cli.BoolFlag {
492- Name : "graphql" ,
493- Usage : "Enable the GraphQL server" ,
494- }
495- GraphQLListenAddrFlag = cli.StringFlag {
496- Name : "graphql.addr" ,
497- Usage : "GraphQL server listening interface" ,
498- Value : node .DefaultGraphQLHost ,
499- }
500- GraphQLPortFlag = cli.IntFlag {
501- Name : "graphql.port" ,
502- Usage : "GraphQL server listening port" ,
503- Value : node .DefaultGraphQLPort ,
504- }
505- GraphQLCORSDomainFlag = cli.StringFlag {
506- Name : "graphql.rpccorsdomain" ,
507- Usage : "Comma separated list of domains from which to accept cross origin requests (browser enforced)" ,
508- Value : "" ,
509- }
510- GraphQLVirtualHostsFlag = cli.StringFlag {
511- Name : "graphql.rpcvhosts" ,
512- Usage : "Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard." ,
513- Value : strings .Join (node .DefaultConfig .HTTPVirtualHosts , "," ),
514- }
515502 RPCCORSDomainFlag = cli.StringFlag {
516503 Name : "rpccorsdomain" ,
517504 Usage : "Comma separated list of domains from which to accept cross origin requests (browser enforced)" ,
@@ -527,14 +514,6 @@ var (
527514 Usage : "API's offered over the HTTP-RPC interface" ,
528515 Value : "" ,
529516 }
530- IPCDisabledFlag = cli.BoolFlag {
531- Name : "ipcdisable" ,
532- Usage : "Disable the IPC-RPC server" ,
533- }
534- IPCPathFlag = DirectoryFlag {
535- Name : "ipcpath" ,
536- Usage : "Filename for IPC socket/pipe within the datadir (explicit paths escape it)" ,
537- }
538517 WSEnabledFlag = cli.BoolFlag {
539518 Name : "ws" ,
540519 Usage : "Enable the WS-RPC server" ,
@@ -559,6 +538,30 @@ var (
559538 Usage : "Origins from which to accept websockets requests" ,
560539 Value : "" ,
561540 }
541+ GraphQLEnabledFlag = cli.BoolFlag {
542+ Name : "graphql" ,
543+ Usage : "Enable the GraphQL server" ,
544+ }
545+ GraphQLListenAddrFlag = cli.StringFlag {
546+ Name : "graphql.addr" ,
547+ Usage : "GraphQL server listening interface" ,
548+ Value : node .DefaultGraphQLHost ,
549+ }
550+ GraphQLPortFlag = cli.IntFlag {
551+ Name : "graphql.port" ,
552+ Usage : "GraphQL server listening port" ,
553+ Value : node .DefaultGraphQLPort ,
554+ }
555+ GraphQLCORSDomainFlag = cli.StringFlag {
556+ Name : "graphql.corsdomain" ,
557+ Usage : "Comma separated list of domains from which to accept cross origin requests (browser enforced)" ,
558+ Value : "" ,
559+ }
560+ GraphQLVirtualHostsFlag = cli.StringFlag {
561+ Name : "graphql.vhosts" ,
562+ Usage : "Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard." ,
563+ Value : strings .Join (node .DefaultConfig .GraphQLVirtualHosts , "," ),
564+ }
562565 ExecFlag = cli.StringFlag {
563566 Name : "exec" ,
564567 Usage : "Execute JavaScript statement" ,
@@ -874,7 +877,6 @@ func setHTTP(ctx *cli.Context, cfg *node.Config) {
874877 cfg .HTTPHost = ctx .GlobalString (RPCListenAddrFlag .Name )
875878 }
876879 }
877-
878880 if ctx .GlobalIsSet (RPCPortFlag .Name ) {
879881 cfg .HTTPPort = ctx .GlobalInt (RPCPortFlag .Name )
880882 }
@@ -916,7 +918,6 @@ func setWS(ctx *cli.Context, cfg *node.Config) {
916918 cfg .WSHost = ctx .GlobalString (WSListenAddrFlag .Name )
917919 }
918920 }
919-
920921 if ctx .GlobalIsSet (WSPortFlag .Name ) {
921922 cfg .WSPort = ctx .GlobalInt (WSPortFlag .Name )
922923 }
@@ -1546,12 +1547,33 @@ func RegisterEthStatsService(stack *node.Node, url string) {
15461547 var lesServ * les.LightEthereum
15471548 ctx .Service (& lesServ )
15481549
1550+ // Let ethstats use whichever is not nil
15491551 return ethstats .New (url , ethServ , lesServ )
15501552 }); err != nil {
15511553 Fatalf ("Failed to register the Ethereum Stats service: %v" , err )
15521554 }
15531555}
15541556
1557+ // RegisterGraphQLService is a utility function to construct a new service and register it against a node.
1558+ func RegisterGraphQLService (stack * node.Node , endpoint string , cors , vhosts []string , timeouts rpc.HTTPTimeouts ) {
1559+ if err := stack .Register (func (ctx * node.ServiceContext ) (node.Service , error ) {
1560+ // Try to construct the GraphQL service backed by a full node
1561+ var ethServ * eth.Ethereum
1562+ if err := ctx .Service (& ethServ ); err == nil {
1563+ return graphql .New (ethServ .APIBackend , endpoint , cors , vhosts , timeouts )
1564+ }
1565+ // Try to construct the GraphQL service backed by a light node
1566+ var lesServ * les.LightEthereum
1567+ if err := ctx .Service (& lesServ ); err == nil {
1568+ return graphql .New (lesServ .ApiBackend , endpoint , cors , vhosts , timeouts )
1569+ }
1570+ // Well, this should not have happened, bail out
1571+ return nil , errors .New ("no Ethereum service" )
1572+ }); err != nil {
1573+ Fatalf ("Failed to register the GraphQL service: %v" , err )
1574+ }
1575+ }
1576+
15551577func SetupMetrics (ctx * cli.Context ) {
15561578 if metrics .Enabled {
15571579 log .Info ("Enabling metrics collection" )
0 commit comments