grpcserver

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrServerAlreadyRunning = errors.New("server is already running")
	ErrListenerNotSet       = errors.New("listener is not set")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Lis              net.Listener                // Listener
	Address          string                      // Address
	ServiceRegistrar func(grpc.ServiceRegistrar) // Server registers
	Reflection       bool                        // Enable reflection
	GrpcServer       grpcServer                  // Grpc server
}

func (*Config) Apply

func (c *Config) Apply(opts ...Option)

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithAddress

func WithAddress(a string) Option

WithAddress sets the address of the server and is used if no listener is set.

func WithGrpcServer

func WithGrpcServer(s grpcServer) Option

WithGrpcServer sets the grpc server. For testing purposes.

func WithListener

func WithListener(l net.Listener) Option

WithListener sets the listener for the server.

func WithReflection

func WithReflection() Option

func WithServiceRegistrar

func WithServiceRegistrar(r func(grpc.ServiceRegistrar)) Option

type Server

type Server struct {
	// contains filtered or unexported fields
}

func New

func New(options ...Option) *Server

New creates a new grpc server.

  • If no listener is set, the server will create a new listener on the address.
  • If no grpc server is set, the server will create a new grpc server.
  • If reflection is enabled, the server will register the reflection service.
  • If server registers are set, the server will register the services.

Example:

grpcserver.New(
	grpcserver.WithGrpcServer(grpc.NewServer(grpc.Creds(creds))),
	grpcserver.WithAddress(fmt.Sprintf(":%d", grpcPort)),
	grpcserver.WithServiceRegistrars(serviceRegistrars),
)

Example with listener:

grpcserver.New(
	grpcserver.WithGrpcServer(grpc.NewServer(grpc.Creds(creds))),
	grpcserver.WithLIstener(lis),
	grpcserver.WithServiceRegistrars(serviceRegistrars),
)

func (*Server) Running

func (s *Server) Running() bool

Running returns true if the server is running.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context) error

Serve starts the grpc server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL