echox

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package echox adds helper functions for the echo server

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultShutdownGracePeriod sets the default for how long we give the sever
	// to shutdown before forcefully stopping the server.
	DefaultShutdownGracePeriod = 5 * time.Second
	// DefaultReadTimeout sets the default maximum duration for reading the entire request including the body.
	DefaultReadTimeout = 15 * time.Second
	// DefaultWriteTimeout sets the default maximum duration before timing out writes of the response.
	DefaultWriteTimeout = 15 * time.Second
	// DefaultIdleTimeout sets the default maximum amount of time to wait for the next request when keep-alives are enabled.
	DefaultIdleTimeout = 30 * time.Second
	// DefaultReadHeaderTimeout sets the default amount of time allowed to read request headers.
	DefaultReadHeaderTimeout = 2 * time.Second
	// DefaultCertFile is the default cert file location
	DefaultCertFile = "server.crt"
	// DefaultKeyFile is the default key file location
	DefaultKeyFile = "server.key"
	// DefaultTLSConfig is the default TLS config used when HTTPS is enabled
	DefaultTLSConfig = &tls.Config{
		MinVersion:               tls.VersionTLS12,
		CurvePreferences:         []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
		PreferServerCipherSuites: true,
		CipherSuites: []uint16{
			tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
			tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
		},
	}
)
View Source
var (
	// ErrJWTMissingInvalid is returned when the JWT is missing or invalid
	ErrJWTMissingInvalid = errors.New("JWT token missing or invalid")

	// ErrJWTClaimsInvalid is returned when the token could not be cast to jwt.MapClaims
	ErrJWTClaimsInvalid = errors.New("JWT claims missing or invalid")

	// ErrSubjectNotFound is returned when the sub is not found in the JWT claims
	ErrSubjectNotFound = errors.New("JWT claims missing subject")

	// ErrUnableToRetrieveEchoContext is returned when the echo context is unable to be parsed from parent context
	ErrUnableToRetrieveEchoContext = errors.New("unable to retrieve echo context")
)
View Source
var EchoContextKey = &ContextKey{"EchoContextKey"}

EchoContextKey is the context key for the echo.Context

Functions

func EchoContextFromContext

func EchoContextFromContext(ctx context.Context) (*echo.Context, error)

EchoContextFromContext gets the echo.Context from the parent context

func EchoContextToContextMiddleware

func EchoContextToContextMiddleware() echo.MiddlewareFunc

EchoContextToContextMiddleware is the middleware that adds the echo.Context to the parent context

func GetActorSubject

func GetActorSubject(c echo.Context) (string, error)

GetActorSubject returns the user from the echo.Context

func NewTestContextWithValidUser

func NewTestContextWithValidUser(subject string) (*echo.Context, error)

NewTestContextWithValidUser creates an echo context with a fake subject for testing purposes ONLY

Types

type Config

type Config struct {
	// Debug enables echo's Debug option.
	Debug bool

	// Dev enables echo's dev mode options.
	Dev bool

	// Listen sets the listen address to serve the echo server on.
	Listen string

	// HTTPS configures an https server
	HTTPS bool

	// ShutdownGracePeriod sets the grace period for in flight requests before shutting down.
	ShutdownGracePeriod time.Duration

	// ReadTimeout sets the maximum duration for reading the entire request including the body.
	ReadTimeout time.Duration

	// WriteTimeout sets the maximum duration before timing out writes of the response.
	WriteTimeout time.Duration

	// IdleTimeout sets the maximum amount of time to wait for the next request when keep-alives are enabled.
	IdleTimeout time.Duration

	// ReadHeaderTimeout sets the amount of time allowed to read request headers.
	ReadHeaderTimeout time.Duration

	// TrustedProxies defines the allowed ip / network ranges to trust a proxy from.
	TrustedProxies []string

	// Middleware includes the provided middleware when echo is initialized.
	Middleware []echo.MiddlewareFunc

	// TLSConfig contains the settings for running an HTTPS server.
	TLSConfig TLSConfig
}

Config is used to configure a new echo server

func (Config) WithAutoCert

func (c Config) WithAutoCert(host string) Config

WithAutoCert generates a letsencrypt certificate, a valid host must be provided

func (Config) WithDebug

func (c Config) WithDebug(debug bool) Config

WithDebug enables echo's Debug option.

func (Config) WithDefaultReadTimeout

func (c Config) WithDefaultReadTimeout(period time.Duration) Config

WithDefaultReadTimeout sets the maximum duration for reading the entire request including the body.

func (Config) WithDefaultTLSConfig

func (c Config) WithDefaultTLSConfig() Config

WithDefaultTLSConfig sets the default TLS Configuration

func (Config) WithDefaults

func (c Config) WithDefaults() Config

WithDefaults creates a new config with defaults set if not already defined.

func (Config) WithDev

func (c Config) WithDev(dev bool) Config

WithDev enables echo's dev mode options.

func (Config) WithHTTPS

func (c Config) WithHTTPS(https bool) Config

WithHTTPS enables https server options

func (Config) WithIdleTimeout

func (c Config) WithIdleTimeout(period time.Duration) Config

WithIdleTimeout sets the maximum amount of time to wait for the next request when keep-alives are enabled.

func (Config) WithListen

func (c Config) WithListen(listen string) Config

WithListen sets the listen address to serve the echo server on.

func (Config) WithMiddleware

func (c Config) WithMiddleware(mdw ...echo.MiddlewareFunc) Config

WithMiddleware includes the provided middleware when echo is initialized.

func (Config) WithReadHeaderTimeout

func (c Config) WithReadHeaderTimeout(period time.Duration) Config

WithReadHeaderTimeout sets the amount of time allowed to read request headers.

func (Config) WithShutdownGracePeriod

func (c Config) WithShutdownGracePeriod(period time.Duration) Config

WithShutdownGracePeriod sets the grace period for in flight requests before shutting down.

func (Config) WithTLSCerts

func (c Config) WithTLSCerts(certFile, certKey string) Config

WithTLSCerts sets the TLS Cert and Key locations

func (Config) WithTLSDefaults

func (c Config) WithTLSDefaults() Config

WithTLSDefaults sets tls default settings assuming a default cert and key file location.

func (Config) WithWriteTimeout

func (c Config) WithWriteTimeout(period time.Duration) Config

WithWriteTimeout sets the maximum duration before timing out writes of the response.

type ContextKey

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

ContextKey is the key name for the additional context

type CustomContext

type CustomContext struct {
	echo.Context
	// contains filtered or unexported fields
}

CustomContext contains the echo.Context and request context.Context

type HTTPSConfig

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

HTTPSConfig contains HTTPS server settings

type Server

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

Server implements the HTTPS Server

func NewServer

func NewServer(logger *zap.Logger, cfg Config) (*Server, error)

NewServer will return an opinionated echo server for processing API requests.

func (*Server) AddHandler

func (s *Server) AddHandler(h handler) *Server

AddHandler provides the ability to add additional HTTP handlers that process requests. The handler that is provided should have a Routes(*echo.Group) function, which allows the routes to be added to the server.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns a new http.Handler for serving requests.

func (*Server) RunWithContext

func (s *Server) RunWithContext(ctx context.Context) error

RunWithContext listens and serves the echo server on the configured address. See ServeWithContext for more details.

func (*Server) ServeHTTPSWithContext

func (s *Server) ServeHTTPSWithContext(ctx context.Context, listener net.Listener) error

ServeHTTPSWithContext serves an https server on the provided listener. Serve blocks until SIGINT or SIGTERM are signalled, or if the http serve fails. A graceful shutdown will be attempted

func (*Server) ServeHTTPWithContext

func (s *Server) ServeHTTPWithContext(ctx context.Context, listener net.Listener) error

ServeHTTPWithContext serves an http server on the provided listener. Serve blocks until SIGINT or SIGTERM are signalled, or if the http serve fails. A graceful shutdown will be attempted

type TLSConfig

type TLSConfig struct {
	// TLSConfig contains the tls settings
	TLSConfig *tls.Config
	// AutoCert generates the cert with letsencrypt, this does not work on localhost
	AutoCert bool
	// CertFile location for the TLS server
	CertFile string
	// CertKey file location for the TLS server
	CertKey string
}

TLSConfig contains config options for the HTTPS server

Jump to

Keyboard shortcuts

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