route/internal/middleware/twirp.go

29 lines
637 B
Go

package middleware
import (
"context"
"net/http"
"github.com/Xe/ln"
"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()
if ctx == nil {
panic("context is nil")
}
ctx, err := twirp.WithHTTPRequestHeaders(ctx, r.Header)
if err != nil {
ln.Error(context.Background(), err, ln.Action("can't get request headers"))
http.Error(w, err.Error(), http.StatusInternalServerError)
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}