Documentation ¶
Overview ¶
Package servers provides server implementations.
Index ¶
- Variables
- func Error(c codes.Code, msg string, details ...map[string]string) error
- func NewRestAPIDocsHandlers(serviceName, basePath, swaggerPath string, swaggerJSON []byte) map[string]http.Handler
- func NewRestRootHandler(serviceName string, links ...any) http.Handler
- func NewRestVersionHandler() http.Handler
- func WrapError(c codes.Code, err error, msg string, details ...map[string]string) error
- type Config
- type GRPC
- type GRPCObserver
- type GRPCRateLimiter
- type GRPCRegisterService
- type GRPCRegisterServiceFunc
- type GRPCRest
- type GRPCRestRegisterService
- type HealthCheck
- type Information
- type Metrics
- type Option
- func WithAddrAssigned() Option
- func WithChainStreamInterceptor(interceptors ...grpc.StreamServerInterceptor) Option
- func WithChainUnaryInterceptor(interceptors ...grpc.UnaryServerInterceptor) Option
- func WithCollector(collectors ...prometheus.Collector) Option
- func WithDocEndpoint(serviceName, basePath, filepath string, json []byte) Option
- func WithExposeAt(path string) Option
- func WithGRPC(grpc *GRPC) Option
- func WithGRPCObserver(observer GRPCObserver) Option
- func WithGRPCRateLimiter(limiter GRPCRateLimiter) Option
- func WithGRPCRest(grpcRest *GRPCRest) Option
- func WithGRPCServer(grpcSrv *GRPC) Option
- func WithGrpcHealthCheck() Option
- func WithHandlers(handlers map[string]http.Handler) Option
- func WithHealthCheck(checks ...health.Config) Option
- func WithListener(l net.Listener, shouldCloseListener bool) Option
- func WithLogger(logger ctxd.Logger) Option
- func WithRateLimiter(observer GRPCObserver) Option
- func WithReflection() Option
- func WithRegisterService(r GRPCRegisterService) Option
- func WithRegisterServiceHandler(r GRPCRestRegisterService) Option
- func WithResponseModifier(...) Option
- func WithServerMuxOption(opts ...runtime.ServeMuxOption) Option
- func WithServerOption(opts ...grpc.ServerOption) Option
- func WithVersionEndpoint() Option
- type PerClientRateLimiter
- type REST
- type Server
- type Status
Constants ¶
This section is empty.
Variables ¶
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") )
var ErrGRPCStart = errors.New("start grpc server")
ErrGRPCStart is returned when an error occurs the GRPC server start.
var ErrListenerFailedStart = errors.New("failed to start listener")
ErrListenerFailedStart is returned when an error occurs starting listener for the REST server.
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
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 ¶
NewRestRootHandler creates a handler for an endpoint to response on / path.
func NewRestVersionHandler ¶
NewRestVersionHandler creates a handler for an endpoint to response on /version path to show the version of the api.
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.
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.
type GRPCRestRegisterService ¶ added in v0.2.0
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 (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 ¶
NewMetrics initiates a new wrapped prom server collector.
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
WithDocEndpoint sets the options for the mux server to serve API docs.
func WithExposeAt ¶ added in v0.7.0
WithExposeAt sets the path to expose the metrics.
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
WithGRPCRest sets up gRPC REST and with server options.
func WithGRPCServer ¶ added in v0.6.0
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
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
WithListener sets the listener. Server does not need to start a new one.
func WithLogger ¶
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 ¶
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`.
type Server ¶
type Server struct { AddrAssigned chan string // contains filtered or unexported fields }
Server is a listening server instance.
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.