servers

package module
v0.15.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2024 License: MIT Imports: 39 Imported by: 3

README

servers

TODO

  • Refactor grpc entities
    • Observer

Documentation

Overview

Package servers provides server implementations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMetricsStart is returned when an error occurs the Metrics server.
	ErrMetricsStart = errors.New("start Metrics server")
	// ErrCollectorAppended is returned when attempting to append a collector that already exists.
	ErrCollectorAppended = errors.New("collector already appended")
)
View Source
var ErrGRPCStart = errors.New("start grpc server")

ErrGRPCStart is returned when an error occurs the GRPC server start.

View Source
var ErrListenerFailedStart = errors.New("failed to start listener")

ErrListenerFailedStart is returned when an error occurs starting listener for the REST server.

View Source
var ErrRESTStart = errors.New("start REST server")

ErrRESTStart is returned when an error occurs the REST server.

Functions

func Error added in v0.12.0

func Error(c codes.Code, msg string, details ...map[string]string) error

Error creates a new error with the given code, message and details if this is provided. If more than one details are provided, they are merged into a single map.

func NewRestAPIDocsHandlers added in v0.2.0

func NewRestAPIDocsHandlers(serviceName, basePath, swaggerPath string, swaggerJSON []byte) map[string]http.Handler

NewRestAPIDocsHandlers creates a handler for an endpoint to response on /docs path to show the api documentation. It returns a map of handlers for the pattern and the handler.

func NewRestRootHandler

func NewRestRootHandler(serviceName string, links ...any) http.Handler

NewRestRootHandler creates a handler for an endpoint to response on / path.

func NewRestVersionHandler

func NewRestVersionHandler() http.Handler

NewRestVersionHandler creates a handler for an endpoint to response on /version path to show the version of the api.

func WrapError added in v0.13.0

func WrapError(c codes.Code, err error, msg string, details ...map[string]string) error

WrapError wraps an error with the given code, message and details if this is provided. If more than one details are provided, they are merged into a single map.

Types

type Config

type Config struct {
	Name string `envconfig:"NAME" required:"true"`
	Host string `envconfig:"HOST"`
	Port uint   `envconfig:"PORT" required:"true"`
	// contains filtered or unexported fields
}

Config contains configuration options for a Server.

type GRPC

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

GRPC is a listening grpc server instance.

func NewGRPC

func NewGRPC(config Config, opts ...Option) *GRPC

NewGRPC initiates a new wrapped grpc server.

func (*GRPC) Start

func (srv *GRPC) Start() error

Start starts serving the GRPC server.

func (*GRPC) Stop

func (srv *GRPC) Stop()

Stop gracefully shuts down the GRPC server.

type GRPCObserver

type GRPCObserver interface {
	UnaryServerInterceptor() func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error)
	StreamServerInterceptor() func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error
}

GRPCObserver interface implemented by anything wants to append observer in the server thro UnaryServerInterceptor and StreamServerInterceptor.

type GRPCRateLimiter added in v0.15.0

type GRPCRateLimiter interface {
	UnaryServerInterceptor() func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error)
}

GRPCRateLimiter interface implemented by anything wants to append rate limiter in the server thro UnaryServerInterceptor.

type GRPCRegisterService

type GRPCRegisterService interface {
	RegisterService(s grpc.ServiceRegistrar)
}

GRPCRegisterService is an interface for a grpc service that provides registration.

type GRPCRegisterServiceFunc

type GRPCRegisterServiceFunc func(s grpc.ServiceRegistrar)

GRPCRegisterServiceFunc is the function to register service to a grpc.

type GRPCRest added in v0.2.0

type GRPCRest struct {
	*REST
	// contains filtered or unexported fields
}

GRPCRest is a listening grpc rest server instance.

func NewGRPCRest added in v0.2.0

func NewGRPCRest(config Config, opts ...Option) (*GRPCRest, error)

NewGRPCRest initiates a new wrapped grpc rest server.

type GRPCRestRegisterService added in v0.2.0

type GRPCRestRegisterService interface {
	RegisterServiceHandler(mux *runtime.ServeMux) error
}

GRPCRestRegisterService is an interface for a grpc rest service that provides registration.

type HealthCheck added in v0.14.0

type HealthCheck struct {
	*REST
	// contains filtered or unexported fields
}

HealthCheck is a listening HTTP server instance with the endpoints "/" and "/health" mainly use for livenessProbe and readinessProbe on Kubernetes cluster.

func NewHealthCheck

func NewHealthCheck(cfg Config, opts ...Option) *HealthCheck

NewHealthCheck is a listening HTTP server instance with the endpoints "/" and "/health" mainly use for livenessProbe and readinessProbe on Kubernetes cluster.

type Information

type Information struct {
	Version      string            `json:"version,omitempty"`
	Revision     string            `json:"revision,omitempty"`
	Branch       string            `json:"branch,omitempty"`
	BuildUser    string            `json:"build_user,omitempty"`
	BuildDate    string            `json:"build_date,omitempty"`
	GoVersion    string            `json:"go_version,omitempty"`
	Dependencies map[string]string `json:"dependencies,omitempty"`
}

Information holds app version info.

func Info

func Info() Information

Info returns app version info.

func (Information) String

func (i Information) String() string

String return version information as string.

func (Information) Values

func (i Information) Values() map[string]string

Values return version information as string map.

type Metrics

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

Metrics is a listening HTTP collector server instance.

func NewMetrics

func NewMetrics(config Config, opts ...Option) *Metrics

NewMetrics initiates a new wrapped prom server collector.

func (*Metrics) Start

func (srv *Metrics) Start() error

Start starts serving the Metrics server.

func (*Metrics) Stop

func (srv *Metrics) Stop()

Stop gracefully shuts down the Metrics server.

type Option

type Option func(srv any)

Option sets up a server.

func WithAddrAssigned

func WithAddrAssigned() Option

WithAddrAssigned sets service to ask for listener assigned address. Mainly used when the port to the listener is assigned dynamically. Apply to all server instances.

func WithChainStreamInterceptor

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

WithChainStreamInterceptor sets the server interceptors for stream. Apply to GRPC server instances.

func WithChainUnaryInterceptor

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

WithChainUnaryInterceptor sets the server interceptors for unary. Apply to GRPC server instances.

func WithCollector

func WithCollector(collectors ...prometheus.Collector) Option

WithCollector add collectors to the metrics.

func WithDocEndpoint added in v0.8.0

func WithDocEndpoint(serviceName, basePath, filepath string, json []byte) Option

WithDocEndpoint sets the options for the mux server to serve API docs.

func WithExposeAt added in v0.7.0

func WithExposeAt(path string) Option

WithExposeAt sets the path to expose the metrics.

func WithGRPC added in v0.14.0

func WithGRPC(grpc *GRPC) Option

WithGRPC sets up gRPC server options.

func WithGRPCObserver

func WithGRPCObserver(observer GRPCObserver) Option

WithGRPCObserver sets the GRPCObserver collector manager, used to append UnaryServerInterceptor and StreamServerInterceptor Apply to GRPC server instances.

func WithGRPCRateLimiter added in v0.15.0

func WithGRPCRateLimiter(limiter GRPCRateLimiter) Option

WithGRPCRateLimiter sets the GRPCRateLimiter, used to append UnaryServerInterceptor. Apply to GRPC server instances.

func WithGRPCRest added in v0.14.0

func WithGRPCRest(grpcRest *GRPCRest) Option

WithGRPCRest sets up gRPC REST and with server options.

func WithGRPCServer added in v0.6.0

func WithGRPCServer(grpcSrv *GRPC) Option

WithGRPCServer sets the GRPC server.

func WithGrpcHealthCheck added in v0.14.0

func WithGrpcHealthCheck() Option

WithGrpcHealthCheck sets service to enable health check. Apply to GRPC server instances.

func WithHandlers added in v0.2.0

func WithHandlers(handlers map[string]http.Handler) Option

WithHandlers sets the options for custom path handlers to the mux server. Apply to GRPCRest server instances.

func WithHealthCheck

func WithHealthCheck(checks ...health.Config) Option

WithHealthCheck sets up health check server.

func WithListener added in v0.2.0

func WithListener(l net.Listener, shouldCloseListener bool) Option

WithListener sets the listener. Server does not need to start a new one.

func WithLogger

func WithLogger(logger ctxd.Logger) Option

WithLogger sets service to use logger. Apply to GRPC server instances.

func WithRateLimiter added in v0.15.0

func WithRateLimiter(observer GRPCObserver) Option

WithRateLimiter sets the rate limiter for the gRPC server.

func WithReflection

func WithReflection() Option

WithReflection sets service to implement reflection. Mainly used in dev. Apply to GRPC server instances.

func WithRegisterService

func WithRegisterService(r GRPCRegisterService) Option

WithRegisterService registers a service. Apply to GRPC server instances.

func WithRegisterServiceHandler added in v0.2.0

func WithRegisterServiceHandler(r GRPCRestRegisterService) Option

WithRegisterServiceHandler registers a service handler to the mux server. Apply to GRPCRes server instances.

func WithResponseModifier added in v0.9.0

func WithResponseModifier(modifier ...func(ctx context.Context, w http.ResponseWriter, _ proto.Message) error) Option

WithResponseModifier sets the options for custom response modifier to the mux server.

func WithServerMuxOption added in v0.2.0

func WithServerMuxOption(opts ...runtime.ServeMuxOption) Option

WithServerMuxOption sets the options for the mux server. Apply to GRPCRest server instances.

func WithServerOption

func WithServerOption(opts ...grpc.ServerOption) Option

WithServerOption sets the options for the grpc server. Apply to GRPC server instances.

func WithVersionEndpoint added in v0.8.0

func WithVersionEndpoint() Option

WithVersionEndpoint sets the options for the mux server to serve version.

type PerClientRateLimiter added in v0.15.0

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

PerClientRateLimiter is a gRPC interceptor that limits the number of requests per client.

func NewPerClientRateLimiter added in v0.15.0

func NewPerClientRateLimiter(rps float64, burst int) *PerClientRateLimiter

NewPerClientRateLimiter creates a new PerClientRateLimiter with the given requests per second and burst limit.

func (*PerClientRateLimiter) UnaryServerInterceptor added in v0.15.0

func (r *PerClientRateLimiter) UnaryServerInterceptor() func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error)

UnaryServerInterceptor returns a new unary server interceptor that limits the number of requests per client.

type REST

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

REST is a listening HTTP server instance.

func NewPProf

func NewPProf(cfg Config) *REST

NewPProf creates a new REST service dedicated to serving pprof routes. This service is designed as an alternative to the default pprof handler, eliminating the necessity to rely on `http.DefaultServeMux`.

func NewREST

func NewREST(config Config, handler http.Handler, opts ...Option) *REST

NewREST constructs a new rest Server.

func (*REST) Start

func (srv *REST) Start() error

Start starts serving the REST server.

func (*REST) Stop

func (srv *REST) Stop()

Stop gracefully shuts down the REST server.

type Server

type Server struct {
	AddrAssigned chan string
	// contains filtered or unexported fields
}

Server is a listening server instance.

func NewServer

func NewServer(config Config, opts ...Option) *Server

NewServer constructs a new Server.

func (*Server) Addr

func (srv *Server) Addr() string

Addr service address.

func (*Server) Name

func (srv *Server) Name() string

Name Service name.

func (*Server) Start

func (srv *Server) Start() error

Start starts serving the server.

func (*Server) Stop

func (srv *Server) Stop()

Stop gracefully shuts down the grpc server.

func (*Server) WithShutdownSignal

func (srv *Server) WithShutdownSignal(shutdown <-chan struct{}, done chan<- struct{})

WithShutdownSignal adds channels to wait for shutdown and to report shutdown finished.

type Status added in v0.11.0

type Status struct {
	*status.Status
	// contains filtered or unexported fields
}

Status references the status.Status from google.golang.org/grpc/status.

func (*Status) Err added in v0.13.0

func (s *Status) Err() error

Err returns an immutable error representing s; returns nil if s.Code() is OK.

Jump to

Keyboard shortcuts

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