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