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))
}
package buffered_test

import (
	"bytes"
	"net/http"
	"net/http/httptest"
	"strconv"

	"github.com/cloudfoundry-incubator/consul-release/src/acceptance-tests/testing/testconsumer/buffered"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ http.ResponseWriter = buffered.NewResponseWriter(nil, nil)

var _ = Describe("ResponseBuffer", func() {
	Describe("Copy", func() {
		var (
			bufferedWriter *buffered.ResponseWriter
			actualWriter   *httptest.ResponseRecorder
			logBuffer      *bytes.Buffer
		)

		BeforeEach(func() {
			logBuffer = bytes.NewBuffer([]byte{})
			actualWriter = httptest.NewRecorder()
			bufferedWriter = buffered.NewResponseWriter(actualWriter, logBuffer)
		})

		It("copies all the headers to the real response writer", func() {