Skip to content
Snippets Groups Projects
Unverified Commit 95fdb991 authored by Will McCutchen's avatar Will McCutchen Committed by GitHub
Browse files

Make httpbin.HTTPBin implement http.Handler interface (#100)

This is a very small improvement to developer ergonomics.
parent 880bfa1e
No related branches found
No related tags found
No related merge requests found
......@@ -62,7 +62,7 @@ import (
func TestSlowResponse(t *testing.T) {
app := httpbin.New()
testServer := httptest.NewServer(app.Handler())
testServer := httptest.NewServer(app)
defer testServer.Close()
client := http.Client{
......
......@@ -12,7 +12,7 @@ import (
func TestSlowResponse(t *testing.T) {
app := httpbin.New()
testServer := httptest.NewServer(app.Handler())
testServer := httptest.NewServer(app)
defer testServer.Close()
client := http.Client{
......
......@@ -13,12 +13,12 @@ import (
func main() {
statsdClient, _ := statsd.New("")
h := httpbin.New(
app := httpbin.New(
httpbin.WithObserver(datadogObserver(statsdClient)),
)
listenAddr := "0.0.0.0:8080"
http.ListenAndServe(listenAddr, h.Handler())
http.ListenAndServe(listenAddr, app)
}
func datadogObserver(client statsd.ClientInterface) httpbin.Observer {
......
This diff is collapsed.
......@@ -106,6 +106,9 @@ type HTTPBin struct {
// The hostname to expose via /hostname.
hostname string
// The app's http handler
handler http.Handler
}
// DefaultParams defines default parameter values
......@@ -236,9 +239,18 @@ func New(opts ...OptionFunc) *HTTPBin {
for _, opt := range opts {
opt(h)
}
h.handler = h.Handler()
return h
}
// ServeHTTP implememnts the http.Handler interface.
func (h *HTTPBin) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.handler.ServeHTTP(w, r)
}
// Assert that HTTPBin implements http.Handler interface
var _ http.Handler = &HTTPBin{}
// OptionFunc uses the "functional options" pattern to customize an HTTPBin
// instance
type OptionFunc func(*HTTPBin)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment