Ejemplo n.º 1
0
func main() {
	port, consulURL, pathToCheckARecord := parseCommandLineFlags()
	proxyURL, err := url.Parse(consulURL)
	if err != nil {
		log.Fatal(err)
	}

	if pathToCheckARecord == "" {
		log.Fatal("--path-to-check-a-record is required")
	}

	mux := http.NewServeMux()
	logBuffer := bytes.NewBuffer([]byte{})
	healthCheckHandler := handlers.NewHealthCheckHandler()
	dnsHandler := handlers.NewDNSHandler(pathToCheckARecord)

	proxy := httputil.NewSingleHostReverseProxy(proxyURL)
	director := proxy.Director
	proxy.Director = func(request *http.Request) {
		director(request)
		request.URL.Path = strings.TrimPrefix(request.URL.Path, "/consul")
		request.Host = request.URL.Host
	}
	proxy.ErrorLog = log.New(logBuffer, "", 0)

	mux.HandleFunc("/consul/", func(w http.ResponseWriter, req *http.Request) {
		bufferedRW := buffered.NewResponseWriter(w, logBuffer)
		proxy.ServeHTTP(bufferedRW, req)
		bufferedRW.Copy()
	})

	mux.HandleFunc("/health_check", func(w http.ResponseWriter, req *http.Request) {
		healthCheckHandler.ServeHTTP(w, req)
	})

	mux.HandleFunc("/dns", func(w http.ResponseWriter, req *http.Request) {
		dnsHandler.ServeHTTP(w, req)
	})

	log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%s", port), mux))
}
			var err error

			args := []string{
				"-ldflags",
				"-X main.Addresses=127.0.0.2,127.0.0.3,127.0.0.4 -X main.ServiceName=some-service",
			}

			pathToCheckARecord, err = gexec.Build("github.com/cloudfoundry-incubator/consul-release/src/acceptance-tests/testing/testconsumer/fakes/checkarecord", args...)
			Expect(err).NotTo(HaveOccurred())
		})

		It("returns an array of ip addresses", func() {
			request, err := http.NewRequest("GET", "/dns?service=some-service", nil)
			Expect(err).NotTo(HaveOccurred())

			handler := handlers.NewDNSHandler(pathToCheckARecord)
			recorder := httptest.NewRecorder()
			handler.ServeHTTP(recorder, request)

			Expect(recorder.Code).To(Equal(http.StatusOK))
			Expect(string(recorder.Body.Bytes())).To(Equal(`["127.0.0.2","127.0.0.3","127.0.0.4"]`))
		})
	})

	Context("when no ip addresses are bound to the service", func() {
		var pathToCheckARecord string

		BeforeEach(func() {
			var err error

			args := []string{