server

package
v0.0.0-...-f4159b9 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2021 License: MIT Imports: 24 Imported by: 8

README

Server

To quickly get a grpc server (in this example, with non-system TLS certs) you'd simply do this:

Note: this example uses the grpc example protobufs

func startTestServer() error {
    srv, err := server.New(
        server.WithCertificate(tls.LocalhostTLSConfig().Certificates[0]),
    )
    if err != nil {
        return err
    }

    echosrv := grpc.New()

    echo.RegisterEchoServer(
        srv.Transport(),
        echosrv,
    )

    srv.Start()
    <-srv.Notify(server.Ready)
    return nil
}

This expects the following environment variables to be in place when calling WithViper() without arguments

Environment Variable Description Default
DDL_RPC_CLIENT_AUTHENTICATION Values: [NoClientCert, RequestClientCert, RequireAnyClientCert, VerifyClientCertIfGiven, RequireAndVerifyClientCert] NoClientCert
DDL_RPC_TLS_CERTIFICATE Sets the certificate to use for transport encryption
DDL_RPC_KEY Private key that pairs with tls certificate
DDL_RPC_TLS_CA Load a custom CA pool instead of using the system CA empty
DDL_RPC_PORT Sets the listener port. 0
DDL_RPC_TLS_CA Sets the certificate authority for client verification empty
DDL_RPC_INSECURE disable TLS verification false

A full list of options can be found in options.go

Documentation

Index

Constants

View Source
const (
	// EnvironmentPrefix sets the prefix to strip from environment variables when resolving keys. Default: "DDL_RPC".
	EnvironmentPrefix = "env-prefix"
	// EnvironmentReplace takes a comma separated list of old,new. Default: .,_,-,_
	EnvironmentReplace = "env-replace"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*options)

Option provides a function definition to set options

func WithCertPool

func WithCertPool(p *x509.CertPool) Option

WithCertPool overrides the system cert pool.

func WithCertificate

func WithCertificate(c ...tls.Certificate) Option

WithCertificate specifies the certificates that will be appended to the tls.Config.

func WithClientAuth

func WithClientAuth(a tls.ClientAuthType) Option

WithClientAuth sets the tls ClientAuthType to control auth behavior.

func WithHTTPPassthrough

func WithHTTPPassthrough() Option

WithHTTPPassthrough starts an additional HTTP passthrough for rest operations

func WithHTTPPassthroughInsecure

func WithHTTPPassthroughInsecure() Option

WithHTTPPassthroughInsecure enables an insecure http passthrough

func WithInsecureSkipVerify

func WithInsecureSkipVerify() Option

WithInsecureSkipVerify makes connections not that safe to use.

func WithLogger

func WithLogger(l log.Logger) Option

WithLogger set the log instance.

func WithPort

func WithPort(p int) Option

WithPort sets the listener port.

func WithReflectionService

func WithReflectionService() Option

WithReflectionService starts a reflection service capable to describing services

func WithStreamServerInterceptors

func WithStreamServerInterceptors(ss ...grpc.StreamServerInterceptor) Option

WithStreamServerInterceptors sets the streaming middleware.

func WithTLSCert

func WithTLSCert(s string) Option

WithTLSCert statically sets a TLS certificate.

func WithTLSKey

func WithTLSKey(s string) Option

WithTLSKey statically sets a TLS key.

func WithUnaryServerInterceptors

func WithUnaryServerInterceptors(us ...grpc.UnaryServerInterceptor) Option

WithUnaryServerInterceptors sets the unary middleware.

func WithViper

func WithViper(args ...string) Option

WithViper specifies that the server should construct its options using viper.

type Server

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

Server is a server struct

func New

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

New constructs a new Server

func (*Server) Address

func (s *Server) Address() net.Addr

Address returns the address

func (*Server) Errors

func (s *Server) Errors() []error

Errors returns any errors registered during construction, starting, or stopping.

func (*Server) HTTPAddress

func (s *Server) HTTPAddress() net.Addr

HTTPAddress returns the address

func (*Server) Notify

func (s *Server) Notify(states ...State) <-chan State

Notify will send requested rpc.State changes over the channel returned by this function.

func (*Server) RegisterHTTPService

func (s *Server) RegisterHTTPService(in []func(context.Context, *grpc_runtime.ServeMux, string, []grpc.DialOption) error) error

RegisterHTTPService accepts a slice of http gateway services to register

func (*Server) Start

func (s *Server) Start()

Start calls the underlying grpc.Server.Serve

func (*Server) State

func (s *Server) State() State

State returns the current state.

func (*Server) Stop

func (s *Server) Stop()

Stop shuts down the service

func (*Server) Transport

func (s *Server) Transport() *grpc.Server

Transport returns the underlying grpc server.

type State

type State int

State tracks the state of the connection

const (
	// Init is a new connection
	Init State = iota
	// Starting means the connection is starting
	Starting
	// Ready means the connection is ready
	Ready
	// Stopping means the connection is being shut down
	Stopping
	// Stopped means the connection is stopping
	Stopped
	// Error means the connection is in error
	Error
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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