Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Adapt ¶
func Adapt(h http.HandlerFunc, middlewares ...Middleware) http.HandlerFunc
Adapt is used to wrap a http handler with Middleware
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/fossapps/starter/middleware" ) func main() { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Println("handler function") }) newHandler := middleware.Adapt(handler, getTestAdapter()) newHandler(httptest.NewRecorder(), nil) } func getTestAdapter() middleware.Middleware { return func(handler http.Handler) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { fmt.Println("before") defer fmt.Println("after") handler.ServeHTTP(w, r) } } }
Output: before handler function after
Types ¶
type LimiterOptions ¶
type LimiterOptions struct { Namespace string RequestHelper RequestHelper Limit int AddHeaders bool Logger Logger Limiter RateLimiter Jwt jwt.Manager }
LimiterOptions dependencies of Limit middleware
type Logger ¶
type Logger interface {
Warn(args ...interface{})
}
Logger implementation needed for Limit middleware to log
type Middleware ¶
type Middleware func(http.Handler) http.HandlerFunc
Middleware should be returned from a func to be used as handler (a.k.a middlewares)
func AuthMw ¶
func AuthMw(jwtManager jwt.Manager) Middleware
AuthMw ensures request is authenticated
func Limit ¶
func Limit(options LimiterOptions) Middleware
Limit middleware ensures a handler isn't called more than N times in D duraton
func MustHavePermission ¶
func MustHavePermission(permission string, manager jwt.Manager) Middleware
MustHavePermission is a middleware which ensures a request has permission before handler is invoked
func ResponseTime ¶
func ResponseTime(logger ResponseTimeLogger) Middleware
ResponseTime middleware to log response time for a request
type RateLimiter ¶
RateLimiter implementation needed to work
type RequestHelper ¶
RequestHelper see RequestHelper in server.go
type ResponseTimeLogger ¶
type ResponseTimeLogger interface {
Info(args ...interface{})
}
ResponseTimeLogger interface implementation needed for ResponseTime for logging