// Mux is a middleware multiplexer for easy plugin composition.
335320
func (r *Request) Mux() *mux.Mux {
	mx := mux.New()
	r.Use(mx)
	return mx
}
1197925
func (m *Mux) Handler(method, path string, handler HandlerFunc) Handler {
	return Handler{
		Method: method,
		Path:   path,
		Func:   handler,
	}
}
1323092
func Match(matchers ...Matcher) *Mux {
	mx := New()
	mx.AddMatcher(matchers...)
	return mx
}
901951
func ServeMux(mux *http.ServeMux, address, port, cert, key string) error {
	listen := fmt.Sprintf("%s:%s", address, port)
	if cert == "false" || key == "false" {
		fmt.Printf("About to listen on http://%s/\n", listen)
		err := http.ListenAndServe(listen, mux)
		if err != nil {
			return err
		}
	} else {
		fmt.Printf("About to listen on https://%s/\n", listen)
		err := http.ListenAndServeTLS(listen, cert, key, mux)
		if err != nil {
			return err
		}
	}
	return nil
}
671516
func AddHTTPMux(path string, callback interface{}) {
	ServerEngineInit.HTTPMuxList = append(ServerEngineInit.HTTPMuxList, ServerMux{PathPrefix: path, Callback: callback})
}
966036
func Error() *Mux {
	return Match(func(ctx *c.Context) bool {
		return (ctx.GetString("$phase") == "error" && ctx.Error != nil) ||
			(ctx.GetString("$phase") == "response" && ctx.Response.StatusCode >= 500)
	})
}
1111722
func SimpleMuxer(fx func(Publisher, interface{})) Handler {
	return func(r Publisher, err error, data interface{}) {
		if err != nil {
			r.ReplyError(err)
			return
		}

		fx(r, data)
	}
}
58520
func (m *Mux) Close() error {
	var err error

	m.listener.closing = true

	m.idle.RLock()

	for conn := range m.idle.conns {
		conn.Close()
	}

	m.idle.RUnlock()
	m.active.RLock()

	if m.Timeout > 0 && len(m.active.conns) > 0 {
		timer := time.NewTimer(m.Timeout)
		<-timer.C
		err = m.listener.Close()
	} else {
		err = m.listener.Close()
	}
	m.active.RUnlock()

	m.Closed(m)

	return err
}
495852
func (m *Mux) AddMatcher(matchers ...Matcher) *Mux {
	m.Matchers = append(m.Matchers, matchers...)
	return m
}
978261
func (mux *TypeMux) Handler(r *Request) Handler {
	return mux.mux.Handler(r)
}
12072
func (mux *ServeMux) Serve(rw ResponseWriter, r *Request) {
	h := mux.Handler(r)
	h.Serve(rw, r)
}
1365051
func New(cfg Config) (*Mux, error) {
	if err := cfg.CheckAndSetDefaults(); err != nil {
		return nil, trace.Wrap(err)
	}

	ctx, cancel := context.WithCancel(cfg.Context)
	waitContext, waitCancel := context.WithCancel(context.TODO())
	return &Mux{
		Entry: log.WithFields(log.Fields{
			trace.Component: teleport.Component("mx", cfg.ID),
		}),
		Config:      cfg,
		context:     ctx,
		cancel:      cancel,
		sshListener: newListener(ctx, cfg.Listener.Addr()),
		tlsListener: newListener(ctx, cfg.Listener.Addr()),
		waitContext: waitContext,
		waitCancel:  waitCancel,
	}, nil
}
548971
func (m *Mux) Prefix(p string) *Mux {
	m.prefix = strings.TrimSuffix(p, "/")
	return m
}
705352
func (m *Mux) HandlerFunc(h http.Handler) http.HandlerFunc {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		h.ServeHTTP(w, r)
	})
}
388515
func (m *Mux) Use(p plugin.Plugin) *Mux {
	m.Middleware.Use(p)
	return m
}
400671
func (m *Medeina) OnMux(path string, handle http.Handler) {
	var path_slash string
	if path[0] != '/' {
		path_slash = "/" + path
	} else {
		path_slash = path
	}
	m.OnHandler(path, HandlerPathPrefix(path_slash, handle))
}
1325191
func NewMux() *Mux {
	return &Mux{
		mux: http.NewServeMux(),
		log: func(err error) { println(err.Error()) },
	}
}