github.com/mprishchepo/go-ethereum@v1.9.7-0.20191031044858-21506be82b68/graphql/service.go (about) 1 // Copyright 2019 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package graphql 18 19 import ( 20 "fmt" 21 "net" 22 "net/http" 23 24 "github.com/Fantom-foundation/go-ethereum/internal/ethapi" 25 "github.com/Fantom-foundation/go-ethereum/log" 26 "github.com/Fantom-foundation/go-ethereum/p2p" 27 "github.com/Fantom-foundation/go-ethereum/rpc" 28 "github.com/graph-gophers/graphql-go" 29 "github.com/graph-gophers/graphql-go/relay" 30 ) 31 32 // Service encapsulates a GraphQL service. 33 type Service struct { 34 endpoint string // The host:port endpoint for this service. 35 cors []string // Allowed CORS domains 36 vhosts []string // Recognised vhosts 37 timeouts rpc.HTTPTimeouts // Timeout settings for HTTP requests. 38 backend ethapi.Backend // The backend that queries will operate onn. 39 handler http.Handler // The `http.Handler` used to answer queries. 40 listener net.Listener // The listening socket. 41 } 42 43 // New constructs a new GraphQL service instance. 44 func New(backend ethapi.Backend, endpoint string, cors, vhosts []string, timeouts rpc.HTTPTimeouts) (*Service, error) { 45 return &Service{ 46 endpoint: endpoint, 47 cors: cors, 48 vhosts: vhosts, 49 timeouts: timeouts, 50 backend: backend, 51 }, nil 52 } 53 54 // Protocols returns the list of protocols exported by this service. 55 func (s *Service) Protocols() []p2p.Protocol { return nil } 56 57 // APIs returns the list of APIs exported by this service. 58 func (s *Service) APIs() []rpc.API { return nil } 59 60 // Start is called after all services have been constructed and the networking 61 // layer was also initialized to spawn any goroutines required by the service. 62 func (s *Service) Start(server *p2p.Server) error { 63 var err error 64 s.handler, err = newHandler(s.backend) 65 if err != nil { 66 return err 67 } 68 if s.listener, err = net.Listen("tcp", s.endpoint); err != nil { 69 return err 70 } 71 go rpc.NewHTTPServer(s.cors, s.vhosts, s.timeouts, s.handler).Serve(s.listener) 72 log.Info("GraphQL endpoint opened", "url", fmt.Sprintf("http://%s", s.endpoint)) 73 return nil 74 } 75 76 // newHandler returns a new `http.Handler` that will answer GraphQL queries. 77 // It additionally exports an interactive query browser on the / endpoint. 78 func newHandler(backend ethapi.Backend) (http.Handler, error) { 79 q := Resolver{backend} 80 81 s, err := graphql.ParseSchema(schema, &q) 82 if err != nil { 83 return nil, err 84 } 85 h := &relay.Handler{Schema: s} 86 87 mux := http.NewServeMux() 88 mux.Handle("/", GraphiQL{}) 89 mux.Handle("/graphql", h) 90 mux.Handle("/graphql/", h) 91 return mux, nil 92 } 93 94 // Stop terminates all goroutines belonging to the service, blocking until they 95 // are all terminated. 96 func (s *Service) Stop() error { 97 if s.listener != nil { 98 s.listener.Close() 99 s.listener = nil 100 log.Info("GraphQL endpoint closed", "url", fmt.Sprintf("http://%s", s.endpoint)) 101 } 102 return nil 103 }