service.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. package graphql
  17. import (
  18. "fmt"
  19. "net"
  20. "net/http"
  21. "github.com/ethereum/go-ethereum/internal/ethapi"
  22. "github.com/ethereum/go-ethereum/log"
  23. "github.com/ethereum/go-ethereum/node"
  24. "github.com/ethereum/go-ethereum/p2p"
  25. "github.com/ethereum/go-ethereum/rpc"
  26. "github.com/graph-gophers/graphql-go"
  27. "github.com/graph-gophers/graphql-go/relay"
  28. )
  29. // Service encapsulates a GraphQL service.
  30. type Service struct {
  31. endpoint string // The host:port endpoint for this service.
  32. cors []string // Allowed CORS domains
  33. vhosts []string // Recognised vhosts
  34. timeouts rpc.HTTPTimeouts // Timeout settings for HTTP requests.
  35. backend ethapi.Backend // The backend that queries will operate on.
  36. handler http.Handler // The `http.Handler` used to answer queries.
  37. listener net.Listener // The listening socket.
  38. }
  39. // New constructs a new GraphQL service instance.
  40. func New(backend ethapi.Backend, endpoint string, cors, vhosts []string, timeouts rpc.HTTPTimeouts) (*Service, error) {
  41. return &Service{
  42. endpoint: endpoint,
  43. cors: cors,
  44. vhosts: vhosts,
  45. timeouts: timeouts,
  46. backend: backend,
  47. }, nil
  48. }
  49. // Protocols returns the list of protocols exported by this service.
  50. func (s *Service) Protocols() []p2p.Protocol { return nil }
  51. // APIs returns the list of APIs exported by this service.
  52. func (s *Service) APIs() []rpc.API { return nil }
  53. // Start is called after all services have been constructed and the networking
  54. // layer was also initialized to spawn any goroutines required by the service.
  55. func (s *Service) Start(server *p2p.Server) error {
  56. var err error
  57. s.handler, err = newHandler(s.backend)
  58. if err != nil {
  59. return err
  60. }
  61. if s.listener, err = net.Listen("tcp", s.endpoint); err != nil {
  62. return err
  63. }
  64. // create handler stack and wrap the graphql handler
  65. handler := node.NewHTTPHandlerStack(s.handler, s.cors, s.vhosts)
  66. // make sure timeout values are meaningful
  67. node.CheckTimeouts(&s.timeouts)
  68. // create http server
  69. httpSrv := &http.Server{
  70. Handler: handler,
  71. ReadTimeout: s.timeouts.ReadTimeout,
  72. WriteTimeout: s.timeouts.WriteTimeout,
  73. IdleTimeout: s.timeouts.IdleTimeout,
  74. }
  75. go httpSrv.Serve(s.listener)
  76. log.Info("GraphQL endpoint opened", "url", fmt.Sprintf("http://%s", s.endpoint))
  77. return nil
  78. }
  79. // newHandler returns a new `http.Handler` that will answer GraphQL queries.
  80. // It additionally exports an interactive query browser on the / endpoint.
  81. func newHandler(backend ethapi.Backend) (http.Handler, error) {
  82. q := Resolver{backend}
  83. s, err := graphql.ParseSchema(schema, &q)
  84. if err != nil {
  85. return nil, err
  86. }
  87. h := &relay.Handler{Schema: s}
  88. mux := http.NewServeMux()
  89. mux.Handle("/", GraphiQL{})
  90. mux.Handle("/graphql", h)
  91. mux.Handle("/graphql/", h)
  92. return mux, nil
  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. }