grpcserver

package
v0.0.0-...-93b1e75 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package grpcserver provides API to grpc server with graceful shutdown.

Typical usage:

grpcserver.New(
    httpserver.WithName("my-server"),
    httpserver.WithAddress("127.0.0.1:8080"),
    httpserver.WithRegisterFn(func(s *grpc.Server) {
        // Register your service here
    }),
).Run(ctx)

Index

Constants

View Source
const (
	// DefAddress is the default server's address that will be listened to.
	DefAddress = "127.0.0.1:50051"
	// DefName is the default server's name.
	DefName = "grpc-server"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config structure with server configuration. Shouldn't be created manually.

type ConfigService

type ConfigService interface {
	GetByKey(string, interface{}) error
	IsNoSuchKeyError(error) bool
}

ConfigService interface used to obtain configuration from somewhere into some specific structure.

type Option

type Option func(*Config) error

Option function that is fed to New and MustNew. Obtain them using 'With' functions down below.

func WithAddress

func WithAddress(address string) Option

WithAddress option applies provided address that will be listened to. Default: "127.0.0.1:50051".

func WithConfig

func WithConfig(service ConfigService, key string) Option

WithConfig option retrieves configuration from provided configuration service.

Example JSON configuration with all possible fields (if some are not present, defaults will be used):

{
    "name": "server-name",
    "address": "127.0.0.1:50051"
}

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger option applies provided logger. Default: standard logger with name equal to server's one.

func WithName

func WithName(name string) Option

WithName option applies provided server name. Default: "grpc-server".

func WithRegisterFn

func WithRegisterFn(registerFn RegisterFn) Option

WithRegisterFn option applies provided register function.

type RegisterFn

type RegisterFn func(*grpc.Server)

RegisterFn callback function that is called before listening in order to register any user-defined grpc service.

type Server

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

Server structure that provides grpc server functionality. Don't create manually, use the functions down below instead.

func MustNew

func MustNew(opts ...Option) *Server

MustNew function creates new server with provided options and panics on any error.

func New

func New(opts ...Option) (*Server, error)

New function creates new server with provided options.

func (*Server) Address

func (server *Server) Address() string

Address method gets server's address that it listens to.

func (*Server) Name

func (server *Server) Name() string

Name method gets server's name.

func (*Server) Run

func (server *Server) Run(ctx context.Context)

Run method runs server listen loop. It is blocking so you probably want to run it in a separate goroutine. If you pass cancellable context here, you will be able to gracefully shutdown server that waits for all requests to complete.

Jump to

Keyboard shortcuts

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