github.com/JFJun/bsc@v1.0.0/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/JFJun/bsc/internal/ethapi"
    25  	"github.com/JFJun/bsc/log"
    26  	"github.com/JFJun/bsc/node"
    27  	"github.com/JFJun/bsc/p2p"
    28  	"github.com/JFJun/bsc/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 on.
    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  	// create handler stack and wrap the graphql handler
    73  	handler := node.NewHTTPHandlerStack(s.handler, s.cors, s.vhosts)
    74  	// make sure timeout values are meaningful
    75  	node.CheckTimeouts(&s.timeouts)
    76  	// create http server
    77  	httpSrv := &http.Server{
    78  		Handler:      handler,
    79  		ReadTimeout:  s.timeouts.ReadTimeout,
    80  		WriteTimeout: s.timeouts.WriteTimeout,
    81  		IdleTimeout:  s.timeouts.IdleTimeout,
    82  	}
    83  	go httpSrv.Serve(s.listener)
    84  	log.Info("GraphQL endpoint opened", "url", fmt.Sprintf("http://%s", s.endpoint))
    85  	return nil
    86  }
    87  
    88  // newHandler returns a new `http.Handler` that will answer GraphQL queries.
    89  // It additionally exports an interactive query browser on the / endpoint.
    90  func newHandler(backend ethapi.Backend) (http.Handler, error) {
    91  	q := Resolver{backend}
    92  
    93  	s, err := graphql.ParseSchema(schema, &q)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	h := &relay.Handler{Schema: s}
    98  
    99  	mux := http.NewServeMux()
   100  	mux.Handle("/", GraphiQL{})
   101  	mux.Handle("/graphql", h)
   102  	mux.Handle("/graphql/", h)
   103  	return mux, nil
   104  }
   105  
   106  // Stop terminates all goroutines belonging to the service, blocking until they
   107  // are all terminated.
   108  func (s *Service) Stop() error {
   109  	if s.listener != nil {
   110  		s.listener.Close()
   111  		s.listener = nil
   112  		log.Info("GraphQL endpoint closed", "url", fmt.Sprintf("http://%s", s.endpoint))
   113  	}
   114  	return nil
   115  }