github.com/decred/dcrlnd@v0.7.6/lnrpc/verrpc/server.go (about)

     1  package verrpc
     2  
     3  import (
     4  	"context"
     5  	sysruntime "runtime"
     6  
     7  	"github.com/decred/dcrlnd/build"
     8  	"github.com/decred/dcrlnd/lnrpc"
     9  	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
    10  	"google.golang.org/grpc"
    11  	"gopkg.in/macaroon-bakery.v2/bakery"
    12  )
    13  
    14  const subServerName = "VersionRPC"
    15  
    16  var macPermissions = map[string][]bakery.Op{
    17  	"/verrpc.Versioner/GetVersion": {{
    18  		Entity: "info",
    19  		Action: "read",
    20  	}},
    21  }
    22  
    23  // ServerShell is a shell struct holding a reference to the actual sub-server.
    24  // It is used to register the gRPC sub-server with the root server before we
    25  // have the necessary dependencies to populate the actual sub-server.
    26  type ServerShell struct {
    27  	VersionerServer
    28  }
    29  
    30  // Server is an rpc server that supports querying for information about the
    31  // running binary.
    32  type Server struct{}
    33  
    34  // Start launches any helper goroutines required for the rpcServer to function.
    35  //
    36  // NOTE: This is part of the lnrpc.SubServer interface.
    37  func (s *Server) Start() error {
    38  	return nil
    39  }
    40  
    41  // Stop signals any active goroutines for a graceful closure.
    42  //
    43  // NOTE: This is part of the lnrpc.SubServer interface.
    44  func (s *Server) Stop() error {
    45  	return nil
    46  }
    47  
    48  // Name returns a unique string representation of the sub-server. This can be
    49  // used to identify the sub-server and also de-duplicate them.
    50  //
    51  // NOTE: This is part of the lnrpc.SubServer interface.
    52  func (s *Server) Name() string {
    53  	return subServerName
    54  }
    55  
    56  // RegisterWithRootServer will be called by the root gRPC server to direct a
    57  // sub RPC server to register itself with the main gRPC root server. Until this
    58  // is called, each sub-server won't be able to have requests routed towards it.
    59  //
    60  // NOTE: This is part of the lnrpc.GrpcHandler interface.
    61  func (r *ServerShell) RegisterWithRootServer(grpcServer *grpc.Server) error {
    62  	RegisterVersionerServer(grpcServer, r)
    63  
    64  	log.Debugf("Versioner RPC server successfully registered with root " +
    65  		"gRPC server")
    66  
    67  	return nil
    68  }
    69  
    70  // RegisterWithRestServer will be called by the root REST mux to direct a sub
    71  // RPC server to register itself with the main REST mux server. Until this is
    72  // called, each sub-server won't be able to have requests routed towards it.
    73  //
    74  // NOTE: This is part of the lnrpc.GrpcHandler interface.
    75  func (r *ServerShell) RegisterWithRestServer(ctx context.Context,
    76  	mux *runtime.ServeMux, dest string, opts []grpc.DialOption) error {
    77  
    78  	// We make sure that we register it with the main REST server to ensure
    79  	// all our methods are routed properly.
    80  	err := RegisterVersionerHandlerFromEndpoint(ctx, mux, dest, opts)
    81  	if err != nil {
    82  		log.Errorf("Could not register Versioner REST server "+
    83  			"with root REST server: %v", err)
    84  		return err
    85  	}
    86  
    87  	log.Debugf("Versioner REST server successfully registered with " +
    88  		"root REST server")
    89  	return nil
    90  }
    91  
    92  // CreateSubServer populates the subserver's dependencies using the passed
    93  // SubServerConfigDispatcher. This method should fully initialize the
    94  // sub-server instance, making it ready for action. It returns the macaroon
    95  // permissions that the sub-server wishes to pass on to the root server for all
    96  // methods routed towards it.
    97  //
    98  // NOTE: This is part of the lnrpc.GrpcHandler interface.
    99  func (r *ServerShell) CreateSubServer(_ lnrpc.SubServerConfigDispatcher) (
   100  	lnrpc.SubServer, lnrpc.MacaroonPerms, error) {
   101  
   102  	subServer := &Server{}
   103  	r.VersionerServer = subServer
   104  	return subServer, macPermissions, nil
   105  }
   106  
   107  // GetVersion returns information about the compiled binary.
   108  func (s *Server) GetVersion(_ context.Context,
   109  	_ *VersionRequest) (*Version, error) {
   110  
   111  	major, minor, patch := build.MajorMinorPatch()
   112  	return &Version{
   113  		Commit:        build.Commit,
   114  		CommitHash:    build.Commit,
   115  		Version:       build.Version(),
   116  		AppMajor:      uint32(major),
   117  		AppMinor:      uint32(minor),
   118  		AppPatch:      uint32(patch),
   119  		AppPreRelease: build.PreRelease,
   120  		BuildTags:     nil,
   121  		GoVersion:     sysruntime.Version(),
   122  	}, nil
   123  }