http

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInternalError = fmt.Errorf("internal server error")

Functions

func HandleWithContext

func HandleWithContext[T any](h func(http.ResponseWriter, *http.Request, T), ctx T) http.HandlerFunc

func NewClientError

func NewClientError(err error, status int) error

NewAppError creates a new application error

Types

type ClientError

type ClientError struct {
	Status  int
	Details string
}

AppError holds information about an application error

func (ClientError) As

func (c ClientError) As(target error) bool

func (*ClientError) Body

func (c *ClientError) Body() []byte

Body formats the application error for the caller

func (*ClientError) Error

func (c *ClientError) Error() string

Error fulfills the error interface

type ErrHandler

type ErrHandler struct {
	Handler handlerWithError
	Logger  *logr.Logger
}

errHandler contains a handler that returns an error and a logger

func (*ErrHandler) ServeHTTP

func (e *ErrHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP satisfies the http.Handler interface to allow for handling of errors from handlers in one place

type MiddlewareWithLogger

type MiddlewareWithLogger func(*Server, http.Handler) http.Handler

type Route

type Route struct {
	Method  string
	Path    string
	Handler http.Handler
}

Route contains the information needed for an HTTP handler

type Server

type Server struct {
	Logger   *logr.Logger
	Router   *http.ServeMux
	Exporter *metrics.Exporter

	TracerProvider *trace.TracerProvider
	// contains filtered or unexported fields
}

Server holds the http.Server, a logger, and the router to attach to the http.Server

func NewHTTPServer

func NewHTTPServer(opts ...ServerOption) *Server

NewHTTPServer initializes and returns a new Server

func (*Server) AutoHandleErrors

func (s *Server) AutoHandleErrors(ctx context.Context, errChan <-chan error)

AutoHandleErrors is a convenience that should be called after starting the server. It will automatically safely stop the server if a signal is received. This breaks the normal pattern of letting the caller handle fatal errors, which is why this is a convenience function that's able to be called separately.

func (*Server) RegisterSubRouter

func (s *Server) RegisterSubRouter(prefix string, routes []Route, middleware ...func(http.Handler) http.Handler) *Server

RegisterSubRouter creates a subrouter based on a path and a slice of routes. Any middlewares passed in will be mounted to the sub router

func (*Server) Serve

func (s *Server) Serve(errChan chan<- error)

Serve starts the http.Server

func (*Server) ShutdownServer

func (s *Server) ShutdownServer(ctx context.Context)

type ServerOption

type ServerOption func(*Server)

ServerOption is a functional option to modify the server

func SetIdleTimeout

func SetIdleTimeout(t int) ServerOption

SetIdleTimeout sets the http.Server idle timeout

func SetReadTimeout

func SetReadTimeout(t int) ServerOption

SetReadTimeout sets the http.Server read timeout

func SetServerPort

func SetServerPort(p int) ServerOption

SetServerPort sets the server listening port

func SetTracerProvider

func SetTracerProvider(t *trace.TracerProvider) ServerOption

func SetWriteTimeout

func SetWriteTimeout(t int) ServerOption

SetWriteTimeout sets the http.Server write timeout

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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