Skip to content

Commit e3ec77f

Browse files
committed
cmd, graphql, node: graphql flag polishes, les integration
1 parent 2b54666 commit e3ec77f

9 files changed

Lines changed: 203 additions & 164 deletions

File tree

‎cmd/geth/config.go‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/ethereum/go-ethereum/cmd/utils"
3131
"github.com/ethereum/go-ethereum/dashboard"
3232
"github.com/ethereum/go-ethereum/eth"
33-
"github.com/ethereum/go-ethereum/graphql"
3433
"github.com/ethereum/go-ethereum/node"
3534
"github.com/ethereum/go-ethereum/params"
3635
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
@@ -177,14 +176,10 @@ func makeFullNode(ctx *cli.Context) *node.Node {
177176
}
178177
utils.RegisterShhService(stack, &cfg.Shh)
179178
}
180-
181-
// Configure GraphQL if required
179+
// Configure GraphQL if requested
182180
if ctx.GlobalIsSet(utils.GraphQLEnabledFlag.Name) {
183-
if err := graphql.RegisterGraphQLService(stack, cfg.Node.GraphQLEndpoint(), cfg.Node.GraphQLCors, cfg.Node.GraphQLVirtualHosts, cfg.Node.HTTPTimeouts); err != nil {
184-
utils.Fatalf("Failed to register the Ethereum service: %v", err)
185-
}
181+
utils.RegisterGraphQLService(stack, cfg.Node.GraphQLEndpoint(), cfg.Node.GraphQLCors, cfg.Node.GraphQLVirtualHosts, cfg.Node.HTTPTimeouts)
186182
}
187-
188183
// Add the Ethereum Stats daemon if requested.
189184
if cfg.Ethstats.URL != "" {
190185
utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)

‎cmd/geth/usage.go‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,25 @@ var AppHelpFlagGroups = []flagGroup{
156156
{
157157
Name: "API AND CONSOLE",
158158
Flags: []cli.Flag{
159+
utils.IPCDisabledFlag,
160+
utils.IPCPathFlag,
159161
utils.RPCEnabledFlag,
160162
utils.RPCListenAddrFlag,
161163
utils.RPCPortFlag,
162164
utils.RPCApiFlag,
163165
utils.RPCGlobalGasCap,
166+
utils.RPCCORSDomainFlag,
167+
utils.RPCVirtualHostsFlag,
164168
utils.WSEnabledFlag,
165169
utils.WSListenAddrFlag,
166170
utils.WSPortFlag,
167171
utils.WSApiFlag,
168172
utils.WSAllowedOriginsFlag,
169-
utils.IPCDisabledFlag,
170-
utils.IPCPathFlag,
171-
utils.RPCCORSDomainFlag,
172-
utils.RPCVirtualHostsFlag,
173+
utils.GraphQLEnabledFlag,
174+
utils.GraphQLListenAddrFlag,
175+
utils.GraphQLPortFlag,
176+
utils.GraphQLCORSDomainFlag,
177+
utils.GraphQLVirtualHostsFlag,
173178
utils.JSpathFlag,
174179
utils.ExecFlag,
175180
utils.PreloadJSFlag,

‎cmd/utils/flags.go‎

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package utils
2020
import (
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+
15551577
func SetupMetrics(ctx *cli.Context) {
15561578
if metrics.Enabled {
15571579
log.Info("Enabling metrics collection")

‎graphql/graphiql.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ var graphiql = []byte(`
6060
<!DOCTYPE html>
6161
<html>
6262
<head>
63-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.css"/>
64-
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.min.js"></script>
65-
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min.js"></script>
66-
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min.js"></script>
67-
<script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.min.js"></script>
63+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.13.0/graphiql.css"/>
64+
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/3.0.0/fetch.min.js"></script>
65+
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.5/umd/react.production.min.js"></script>
66+
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.5/umd/react-dom.production.min.js"></script>
67+
<script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.13.0/graphiql.min.js"></script>
6868
</head>
6969
<body style="width: 100%; height: 100%; margin: 0; overflow: hidden;">
7070
<div id="graphiql" style="height: 100vh;">Loading...</div>

0 commit comments

Comments
 (0)