// wrapHandler wraps Handler type
320028
func (l *LARS) wrapHandler(h Handler) HandlerFunc {

	switch h := h.(type) {
	case HandlerFunc:
		return h

	case func(Context):
		return h

	case http.Handler, http.HandlerFunc:
		return func(c Context) {

			ctx := c.BaseContext()

			if h.(http.Handler).ServeHTTP(ctx.response, ctx.request); ctx.response.status != http.StatusOK || ctx.response.committed {
				return
			}

			if ctx.index+1 < len(ctx.handlers) {
				c.Next()
			}
		}

	case func(http.ResponseWriter, *http.Request):
		return func(c Context) {

			ctx := c.BaseContext()

			if h(ctx.response, ctx.request); ctx.response.status != http.StatusOK || ctx.response.committed {
				return
			}

			if ctx.index+1 < len(ctx.handlers) {
				c.Next()
			}
		}

	case func(http.ResponseWriter, *http.Request, http.Handler):

		return func(c Context) {
			ctx := c.BaseContext()

			h(ctx.response, ctx.request, NativeChainHandler)
		}

	case func(http.Handler) http.Handler:

		hf := h(NativeChainHandler)

		return func(c Context) {
			ctx := c.BaseContext()

			hf.ServeHTTP(ctx.response, ctx.request)
		}

	default:
		if fn, ok := l.customHandlersFuncs[reflect.TypeOf(h)]; ok {
			return func(c Context) {
				fn(c, h)
			}
		}

		panic("unknown handler")
	}
}
879118
func FlatHandlerFuncWrap(h http.HandlerFunc) FlatHandler {
	return (func(c *Context, nx NextHandler) {
		h(c.Res, c.Req)
		nx(c)
	})
}
489339
func (a *Module) Wrapper(name string) (wp *Wrapper) {
	wp, _ = a.wrappers[name]
	return
}
576056
func (s *Selection) WrapInner(selector string) *Selection {
	return s.WrapInnerMatcher(compileMatcher(selector))
}
759360
func NewWrapper(wrapperFunc func(w http.ResponseWriter, r *http.Request, routerNext http.HandlerFunc), wrapped http.HandlerFunc) http.Handler {
	return &wrapper{
		wrapperFunc: wrapperFunc,
		router:      wrapped,
	}
}
502653
func WrapHandler(h http.Handler) HandlerFunc {
	return func(c Context) error {
		h.ServeHTTP(c.Response(), c.Request())
		return nil
	}
}
33114
func (l *Limiter) WrapHandle(h http.Handler) {
	l.rateLimiter.Wrap(h)
	l.ConnLimiter.Wrap(l.rateLimiter)
}
209300
func NewHandlerWrapper(hw server.HandlerWrapper, eps ...string) server.HandlerWrapper {
	// create map of endpoints
	endpoints := make(map[string]bool)
	for _, ep := range eps {
		endpoints[ep] = true
	}

	return func(h server.HandlerFunc) server.HandlerFunc {
		return func(ctx context.Context, req server.Request, rsp interface{}) error {
			// execute the handler wrapper?
			if !endpoints[req.Endpoint()] {
				// no
				return h(ctx, req, rsp)
			}

			// yes
			return hw(h)(ctx, req, rsp)
		}
	}
}
497030
func HTTPHandler(srv *rpc.Server) http.Handler {
	if srv == nil {
		srv = rpc.DefaultServer
	}
	return &httpHandler{srv}
}
536597
func (a *Module) AddHandler(c interface{}) *Wrapper {
	name := fmt.Sprintf("%T", c) //example: *controller.Index
	if len(name) > 0 && name[0] == '*' {
		name = name[1:]
	}
	wr := &Wrapper{
		Controller:    c,
		RouteRegister: a.Router(),
		Module:        a,
	}
	if _, ok := c.(Initer); ok {
		_, wr.hasBefore = c.(Before)
		_, wr.hasAfter = c.(After)
		_, wr.hasMain = c.(Main)
	} else {
		if hf, ok := c.(BeforeHandler); ok {
			wr.beforeHandler = hf.Before
		}
		if hf, ok := c.(AfterHandler); ok {
			wr.afterHandler = hf.After
		}
	}
	//controller.Index
	a.wrappers[name] = wr
	return wr
}
391258
func HandlerErrorHandler(errorHandler transport.ErrorHandler) HandlerOption {
	return func(h *Handler) { h.errorHandler = errorHandler }
}
1296105
func WrapEntryPointHandler(ctx context.Context, tracer *tracing.Tracing, entryPointName string) alice.Constructor {
	return func(next http.Handler) (http.Handler, error) {
		return NewEntryPoint(ctx, tracer, entryPointName, next), nil
	}
}
269580
func WrapCall(w ...client.CallWrapper) Option {
	return func(o *Options) {
		o.Client.Init(client.WrapCall(w...))
	}
}
1154785
func (c *Config) registerHandler(h plugin.Handler) {
	if c.registry == nil {
		c.registry = make(map[string]plugin.Handler)
	}

	// Just overwrite...
	c.registry[h.Name()] = h
}
1066702
func defaultWrapHandler(path string, handler http.Handler, err error) (string, http.Handler, error) {
	return v1APIPath(path), handler, err
}
45367
func NetHandler(network, addr string, fmtr Format) (Handler, error) {
	conn, err := net.Dial(network, addr)
	if err != nil {
		return nil, err
	}

	return closingHandler{conn, StreamHandler(conn, fmtr)}, nil
}
415599
func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
	options := Options{
		Daemon: "localhost:2000",
	}

	for _, o := range opts {
		o(&options)
	}

	x := newXRay(options)

	return func(h server.HandlerFunc) server.HandlerFunc {
		return func(ctx context.Context, req server.Request, rsp interface{}) error {
			name := options.Name
			if len(name) == 0 {
				// default name
				name = req.Service() + "." + req.Endpoint()
			}

			var err error
			s := getSegment(name, ctx)

			defer func() {
				setCallStatus(s, req.Service(), req.Endpoint(), err)
				go record(x, s)
			}()

			ctx = newContext(ctx, s)
			err = h(ctx, req, rsp)
			return err
		}
	}
}