// Middleware is a simple http.Handler middleware that extracts the RealIP from
// the request and set it on the request context. Anything downstream can then
// simply call realip.RealIP to extract the real ip from the request.
164225
func Middleware(h http.Handler, r *Resolver) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		ip := r.RealIP(req)
		h.ServeHTTP(w, req.WithContext(context.WithValue(req.Context(), realIPKey, ip)))
	})
}
1302583
func (m *Middleware) Handler(h http.Handler, component string) *Handler {
	return &Handler{
		m:         m,
		handler:   h,
		component: component,
	}
}
419615
func Chain(hs ...Middleware) Middleware {
	return func(h http.Handler) http.Handler {
		for i := len(hs); i > 0; i-- {
			h = hs[i-1](h)
		}
		return h
	}
}
263194
func handlerToMiddleware(m Handler) Middleware {
	return func(h Handler) Handler {
		return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
			if err := m(ctx, rw, req); err != nil {
				return err
			}
			return h(ctx, rw, req)
		}
	}
}
830838
func Middleware(level int, o ...Option) goa.Middleware {
	opts := options{
		ignoreRange:  true,
		minSize:      256,
		contentTypes: defaultContentTypes,
	}
	opts.statusCodes = make(map[int]struct{}, len(defaultStatusCodes))
	for _, v := range defaultStatusCodes {
		opts.statusCodes[v] = struct{}{}
	}
	for _, opt := range o {
		err := opt(&opts)
		if err != nil {
			panic(err)
		}
	}
	gzipPool := sync.Pool{
		New: func() interface{} {
			gz, err := gzip.NewWriterLevel(ioutil.Discard, level)
			if err != nil {
				panic(err)
			}
			return gz
		},
	}
	return func(h goa.Handler) goa.Handler {
		return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
			// Skip compression if the client doesn't accept gzip encoding, is
			// requesting a WebSocket or the data is already compressed.
			if !strings.Contains(req.Header.Get(headerAcceptEncoding), encodingGzip) ||
				len(req.Header.Get(headerSecWebSocketKey)) > 0 ||
				rw.Header().Get(headerContentEncoding) == encodingGzip ||
				(!opts.ignoreRange && req.Header.Get(headerRange) != "") {
				return h(ctx, rw, req)
			}

			// Set the appropriate gzip headers.
			resp := goa.ContextResponse(ctx)

			// Get the original http.ResponseWriter
			w := resp.SwitchWriter(nil)

			// Wrap the original http.ResponseWriter with our gzipResponseWriter
			grw := &gzipResponseWriter{
				ResponseWriter: w,
				pool:           &gzipPool,
				statusCode:     http.StatusOK,
				o:              opts,
			}

			// Set the new http.ResponseWriter
			resp.SwitchWriter(grw)

			// We cannot do ranges, if possibly gzipped responses.
			req.Header.Del("Range")

			// Call the next handler supplying the gzipResponseWriter instead of
			// the original.
			err = h(ctx, rw, req)
			if err != nil {
				return
			}

			// Check for uncompressed data
			if grw.buf.Len() > 0 {
				w.Header().Set(headerContentLength, strconv.Itoa(grw.buf.Len()))
				w.WriteHeader(grw.statusCode)
				_, err = w.Write(grw.buf.Bytes())
				return
			}

			// Flush compressor.
			if grw.gzw != nil {
				if err = grw.gzw.Close(); err != nil {
					return
				}
				gzipPool.Put(grw.gzw)
				return
			}
			// No writes, set status code.
			if grw.shouldCompress == nil {
				w.WriteHeader(grw.statusCode)
			}
			return
		}
	}
}
1280328
func (h *Hitch) Handler() http.Handler {
	handler := http.Handler(h.Router)
	for i := len(h.middleware) - 1; i >= 0; i-- {
		handler = h.middleware[i](handler)
	}
	return handler
}
301700
func WrapMiddlewareFromHandler(h HandlerFunc) Middleware {
	return MiddlewareFunc(func(next Handler) Handler {
		return HandlerFunc(func(c Context) error {
			if err := h.Handle(c); err != nil {
				return err
			}
			return next.Handle(c)
		})
	})
}
838900
func UseEventHandlerMiddleware(h EventHandler, middleware ...EventHandlerMiddleware) EventHandler {
	// Apply in reverse order.
	for i := len(middleware) - 1; i >= 0; i-- {
		m := middleware[i]
		h = m(h)
	}
	return h
}
841955
func ChainMiddlewares(h http.Handler, ms ...Middleware) http.Handler {
	return ChainMiddlewaresWithPrefix(h, []string{}, ms...)
}
1154541
func (s *ServeMux) Middleware(path string, middleware Middleware) {
	s.Route(path).Middleware(middleware)
}
710208
func middleware(event *bugsnag.Event, config *bugsnag.Configuration) error {
	for _, datum := range event.RawData {
		if controller, ok := datum.(*revel.Controller); ok {
			// make the request visible to the builtin HttpMiddleware
			event.Context = controller.Action
			event.MetaData.AddStruct("Session", controller.Session)
		}
	}

	return nil
}
1288261
func Middleware(store sessions.Store) echo.MiddlewareFunc {
	c := DefaultConfig
	c.Store = store
	return MiddlewareWithConfig(c)
}
352367
func Middleware(ce *casbin.Enforcer) echo.MiddlewareFunc {
	c := DefaultConfig
	c.Enforcer = ce
	return MiddlewareWithConfig(c)
}
1322341
func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc {
	return func(next HandlerFunc) HandlerFunc {
		return func(c Context) (err error) {
			m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				c.SetRequest(r)
				err = next(c)
			})).ServeHTTP(c.Response(), c.Request())
			return
		}
	}
}
678113
func middleware(h http.Handler) http.Handler {
	if h == nil {
		return nil
	}
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, cn := w.(http.CloseNotifier)
		_, fl := w.(http.Flusher)
		_, hj := w.(http.Hijacker)
		_, rf := w.(io.ReaderFrom)

		bw := basicWriter{ResponseWriter: w}

		if cn && fl && hj && rf {
			h.ServeHTTP(&fancyWriter{bw}, r)
		} else {
			h.ServeHTTP(&bw, r)
		}
		if !bw.headerWritten {
			bw.maybeClose()
		}
	})
}
1168726
func Middleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	var authToken string = strings.Replace(r.Header.Get("Authorization"), "Bearer ", "", 1)
	claims, err := getClaims(authToken)
	if err == nil || failRequest == false {
		if err == nil {
			context.Set(r, context_key, claims)
		}
		next(rw, r)
	} else {
		rw.WriteHeader(401)
	}
}
1106789
func (r *Router) middlewareSelect(handle Handle) httprouter.Handle {
	if r.recoverPanic {
		return r.MWErrorPanic(handle)
	}
	return r.MWError(handle)
}