router

package
v0.58.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2021 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleError

func HandleError(err error, w http.ResponseWriter, r *http.Request)

HandleError will handle any error. If it is of type *HTTPError then it will log anything of a 50x or an InternalError. It will write the right error response to the client. This way if you return a BadRequestError, it will simply write to the client. Any non-HTTPError will be treated as unhandled and result in a 50x

func HandlerFunc

func HandlerFunc(fn APIHandler) http.HandlerFunc

func OptEnableCORS

func OptEnableCORS(r *chiWrapper)

func SendJSON

func SendJSON(w http.ResponseWriter, status int, obj interface{}) error

SendJSON will write the response object as JSON

Types

type APIHandler

type APIHandler func(w http.ResponseWriter, r *http.Request) error

type HTTPError

type HTTPError struct {
	Code            int         `json:"code"`
	Message         string      `json:"msg"`
	JSON            interface{} `json:"json,omitempty"`
	InternalError   error       `json:"-"`
	InternalMessage string      `json:"-"`
	ErrorID         string      `json:"error_id,omitempty"`
}

HTTPError is an error with a message and an HTTP status code.

func BadRequestError

func BadRequestError(fmtString string, args ...interface{}) *HTTPError

BadRequestError creates a 400 HTTP error

func InternalServerError

func InternalServerError(fmtString string, args ...interface{}) *HTTPError

InternalServerError creates a 500 HTTP error

func NotFoundError

func NotFoundError(fmtString string, args ...interface{}) *HTTPError

NotFoundError creates a 404 HTTP error

func UnauthorizedError

func UnauthorizedError(fmtString string, args ...interface{}) *HTTPError

UnauthorizedError creates a 401 HTTP error

func UnavailableServiceError

func UnavailableServiceError(fmtString string, args ...interface{}) *HTTPError

UnavailableServiceError creates a 503 HTTP error

func (*HTTPError) Cause

func (e *HTTPError) Cause() error

Cause will return the root cause error

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error will describe the HTTP error in text

func (*HTTPError) WithInternalError

func (e *HTTPError) WithInternalError(err error) *HTTPError

WithInternalError will add internal error information to an error

func (*HTTPError) WithInternalMessage

func (e *HTTPError) WithInternalMessage(fmtString string, args ...interface{}) *HTTPError

WithInternalMessage will add and internal message to an error

func (*HTTPError) WithJSONError

func (e *HTTPError) WithJSONError(json interface{}) *HTTPError

WithJSONError will add json details to the error

type Middleware

type Middleware func(http.Handler) http.Handler

func BugSnag added in v0.41.0

func BugSnag() Middleware

func CheckAuth

func CheckAuth(secret string) Middleware

func HealthCheck

func HealthCheck(route string, f APIHandler) Middleware

func MiddlewareFunc

func MiddlewareFunc(f func(w http.ResponseWriter, r *http.Request, next http.Handler)) Middleware

func Recoverer

func Recoverer(errLog logrus.FieldLogger) Middleware

Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recoverer prints a request ID if one is provided.

func TrackAllRequests added in v0.48.0

func TrackAllRequests(log logrus.FieldLogger, service string) Middleware

func VersionHeader

func VersionHeader(serviceName, version string) Middleware

type Option

type Option func(r *chiWrapper)

func OptEnableTracing added in v0.48.0

func OptEnableTracing(svcName string) Option

func OptHealthCheck

func OptHealthCheck(path string, checker APIHandler) Option

func OptRecoverer added in v0.38.0

func OptRecoverer() Option

func OptVersionHeader

func OptVersionHeader(svcName, version string) Option

type Router

type Router interface {
	// Use appends one middleware onto the Router stack.
	Use(fn Middleware)

	// With adds an inline middleware for an endpoint handler.
	With(fn Middleware) Router

	// Route mounts a sub-Router along a `pattern“ string.
	Route(pattern string, fn func(r Router))

	// Method adds a routes for a `pattern` that matches the `method` HTTP method.
	Method(method, pattern string, h APIHandler)

	// HTTP-method routing along `pattern`
	Delete(pattern string, h APIHandler)
	Get(pattern string, h APIHandler)
	Post(pattern string, h APIHandler)
	Put(pattern string, h APIHandler)

	// Mount attaches another http.Handler along ./pattern/*
	Mount(pattern string, h http.Handler)

	ServeHTTP(http.ResponseWriter, *http.Request)
}

Router wraps the chi router to make it slightly more accessible

func New

func New(log logrus.FieldLogger, options ...Option) Router

New creates a router with sensible defaults (xff, request id, cors)

Jump to

Keyboard shortcuts

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