Пример #1
0
func Ambassador(ctx *cli.Context) {
	var (
		addr = ctx.String("addr")

		proxycfg = ctx.String("proxycfg")

		localproxycfg = proxy.ConfigKey()

		proxyTargets = ctx.StringSlice("proxy")

		stop = make(chan struct{}, 1)
	)

	// setup register path for discovery
	disc.RegisterPath = path.Join(ctx.String("cluster"), proxy.DiscoveryPath)

	// setup Discovery URI from config
	proxy.DiscoveryURI()

	if err := dcli.Before(ctx); err != nil { // We don't want to setup discovery
		if err == dcli.ErrRequireDiscovery {
			log.WithFields(log.Fields{"err": err}).Warning("discovery feature disabled")
		} else {
			log.WithFields(log.Fields{"err": err}).Fatal("halt")
		}
	} else { // We had successfully setup discovery
		if localproxycfg != "" {
			proxy.ProxyConfigKey = localproxycfg
		} else if proxycfg != "" {
			proxy.ProxyConfigKey = proxycfg
		}
		if proxyTargets != nil {
			proxy.Targets = proxyTargets
		}
		proxy.EnableDiscoveryProxy = ctx.Bool("proxy2discovery")
		proxy.Follow()
	}

	if addr != "" {
		log.WithFields(log.Fields{"addr": addr}).Info("API endpoint begin")
		web.RunAPIEndpoint(addr, stop)
	} else {
		log.Warning("API endpoint disabled")
	}

	<-stop // we should never reach here

	// TODO: we should gracefully shutdown proxied connections
}
Пример #2
0
func Follow(w http.ResponseWriter, r *http.Request) {
	if err := common("PUT", r); err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	var (
		discovery = r.Form.Get("discovery")
		cluster   = r.Form.Get("cluster")

		proxycfg = r.Form.Get("key")

		heartbeat time.Duration
		ttl       time.Duration
	)

	if proxycfg == "" {
		http.Error(w, "Bad Request", 400)
		return
	} else {
		proxy.ProxyConfigKey = proxycfg
	}

	if hbStr := r.Form.Get("hb"); hbStr == "" {
		heartbeat = disc.DefaultHeartbeat
	} else {
		if hb, err := time.ParseDuration(hbStr); err != nil {
			heartbeat = disc.DefaultHeartbeat
		} else {
			heartbeat = hb
		}
	}

	if ttlStr := r.Form.Get("ttl"); ttlStr == "" {
		ttl = disc.DefaultTTL
	} else {
		if t, err := time.ParseDuration(ttlStr); err != nil {
			ttl = disc.DefaultTTL
		} else {
			ttl = t
		}
	}

	if proxy.ConfigReset != nil {
		proxy.ConfigReset() // abort config
	}
	if disc.Cancel != nil {
		disc.Cancel() // abort previous session
	}

	// resume advertising node, if we were advertising
	if discovery != "null" && discovery != "" {
		disc.Discovery = discovery
	}
	if cluster != "null" && cluster != "" {
		disc.RegisterPath = path.Join(cluster, proxy.DiscoveryPath)
	}
	if disc.Discovery != "" {
		disc.Register(heartbeat, ttl)
		proxy.Follow()
	}

	w.Write([]byte("done"))
}