Пример #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))
}
	"net/http"
	"net/http/httptest"
	"strings"

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

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

var _ = Describe("HealthCheckHandler", func() {
	It("returns a 200 if the state is good", func() {
		request, err := http.NewRequest("GET", "/health_check", strings.NewReader(""))
		Expect(err).NotTo(HaveOccurred())

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

		Expect(recorder.Code).To(Equal(http.StatusOK))
	})

	It("returns a 405 when an unknown method has been received", func() {
		request, err := http.NewRequest("DELETE", "/health_check", strings.NewReader(""))
		Expect(err).NotTo(HaveOccurred())

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

		Expect(recorder.Code).To(Equal(http.StatusMethodNotAllowed))