Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Spike into switching to standard http interface #100

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions svc/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package svc

import (
"context"
"net/http"
"strings"
"time"

"github.com/remind101/pkg/httpx"
"github.com/remind101/pkg/httpx/middleware"
"github.com/remind101/pkg/reporter"
)

type HTTPHandlerOpts struct {
Reporter reporter.Reporter
ForwardingHeaders []string
BasicAuth string
ErrorHandler middleware.ErrorHandlerFunc
HandlerTimeout time.Duration
}

func NewHTTPStack(h http.Handler, opts HTTPHandlerOpts) http.Handler {
var hx httpx.Handler

// Adapter for httpx middlewares. No middleware will rely on the error return value
// or context arguement.
hx = httpx.HandlerFunc(func(ctx context.Context, rw http.ResponseWriter, r *http.Request) error {
h.ServeHTTP(rw, r)
return nil
})

// Recover from panics. A panic is converted to an error. This should be first,
// even though it means panics in middleware will not be recovered, because
// later middleware expects endpoint panics to be returned as an error.
hx = middleware.BasicRecover(hx)

// Handler errors returned by endpoint handler or recovery middleware.
// Errors will no longer be returned after this middeware.
errorHandler := opts.ErrorHandler
if errorHandler == nil {
errorHandler = middleware.ReportingErrorHandler
}
hx = middleware.HandleError(hx, errorHandler)

// Insert logger into context and log requests at INFO level.
hx = middleware.LogTo(hx, middleware.LoggerWithRequestID)

// Add reporter to context and request to reporter context.
hx = middleware.WithReporter(hx, opts.Reporter)

// Add the request id to the context.
hx = middleware.ExtractRequestID(hx)

// Add basic auth
if opts.BasicAuth != "" {
user := strings.Split(opts.BasicAuth, ":")[0]
pass := strings.Split(opts.BasicAuth, ":")[1]
hx = middleware.BasicAuth(hx, user, pass, "")
}

// Adds forwarding headers from request to the context. This allows http clients
// to get those headers from the context and add them to upstream requests.
if len(opts.ForwardingHeaders) > 0 {
for _, header := range opts.ForwardingHeaders {
hx = middleware.ExtractHeader(hx, header)
}
}

// Wrap the route in middleware to add a context.Context. This middleware must be
// last as it acts as the adaptor between http.Handler and httpx.Handler.
return middleware.BackgroundContext(hx)
}
42 changes: 42 additions & 0 deletions svc/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package svc_test

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/gorilla/mux"
"github.com/remind101/pkg/httpx"
"github.com/remind101/pkg/reporter"
"github.com/remind101/pkg/svc"
)

func TestHTTPStack(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/boom", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusCreated)
fmt.Fprintln(rw, r.Header.Get("X-Request-ID"))
}).Methods("GET")

h := svc.NewHTTPStack(r, svc.HTTPHandlerOpts{
Reporter: reporter.NewLogReporter(),
ErrorHandler: httpx.Error,
})

req, _ := http.NewRequest("GET", "/boom", nil)
req.Header.Add("X-Request-ID", "abc")

rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)

if got, want := rw.Result().StatusCode, http.StatusCreated; got != want {
t.Errorf("got %v; expected %v", got, want)
}

body, _ := ioutil.ReadAll(rw.Body)
if got, want := string(body), "abc\n"; got != want {
t.Errorf("got %v; expected %v", got, want)
}
}