router

package
v0.0.0-...-3e08555 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2020 License: MIT Imports: 14 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 an error

func HandlerFunc

func HandlerFunc(fn APIHandler) http.HandlerFunc

func OptEnableCORS

func OptEnableCORS(r *chiWrapper)

func Recoverer

func Recoverer(w http.ResponseWriter, r *http.Request, next http.Handler)

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 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) *HTTPError

type HTTPError

type HTTPError struct {
	Code            int         `json:"code"`
	Message         string      `json:"msg"`
	JSON            interface{} `json:"json"`
	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 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 VersionHeader

func VersionHeader(serviceName, version string) Middleware

type Option

type Option func(r *chiWrapper)

func OptHealthCheck

func OptHealthCheck(path string, checker APIHandler) Option

func OptTracingMiddleware

func OptTracingMiddleware(log logrus.FieldLogger, svcName string) 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

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