added apigee client id to middleware context

This commit is contained in:
phriscage 2018-12-03 11:29:04 -05:00
parent 3e19a763b4
commit a72c0c729a
3 changed files with 17 additions and 3 deletions

View file

@ -24,12 +24,11 @@ import (
"strconv"
"time"
pb "github.com/GoogleCloudPlatform/microservices-demo/src/frontend/genproto"
"github.com/GoogleCloudPlatform/microservices-demo/src/frontend/money"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
pb "github.com/GoogleCloudPlatform/microservices-demo/src/frontend/genproto"
"github.com/GoogleCloudPlatform/microservices-demo/src/frontend/money"
)
var (
@ -353,6 +352,8 @@ func (fe *frontendServer) setCurrencyHandler(w http.ResponseWriter, r *http.Requ
// TODO(phriscage) update for multiple config values
func (fe *frontendServer) viewConfigHandler(w http.ResponseWriter, r *http.Request) {
log := r.Context().Value(ctxKeyLog{}).(logrus.FieldLogger)
log.Debug("Viewing the configuration")
currencies, err := fe.getCurrencies(r.Context())
if err != nil {
renderHTTPError(log, r, w, errors.Wrap(err, "could not retrieve currencies"), http.StatusInternalServerError)

View file

@ -138,6 +138,7 @@ func main() {
var handler http.Handler = r
handler = &logHandler{log: log, next: handler} // add logging
handler = ensureSessionID(handler) // add session ID
handler = ensureApigeeClientID(handler) // add apigee client ID
handler = &ochttp.Handler{ // add opencensus instrumentation
Handler: handler,
Propagation: &b3.HTTPFormat{}}

View file

@ -21,6 +21,7 @@ import (
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/metadata"
)
type ctxKeyLog struct{}
@ -103,3 +104,14 @@ func ensureSessionID(next http.Handler) http.HandlerFunc {
next.ServeHTTP(w, r)
}
}
// update context metadata with appropriate Apigee Authorization header
func ensureApigeeClientID(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var apigeeClientID string
apigeeClientID = currentApigeeClientID(r)
ctx := metadata.AppendToOutgoingContext(r.Context(), apigeeClientIDHeaderName, apigeeClientID)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
}
}