code.vegaprotocol.io/vega@v0.79.0/blockexplorer/api/grpc/server.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package grpc 17 18 import ( 19 "net" 20 21 "code.vegaprotocol.io/vega/logging" 22 pb "code.vegaprotocol.io/vega/protos/blockexplorer/api/v1" 23 24 "google.golang.org/grpc" 25 "google.golang.org/grpc/reflection" 26 ) 27 28 type Server struct { 29 Config 30 log *logging.Logger 31 blockExplorer pb.BlockExplorerServiceServer 32 grpc *grpc.Server 33 lis net.Listener 34 } 35 36 func NewServer(cfg Config, log *logging.Logger, blockExplorerServer pb.BlockExplorerServiceServer, lis net.Listener) *Server { 37 log = log.Named(namedLogger) 38 39 grpcServer := grpc.NewServer() 40 pb.RegisterBlockExplorerServiceServer(grpcServer, blockExplorerServer) 41 if cfg.Reflection { 42 reflection.Register(grpcServer) 43 } 44 45 return &Server{ 46 Config: cfg, 47 log: log, 48 blockExplorer: blockExplorerServer, 49 grpc: grpcServer, 50 lis: lis, 51 } 52 } 53 54 func (g *Server) Serve() error { 55 g.log.Info("Starting gRPC server", logging.String("address", g.lis.Addr().String())) 56 return g.grpc.Serve(g.lis) 57 } 58 59 func (g *Server) Stop() { 60 if g.grpc != nil { 61 g.log.Info("Stopping gRPC server", logging.String("address", g.lis.Addr().String())) 62 g.grpc.Stop() 63 } 64 }