router

package
v0.0.0-...-7df1e21 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLogger

func GetLogger(ctx context.Context) *logrus.Entry

GetLogger from context

func HandleError

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

HandleError handles all errors

func HandleSQLError

func HandleSQLError(err error) error

HandleSQLError handle sql errors as internal server error

func LoggerSetField

func LoggerSetField(ctx context.Context, key string, value interface{}) context.Context

LoggerSetField and overwrite the logger entry

func LoggerSetFields

func LoggerSetFields(ctx context.Context, fields logrus.Fields) context.Context

LoggerSetFields and overwrite the logger entry

func QueryParamInt

func QueryParamInt(r *http.Request, key string) int

QueryParamInt get a int query parameter from url

func QueryParamString

func QueryParamString(r *http.Request, key string) string

QueryParamString get a int query parameter from url

func Recoverer

func Recoverer(w http.ResponseWriter, r *http.Request) (context.Context, error)

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 send response as json

func SendList

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

SendList with data wrapper

func SendObject

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

SendObject with data wrapper

func SetLogger

func SetLogger(ctx context.Context, requestLogger *logrus.Entry) context.Context

SetLogger to context

func WithLogger

func WithLogger(log *logrus.Entry) func(w http.ResponseWriter, r *http.Request) (context.Context, error)

WithLogger add metadata to log

Types

type HTTPError

type HTTPError struct {
	Code            int         `json:"error"`
	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 return bad request error

func InternalServerError

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

InternalServerError return internal server error

func NewHTTPError

func NewHTTPError(code int, fmtString string, args ...interface{}) *HTTPError

NewHTTPError create a new http error

func NotFoundError

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

NotFoundError return not found error

func UnauthorizedError

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

UnauthorizedError return unauthorized error

func UnavailableServiceError

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

UnavailableServiceError return unavailable service error

func (*HTTPError) Cause

func (e *HTTPError) Cause() error

Cause returns the root cause error

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error return error as string

func (*HTTPError) WithInternalError

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

WithInternalError adds internal error information to the error

func (*HTTPError) WithInternalMessage

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

WithInternalMessage adds internal message information to the error

func (*HTTPError) WithJSONError

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

WithJSONError add a json to error

type Router

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

Router wrapper to handle automatic error response

func NewRouter

func NewRouter() *Router

NewRouter create new router wrapper

func (*Router) Delete

func (r *Router) Delete(pattern string, fn apiHandler)

Delete HTTP-method routing along `pattern`

func (*Router) Get

func (r *Router) Get(pattern string, fn apiHandler)

Get HTTP-method routing along `pattern`

func (*Router) Post

func (r *Router) Post(pattern string, fn apiHandler)

Post HTTP-method routing along `pattern`

func (*Router) Put

func (r *Router) Put(pattern string, fn apiHandler)

Put HTTP-method routing along `pattern`

func (*Router) Route

func (r *Router) Route(pattern string, fn func(*Router))

Route mounts a sub-Router along a `pattern“ string.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP wrap the chi function

func (*Router) Sse

func (r *Router) Sse(pattern string, fn func(w http.ResponseWriter, r *http.Request))

Sse HTTP-method routing along `pattern`

func (*Router) Use

func (r *Router) Use(fn middlewareHandler)

Use appends one of more middlewares onto the Router stack.

func (*Router) UseBypass

func (r *Router) UseBypass(fn func(next http.Handler) http.Handler)

UseBypass appends one of more middlewares with go-chi signature (no automatic error response)

func (*Router) With

func (r *Router) With(fn middlewareHandler) *Router

With adds inline middlewares for an endpoint handler.

func (*Router) WithBypass

func (r *Router) WithBypass(fn func(next http.Handler) http.Handler) *Router

WithBypass adds inline middlewares with go-chi signature (no automatic error response)

Jump to

Keyboard shortcuts

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