Documentation ¶
Index ¶
- Constants
- Variables
- func WriteJsonError(w http.ResponseWriter, v ErrorResponse, code int) (int, error)
- type Chain
- type DatabaseError
- type ErrorEntity
- type ErrorResponse
- type JsonResponseWriter
- func (w *JsonResponseWriter) CloseNotify() <-chan bool
- func (w *JsonResponseWriter) EncodeJson(v interface{}) ([]byte, error)
- func (w *JsonResponseWriter) Flush()
- func (w *JsonResponseWriter) Header() http.Header
- func (w *JsonResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (w *JsonResponseWriter) Write(b []byte) (int, error)
- func (w *JsonResponseWriter) WriteError(herr interface{}, code int) error
- func (w *JsonResponseWriter) WriteHeader(code int)
- func (w *JsonResponseWriter) WriteJson(v interface{}) error
- type Middleware
- func Cors(defaultHeaders string) Middleware
- func Delay() Middleware
- func DumpIncomingRequest(logger *log.Logger) Middleware
- func Json() Middleware
- func JsonHeaderCheck() Middleware
- func Logging(callback func(s string)) Middleware
- func NoCache() Middleware
- func Recovery(callback func(requestDump []byte, stackDump []byte)) Middleware
- func SetBodyLimit(limit int64) Middleware
- func SqlDb(databaseDriver string, dsn string) Middleware
- func Timeout(timeoutAfter time.Duration) Middleware
- func URLQuery() Middleware
- func WithValue(key interface{}, val interface{}) Middleware
- type Queries
Constants ¶
const DELAY_HEADER_KEY = "X-Add-Delay"
const SqlDbContextKey contextKey = "sqldb"
const URLQueryKey contextKey = "urlquery"
URLQueryKey is the context key for the URL Query
Variables ¶
var Close = close
var Open = open
Functions ¶
func WriteJsonError ¶
func WriteJsonError(w http.ResponseWriter, v ErrorResponse, code int) (int, error)
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain acts as a list of middleware. Chain is effectively immutable: once created, it will always hold the same set of constructors in the same order.
func Create ¶
func Create(chain Chain, middleware ...Middleware) Chain
Create a chain, adding the specified middleware as the last ones in the request flow.
stdChain := easy_middleware.New(m1, m2) extChain := Create(stdChain, m3, m4) // requests in stdChain go m1 -> m2 // requests in extChain go m1 -> m2 -> m3 -> m4
func Merge ¶
Merge two chains Returns a new chain, leaving the originals one untouched.
stdChain := easy_middleware.New(m1, m2) stdChain := easy_middleware.New(m3, m4) mergedChain := Merge(m3, m4) // requests in stdChain go m1 -> m2 // requests in extChain go m1 -> m2 -> m3 -> m4
func New ¶
func New(middleware ...Middleware) Chain
New creates a new chain, memorizing the given list of middleware. New serves no other function, constructors are only called upon a call to Then().
func (*Chain) Add ¶
func (c *Chain) Add(middleware ...Middleware)
Add a middleware to a existing chain stdChain := easy_middleware.New(m1, m2) stdChainCopy := stdChain.Copy() stdChainCopy := stdChain.Add(m1) or stdChainCopy := stdChain.Add(m1, m2)
func (Chain) Copy ¶
Copy middleware of existing chain and takes that as base for a new chain stdChain := easy_middleware.New(m1, m2) stdChainCopy := stdChain.Copy()
func (Chain) Then ¶
Then chains the middleware and returns the final http.Handler.
New(m1, m2, m3).Then(h)
is equivalent to:
m1(m2(m3(h)))
When the request comes in, it will be passed to m1, then m2, then m3 and finally, the given handler (assuming every middleware calls the following one).
A chain can be safely reused by calling Then() several times.
stdStack := easy_middleware.New(ratelimitHandler, csrfHandler) indexPipe = stdStack.Then(indexHandler) authPipe = stdStack.Then(authHandler)
Note that constructors are called on every call to Then() and thus several instances of the same middleware will be created when a chain is reused in this way. For proper middleware, this should cause no problems.
Then() treats nil as http.DefaultServeMux.
func (Chain) ThenFunc ¶
func (c Chain) ThenFunc(endpointFunc http.HandlerFunc) http.Handler
ThenFunc works identically to Then, but takes a HandlerFunc instead of a Handler.
The following two statements are equivalent:
c.Then(http.HandlerFunc(fn)) c.ThenFunc(fn)
ThenFunc provides all the guarantees of Then.
type DatabaseError ¶
type DatabaseError struct {
// contains filtered or unexported fields
}
DatabaseError represenst a error for bad database connection, it's occurs while a request process
func (*DatabaseError) Error ¶
func (e *DatabaseError) Error() string
type ErrorEntity ¶
type ErrorEntity struct { // Code is the HTTP response status code and will always be populated. Code int `json:"code"` Message interface{} `json:"message"` }
type ErrorResponse ¶
type ErrorResponse struct {
Error ErrorEntity `json:"error"`
}
func NewError ¶
func NewError(message interface{}, code int) ErrorResponse
type JsonResponseWriter ¶
type JsonResponseWriter struct {
http.ResponseWriter
}
It implements the following interfaces: ResponseWriter http.ResponseWriter http.Flusher http.CloseNotifier http.Hijacker
func (*JsonResponseWriter) CloseNotify ¶
func (w *JsonResponseWriter) CloseNotify() <-chan bool
Call the parent CloseNotify. Provided in order to implement the http.CloseNotifier interface.
func (*JsonResponseWriter) EncodeJson ¶
func (w *JsonResponseWriter) EncodeJson(v interface{}) ([]byte, error)
Replace the parent EncodeJson to provide indentation.
func (*JsonResponseWriter) Flush ¶
func (w *JsonResponseWriter) Flush()
Make sure the local WriteHeader is called, and call the parent Flush. Provided in order to implement the http.Flusher interface.
func (*JsonResponseWriter) Header ¶
func (w *JsonResponseWriter) Header() http.Header
func (*JsonResponseWriter) Hijack ¶
func (w *JsonResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
Provided in order to implement the http.Hijacker interface.
func (*JsonResponseWriter) Write ¶
func (w *JsonResponseWriter) Write(b []byte) (int, error)
Make sure the local WriteHeader is called, and call the parent Write. Provided in order to implement the http.ResponseWriter interface.
func (*JsonResponseWriter) WriteError ¶
func (w *JsonResponseWriter) WriteError(herr interface{}, code int) error
func (*JsonResponseWriter) WriteHeader ¶
func (w *JsonResponseWriter) WriteHeader(code int)
Call the parent WriteHeader.
func (*JsonResponseWriter) WriteJson ¶
func (w *JsonResponseWriter) WriteJson(v interface{}) error
Make sure the local EncodeJson and local Write are called. Does not call the parent WriteJson.
type Middleware ¶
func Cors ¶
func Cors(defaultHeaders string) Middleware
CORS Middleware creates a new CORS Middleware with default headers.
func Delay ¶ added in v1.6.0
func Delay() Middleware
decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
func DumpIncomingRequest ¶
func DumpIncomingRequest(logger *log.Logger) Middleware
Dump request middleware creates a dump of in coming request.
func JsonHeaderCheck ¶
func JsonHeaderCheck() Middleware
Verifies the request Content-Type header and returns a StatusUnsupportedMediaType (415) HTTP error response if it's incorrect. The expected Content-Type is 'application/json' if the content is non-null. Note: If a charset parameter exists, it MUST be UTF-8.
func NoCache ¶ added in v1.3.0
func NoCache() Middleware
NoCache is a simple piece of middleware that sets a number of HTTP headers to prevent a router from being cached by an upstream proxy and/or client.
func Recovery ¶
func Recovery(callback func(requestDump []byte, stackDump []byte)) Middleware
Recovery middleware for panic
func SetBodyLimit ¶
func SetBodyLimit(limit int64) Middleware
func SqlDb ¶
func SqlDb(databaseDriver string, dsn string) Middleware
SqlDb is a middlware for creating a unique session for each incomming request
func Timeout ¶ added in v1.5.0
func Timeout(timeoutAfter time.Duration) Middleware
Timeout is a middleware that cancels ctx after a given timeout and return a 504 Gateway Timeout error to the client.
func URLQuery ¶ added in v1.2.0
func URLQuery() Middleware
URLQuery is a middleware to parse the URL Query parameters just once, and store the resulting url.Values in the context.
func WithValue ¶ added in v1.1.0
func WithValue(key interface{}, val interface{}) Middleware
WithValue is a middleware that sets a given key/value in the request context.