server

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2020 License: MIT Imports: 23 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultHeaderMatcher

func DefaultHeaderMatcher() runtime.ServeMuxOption

DefaultHeaderMatcher return a ServerMuxOption that forward header keys request-id, api-key to GRPC Context.

func HeaderMatcher

func HeaderMatcher(keys []string) runtime.ServeMuxOption

HeaderMatcher return a serveMuxOption for matcher header for passing a set of non IANA headers to GRPC context without a need to prefix them with Grpc-Metadata.

func ListenAndServe

func ListenAndServe(services ...Service) error

ListenAndServe create a new server base on environment configuration and serve the services with background context. See server.ListenAndServe for detail document.

func ListenAndServeContext

func ListenAndServeContext(ctx context.Context, services ...Service) error

ListenAndServeContext create a new server base on environment configuration and serve the services with the given context. See server.ListenAndServeContext for detail document.

Types

type Authenticator

type Authenticator interface {
	Authenticate(ctx context.Context) (context.Context, error)
}

Authenticator defines the interface to perform the actual authentication of the request. Implementations should fetch the required data from the context.Context object. GRPC specific data like `metadata` and `peer` is available on the context. Should return a new `context.Context` that is a child of `ctx` or `codes.Unauthenticated` when auth is lacking or `codes.PermissionDenied` when lacking permissions.

type Config

type Config struct {
	Name        string `envconfig:"NAME" default:"micro"`
	Address     string `envconfig:"ADDRESS" default:":8000"`
	TLSCertFile string `envconfig:"TLS_CERT_FILE"`
	TLSKeyFile  string `envconfig:"TLS_KEY_FILE"`

	// Paths
	LivenessPath  string `envconfig:"LIVENESS_PATH" default:"/internal/liveness"`
	ReadinessPath string `envconfig:"READINESS_PATH" default:"/internal/readiness"`
	MetricsPath   string `envconfig:"METRICS_PATH" default:"/internal/metrics"`

	// HTTP
	ReadTimeout  time.Duration `envconfig:"READ_TIMEOUT" default:"30s"`
	WriteTimeout time.Duration `envconfig:"WRITE_TIMEOUT" default:"30s"`

	JWTSecret     string `envconfig:"JWT_SECRET"`
	ContextLogger bool   `envconfig:"CONTEXT_LOGGER" default:"true"`
}

Config is a common configuration of a default server. Mostly used by lazy guys via NewFromEnv().

type EndpointService

type EndpointService interface {
	RegisterWithEndpoint(ctx context.Context, mux *runtime.ServeMux, addr string, opts []grpc.DialOption)
}

EndpointService implement an endpoint registration interface for service to attach their endpoint to GRPC gateway

type Option

type Option func(*Server)

Option is a configuration option.

func AddressFromEnv added in v0.0.2

func AddressFromEnv() Option

AddressFromEnv is an option to get address from environment configuration. It looks for PORT and then ADDRESS variables. This option is mostly used for cloud environment like Heroku where the port is randomly set.

func HealthChecks

func HealthChecks(checks ...health.CheckFunc) Option

HealthChecks is an option allow set health check function.

func JWTAuth

func JWTAuth(secret string) Option

JWTAuth is an option allow to add jwt authenticator to the server.

func Logger

func Logger(logger log.Logger) Option

Logger is an option allow add a custom logger into the server.

func MetricsPaths

func MetricsPaths(ready, live, metrics string) Option

MetricsPaths is an option allow override readiness, liveness and metrics path.

func Options

func Options(serverOpts ...grpc.ServerOption) Option

Options is an option allow add additional grpc.ServerOption.

func ServeMuxOptions

func ServeMuxOptions(muxOpts ...runtime.ServeMuxOption) Option

ServeMuxOptions is an option allow add additional ServeMuxOption.

func StreamInterceptors

func StreamInterceptors(interceptors ...grpc.StreamServerInterceptor) Option

StreamInterceptors is an option allows add additional stream interceptors to the server.

func TLS

func TLS(key, cert string) Option

TLS is an option allow add TLS for transport security to the server.

func Timeout

func Timeout(read, write time.Duration) Option

Timeout is an option to override default read/write timeout.

func UnaryInterceptors

func UnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) Option

UnaryInterceptors is an option allows add additional unary interceptors to the server.

type Server

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

Server holds the configuration options for the server instance.

func New

func New(addr string, ops ...Option) *Server

New return new server

func NewFromEnv

func NewFromEnv() *Server

NewFromEnv load configurations from environment and create a new server. Additional options can be added to the sever via Server.WithOptions(...). See Config for environment names.

func (*Server) ListenAndServe

func (server *Server) ListenAndServe(services ...Service) error

ListenAndServe call ListenAndServeContext with background context.

func (*Server) ListenAndServeContext

func (server *Server) ListenAndServeContext(ctx context.Context, services ...Service) error

ListenAndServeContext opens a tcp listener used by a grpc.Server and a HTTP server, and registers each Service with the grpc.Server. If the Service implements EndpointService its endpoints will be registered to the HTTP Server running on the same port. The server starts with default metrics and health endpoints. If the context is canceled or times out, the GRPC server will attempt a graceful shutdown.

func (*Server) WithOptions

func (server *Server) WithOptions(opts ...Option)

WithOptions allows add more options to the server after created.

type Service

type Service interface {
	Register(srv *grpc.Server)
}

Service implements a registration interface for services to attach themselves to the grpc.Server.

Jump to

Keyboard shortcuts

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