service.go 3.4 KB

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