cmd/routed: add twirp middleware

This commit is contained in:
Cadey Ratio 2018-01-21 08:53:43 -08:00
parent dd53921e77
commit 2f92dfbe46
2 changed files with 18 additions and 1 deletions

View File

@ -160,7 +160,7 @@ func New(cfg Config) (*Server, error) {
hs := &http.Server{ hs := &http.Server{
TLSConfig: tc, TLSConfig: tc,
Addr: cfg.GRPCAddr, Addr: cfg.GRPCAddr,
Handler: middleware.Trace("twirp-https")(mux), Handler: middleware.Twirp(middleware.Trace("twirp-https")(mux)),
} }
go hs.ListenAndServeTLS("", "") go hs.ListenAndServeTLS("", "")

View File

@ -0,0 +1,17 @@
package middleware
import (
"net/http"
"github.com/twitchtv/twirp"
)
// Twirp adds the needed values to the request context for twirp services.
func Twirp(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx, _ = twirp.WithHTTPRequestHeaders(ctx, r.Header)
next.ServeHTTP(w, r.WithContext(ctx))
})
}