nautilus/gateway

Forward All Headers from Gateway to Service

krisanalfa opened this issue · 1 comments

How do I achieve this? I wrote this middleware, turns out the middleware can't read gateway request headers.

forwardHeaderMiddleware := gateway.RequestMiddleware(func(r *http.Request) error {
	// Loop over header names
	for name, values := range r.Header {
		// Loop over all values for the name.
		for _, value := range values {
			r.Header.Set(name, value)
		}
	}

	return nil
})

Found the answer, the trick is using custom context value

forwardHeaderMiddleware := gateway.RequestMiddleware(func(r *http.Request) error {
	h := r.Context().Value(customContextKey).(http.Header)

	// Loop over header names
	for name, values := range r.Header {
		// Loop over all values for the name.
		for _, value := range values {
			r.Header.Set(name, value)
		}
	}

	return nil
})

http.HandleFunc("/graphql",
	func(w http.ResponseWriter, r *http.Request) {
		r = r.WithContext(context.WithValue(r.Context(), customContextKey, r.Header))

		gw.GraphQLHandler(w, r)
	})