added apigee client id to middleware context
This commit is contained in:
parent
3e19a763b4
commit
a72c0c729a
3 changed files with 17 additions and 3 deletions
|
@ -24,12 +24,11 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
pb "github.com/GoogleCloudPlatform/microservices-demo/src/frontend/genproto"
|
||||||
|
"github.com/GoogleCloudPlatform/microservices-demo/src/frontend/money"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
pb "github.com/GoogleCloudPlatform/microservices-demo/src/frontend/genproto"
|
|
||||||
"github.com/GoogleCloudPlatform/microservices-demo/src/frontend/money"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -353,6 +352,8 @@ func (fe *frontendServer) setCurrencyHandler(w http.ResponseWriter, r *http.Requ
|
||||||
// TODO(phriscage) update for multiple config values
|
// TODO(phriscage) update for multiple config values
|
||||||
func (fe *frontendServer) viewConfigHandler(w http.ResponseWriter, r *http.Request) {
|
func (fe *frontendServer) viewConfigHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log := r.Context().Value(ctxKeyLog{}).(logrus.FieldLogger)
|
log := r.Context().Value(ctxKeyLog{}).(logrus.FieldLogger)
|
||||||
|
log.Debug("Viewing the configuration")
|
||||||
|
|
||||||
currencies, err := fe.getCurrencies(r.Context())
|
currencies, err := fe.getCurrencies(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
renderHTTPError(log, r, w, errors.Wrap(err, "could not retrieve currencies"), http.StatusInternalServerError)
|
renderHTTPError(log, r, w, errors.Wrap(err, "could not retrieve currencies"), http.StatusInternalServerError)
|
||||||
|
|
|
@ -138,6 +138,7 @@ func main() {
|
||||||
var handler http.Handler = r
|
var handler http.Handler = r
|
||||||
handler = &logHandler{log: log, next: handler} // add logging
|
handler = &logHandler{log: log, next: handler} // add logging
|
||||||
handler = ensureSessionID(handler) // add session ID
|
handler = ensureSessionID(handler) // add session ID
|
||||||
|
handler = ensureApigeeClientID(handler) // add apigee client ID
|
||||||
handler = &ochttp.Handler{ // add opencensus instrumentation
|
handler = &ochttp.Handler{ // add opencensus instrumentation
|
||||||
Handler: handler,
|
Handler: handler,
|
||||||
Propagation: &b3.HTTPFormat{}}
|
Propagation: &b3.HTTPFormat{}}
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ctxKeyLog struct{}
|
type ctxKeyLog struct{}
|
||||||
|
@ -103,3 +104,14 @@ func ensureSessionID(next http.Handler) http.HandlerFunc {
|
||||||
next.ServeHTTP(w, r)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue