http

package
v0.0.0-...-6ad1837 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChainMiddlewares

func ChainMiddlewares(lastHandler nethttp.Handler, middlewares ...Middleware) nethttp.Handler

ChainMiddlewares returns a net/http.Handler that chains a list of Middlewares where the last middleware is a simple wrapper around the given lastHandler.

func SetErrorForMiddlewares

func SetErrorForMiddlewares(ctx context.Context, err error) bool

SetErrorForMiddlewares tries to return the given error for the chained middlewares and returns true if it was possible.

Types

type HandlerFactory

type HandlerFactory func(ctx context.Context, endpoint string) (nethttp.Handler, error)

HandlerFactory is a function used to create the net/http.Handler for the main server.

type Middleware

Middleware represents an HTTP handler function that receives the next Middleware (through its MiddlewareWrapper) and is allowed to control the execution flow calling this next Middleware or not.

type MiddlewareWrapper

type MiddlewareWrapper func(nethttp.ResponseWriter, *nethttp.Request) error

MiddlewareWrapper wraps around a Middleware.

type Server

type Server struct {
	// HTTPEndpoint is the endpoint to serve the main server.
	HTTPEndpoint string

	// HTTPRandomPort controls if a random port should be chosen to expose the HTTPEndpoint; if
	// true, a random port p is chosen and the leftmost occurrence of POSIX regex ($)|(:[0-9]*$)
	// in HTTPEndpoint is replaced by ":p".
	HTTPRandomPort bool

	// HTTPSEndpoint is the endpoint to serve the main server over TLS.
	HTTPSEndpoint string

	// HTTPSRandomPort controls if a random port should be chosen to expose the HTTPSEndpoint; if
	// true, a random port p is chosen and the leftmost occurrence of POSIX regex ($)|(:[0-9]*$)
	// in HTTPSEndpoint is replaced by ":p".
	HTTPSRandomPort bool

	// CertFile contains the TLS certificate to pass to net/http.Server.ServeTLS().
	CertFile string

	// KeyFile contains the TLS key to pass to net/http.Server.ServeTLS().
	KeyFile string

	// HandlerFactory is used to create the net/http.Handler that serves both http://HTTPEndpoint
	// and https://HTTPSEndpoint if they are set to be created.
	HandlerFactory HandlerFactory

	// InternalEndpoint is the endpoint to serve helper routes like GET /health.
	InternalEndpoint string

	// HealthHandler serves GET http://InternalEndpoint/health.
	HealthHandler nethttp.Handler

	// RegisterInternalHandlers is a hook to register custom handlers at http://InternalEndpoint,
	// except GET /health which is already required.
	RegisterInternalHandlers func(*nethttp.ServeMux)

	// Logger is used to emit logs when the starts and stops.
	Logger logrus.FieldLogger
	// contains filtered or unexported fields
}

Server exposes HTTPEndpoint with the net/http.Handler returned by HandlerFactory, HTTPSEndpoint with the same net/http.Handler and an InternalEndpoint routing GET /health to HealthHandler; the response of HealthHandler is used by WaitForReady() to determine if the server is ready. If HTTPEndpoint is empty, the endpoint is not exposed unless HTTPRandomPort is set to true (the same is valid for HTTPSEndpoint and HTTPSRandomPort). If InternalEndpoint is empty, a random port p is chosen and InternalEndpoint is set to ":p".

func NewServer

func NewServer(server *Server, runtime serverRuntime) *Server

NewServer creates or only initializes (if server is nil) the runtime of a server with the default runtime if runtime is nil, and also initializes other fields that compose internal state.

func (*Server) GracefulShutdown

func (s *Server) GracefulShutdown()

GracefulShutdown notifies the server to stop.

func (*Server) Serve

func (s *Server) Serve() error

Serve starts the server and only returns when it shuts down.

func (*Server) WaitForReady

func (s *Server) WaitForReady(timeout time.Duration) error

WaitForReady waits until the server is ready to serve requests, or returns a timeout error.

Jump to

Keyboard shortcuts

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