service

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT Imports: 20 Imported by: 1

README

Go Report Card godoc license Coverage

service

A service package to serve grpc, grpc-gateway, and additional http handlers on a single port

Documentation

Index

Constants

View Source
const (
	// EnvPort is the primary port to serve everything on.
	EnvPort = "PORT"

	// EnvGRPCPort serves GRPC on a separate port
	EnvGRPCPort = "GRPC_PORT"

	// EnvDebug enables debug output
	EnvDebug = "SERVICE_DEBUG"

	// EnvInsecureVerifySkip disables TLS verification
	EnvInsecureVerifySkip = "INSECURE_VERIFY_SKIP"

	// EnvTLSDisabled will disable TLS (will only support HTTP/1 requests)
	EnvTLSDisabled = "TLS_DISABLED"

	// EnvCorsEnable env var (default: false)
	EnvCorsEnable = "CORS_ENABLE"

	// EnvCorsDebug env var (default: false)
	EnvCorsDebug = "CORS_DEBUG"

	// EnvCorsAllowedOrigins env var
	EnvCorsAllowedOrigins = "CORS_ALLOWED_ORIGINS"

	// EnvCorsAllowedMethods env var
	EnvCorsAllowedMethods = "CORS_ALLOWED_METHODS"

	// EnvCorsAllowedHeaders env var
	EnvCorsAllowedHeaders = "CORS_ALLOWED_HEADERS"
)

Variables

View Source
var (
	// ErrInvalidPort error message
	ErrInvalidPort                   = errors.New("invalid port %d")
	ErrInvalidRecieveSize            = errors.New("invalid max recieve size %d")
	ErrInvalidSendSize               = errors.New("invalid max send size %d")
	ErrMissingGrpcServerOptions      = errors.New("invalid grpc server options: missing")
	ErrMissingGrpcUnaryInterceptors  = errors.New("invalid grpc unary interceptors: missing")
	ErrMissingGrpcStreamInterceptors = errors.New("invalid grpc stream interceptors: missing")
	ErrMissingGatewayHandlers        = errors.New("invalid grpc gateway handlers: missing")
	ErrWithGRPCServerIsNil           = errors.New("invalid grpc server: is nil ")
	ErrServeGRPCNotYetDefined        = errors.New("service serve gprc not yet defined (has pb.RegisterYourServer(service.GRPC(), ...) been called?)")

	ErrDisableTLSCertsMissingGRPCPort = errors.New("invalid grpc port: must set GRPC port when disabling TLS (use environment " + EnvGRPCPort + " or WithGRPCPort(port)")

	// ErrAddingRootCA error message
	ErrAddingRootCA = errors.New("Unable to add Root CA to cert pool, invalid cert.")

	// ErrMissingPublicCert error message
	ErrMissingPublicCert = errors.New("Missing public cert. Fix by using: service.AddKeyPair or server.AddKeyPairFromFiles")

	// ErrMissingPrivateKey error message
	ErrMissingPrivateKey = errors.New("Missing private key. Fix by using: service.AddKeyPair or server.AddKeyPairFromFiles")

	// ErrAddKeyPairPublicCertIsNil error message
	ErrAddKeyPairPublicCertIsNil = errors.New("Unable to add key pair, public cert is nil")

	// ErrAddKeyPairPrivateKeyIsNil error message
	ErrAddKeyPairPrivateKeyIsNil = errors.New("Unable to add key pair, private key is nil")

	// ErrAddKeyPairFromFilePublicCertEmpty error message
	ErrAddKeyPairFromFilePublicCertEmpty = errors.New("Unable to add key pair from files, public cert is empty")

	// ErrAddKeyPairFromFilePublicCertNotFound error message
	ErrAddKeyPairFromFilePublicCertNotFound = errors.New("Unable to add key pair, public cert not found at %s")

	// ErrAddKeyPairPrivateKeyEmpty error message
	ErrAddKeyPairPrivateKeyEmpty = errors.New("Unable to add key pair, private key is nil")

	// ErrAddKeyPairFromFilePrivateKeyNotFound error message
	ErrAddKeyPairFromFilePrivateKeyNotFound = errors.New("Unable to add key pair, private key not found at %s")
)

Functions

func WithDebug added in v0.2.2

func WithDebug() func(*Service) error

func WithGRPCPort added in v0.2.2

func WithGRPCPort(port int) func(*Service) error

func WithGRPCServer added in v0.2.2

func WithGRPCServer(server *grpc.Server) func(*Service) error

func WithGRPCServerOptions added in v0.2.2

func WithGRPCServerOptions(opts ...grpc.ServerOption) func(*Service) error

func WithGatewayHandlers added in v0.2.2

func WithGatewayHandlers(handlers ...func(context.Context, *runtime.ServeMux, *grpc.ClientConn) error) func(*Service) error

func WithHTTPMiddleware added in v0.2.2

func WithHTTPMiddleware(handlers ...httpHandler) func(*Service) error

func WithInsecureSkipVerify added in v0.2.2

func WithInsecureSkipVerify() func(*Service) error

func WithMaxRecieveSize added in v0.2.2

func WithMaxRecieveSize(size int) func(*Service) error

func WithMaxSendSize added in v0.2.2

func WithMaxSendSize(size int) func(*Service) error

func WithPort added in v0.2.2

func WithPort(port int) func(*Service) error

func WithServerMuxOptions added in v0.2.2

func WithServerMuxOptions(opts ...runtime.ServeMuxOption) func(*Service) error

func WithStreamInterceptors added in v0.2.2

func WithStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) func(*Service) error

func WithTLSDisabled added in v0.2.2

func WithTLSDisabled() func(*Service) error

func WithUnaryInterceptors added in v0.2.2

func WithUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) func(*Service) error

Types

type Option added in v0.2.2

type Option func(*Service) error

type Service

type Service struct {
	HTTP   *http.ServeMux
	Router *chi.Mux

	CertPool   *x509.CertPool
	PublicCert []byte
	PrivateKey []byte
	// contains filtered or unexported fields
}

func New

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

func (*Service) AddGatewayHandlers added in v0.2.2

func (s *Service) AddGatewayHandlers(handlers ...func(context.Context, *runtime.ServeMux, *grpc.ClientConn) error) error

func (*Service) AddHTTPMiddleware added in v0.2.0

func (s *Service) AddHTTPMiddleware(handler httpHandler)

func (*Service) AddKeyPair

func (s *Service) AddKeyPair(publicCert, privateKey []byte) error

AddKeyPair allows for direct setting of cert/keys

func (*Service) AddKeyPairFromFiles

func (s *Service) AddKeyPairFromFiles(publicCertFile, privateKeyFile string) error

AddKeyPairFromFiles allows for setting cert/key from files

func (*Service) AddStreamInterceptors added in v0.2.2

func (s *Service) AddStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) error

func (*Service) AddUnaryInterceptors added in v0.2.2

func (s *Service) AddUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) error

func (*Service) AppendCertsFromPEM

func (s *Service) AppendCertsFromPEM(rootCA []byte) error

AppendCertsFromPEM support direct setting of rootCA

func (*Service) GRPC added in v0.2.2

func (s *Service) GRPC() *grpc.Server

func (*Service) GetCertificate

func (s *Service) GetCertificate() (tls.Certificate, error)

GetCertificate is a helper for pulling tls.Certificate from provided cert/key

func (*Service) PrintDebug added in v0.2.2

func (s *Service) PrintDebug()

func (*Service) Serve

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

func (*Service) Shutdown

func (s *Service) Shutdown(ctx context.Context) error

Shutdown attempts to gracefully shutdown server given a context timeout

func (*Service) WithOptions added in v0.2.2

func (s *Service) WithOptions(opts ...Option) error

Directories

Path Synopsis
protos
Package protos is a reverse proxy.
Package protos is a reverse proxy.

Jump to

Keyboard shortcuts

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