Beispiel #1
0
func scopedHandler(scope string, proxy http.Handler) (http.Handler, error) {
	if scope == "" {
		return proxy, nil
	}
	mux := route.NewMux()
	mux.NotFound = &DefaultNotFound{}
	if err := mux.Handle(scope, proxy); err != nil {
		return nil, err
	}
	return mux, nil
}
Beispiel #2
0
func New(id int, st stapler.Stapler, o Options) (*mux, error) {
	o = setDefaults(o)
	m := &mux{
		id:  id,
		wg:  &sync.WaitGroup{},
		mtx: &sync.RWMutex{},

		options: o,

		router:      route.NewMux(),
		connTracker: newConnTracker(),

		servers:   make(map[engine.ListenerKey]*srv),
		backends:  make(map[engine.BackendKey]*backend),
		frontends: make(map[engine.FrontendKey]*frontend),
		hosts:     make(map[engine.HostKey]engine.Host),

		stapleUpdatesC: make(chan *stapler.StapleUpdated),
		stopC:          make(chan struct{}),
		stapler:        st,
	}

	m.router.NotFound = &DefaultNotFound{}
	if o.NotFoundMiddleware != nil {
		if handler, err := o.NotFoundMiddleware.NewHandler(m.router.NotFound); err == nil {
			m.router.NotFound = handler
		}
	}

	if m.options.DefaultListener != nil {
		if err := m.upsertListener(*m.options.DefaultListener); err != nil {
			return nil, err
		}
	}

	// Subscribe to staple responses and kick staple updates
	m.stapler.Subscribe(m.stapleUpdatesC, m.stopC)

	m.wg.Add(1)
	go func() {
		defer m.wg.Done()
		for {
			select {
			case <-m.stopC:
				log.Infof("%v stop listening for staple updates", m)
				return
			case e := <-m.stapleUpdatesC:
				m.processStapleUpdate(e)
			}
		}
	}()

	m.wg.Add(1)
	go func() {
		defer m.wg.Done()
		for {
			select {
			case <-m.stopC:
				log.Infof("%v stop emitting metrics", m)
				return
			case <-time.After(time.Second):
				m.emitMetrics()
			}
		}
	}()
	return m, nil
}