frontend: proper ensureSess middleware, add req id

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2018-07-02 10:13:02 -07:00
parent 2dcd7b6221
commit 85d04fc0b5
2 changed files with 21 additions and 11 deletions

View file

@ -77,19 +77,20 @@ func main() {
mustConnGRPC(ctx, &svc.checkoutSvcConn, svc.checkoutSvcAddr)
r := mux.NewRouter()
r.HandleFunc("/", ensureSessionID(svc.homeHandler)).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/product/{id}", ensureSessionID(svc.productHandler)).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/cart", ensureSessionID(svc.viewCartHandler)).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/cart", ensureSessionID(svc.addToCartHandler)).Methods(http.MethodPost)
r.HandleFunc("/cart/empty", ensureSessionID(svc.emptyCartHandler)).Methods(http.MethodPost)
r.HandleFunc("/setCurrency", ensureSessionID(svc.setCurrencyHandler)).Methods(http.MethodPost)
r.HandleFunc("/", svc.homeHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/product/{id}", svc.productHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/cart", svc.viewCartHandler).Methods(http.MethodGet, http.MethodHead)
r.HandleFunc("/cart", svc.addToCartHandler).Methods(http.MethodPost)
r.HandleFunc("/cart/empty", svc.emptyCartHandler).Methods(http.MethodPost)
r.HandleFunc("/setCurrency", svc.setCurrencyHandler).Methods(http.MethodPost)
r.HandleFunc("/logout", svc.logoutHandler).Methods(http.MethodGet)
r.HandleFunc("/cart/checkout", ensureSessionID(svc.placeOrderHandler)).Methods(http.MethodPost)
r.HandleFunc("/cart/checkout", svc.placeOrderHandler).Methods(http.MethodPost)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
log.Infof("starting server on " + addr + ":" + srvPort)
loggedHandler := &logHandler{log: log, next: r}
log.Fatal(http.ListenAndServe(addr+":"+srvPort, loggedHandler))
log.Fatal(http.ListenAndServe(addr+":"+srvPort,
http.HandlerFunc(ensureSessionID(loggedHandler))))
}
func mustMapEnv(target *string, envKey string) {

View file

@ -10,6 +10,7 @@ import (
)
type ctxKeyLog struct{}
type ctxKeyRequestID struct{}
type logHandler struct {
log *logrus.Logger
@ -39,12 +40,20 @@ func (r *responseRecorder) WriteHeader(statusCode int) {
}
func (lh *logHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
requestID, _ := uuid.NewRandom()
ctx = context.WithValue(ctx, ctxKeyRequestID{}, requestID.String())
start := time.Now()
rr := &responseRecorder{w: w}
log := lh.log.WithFields(logrus.Fields{
"http.req.path": r.URL.Path,
"http.req.method": r.Method,
"http.req.id": requestID.String(),
})
if v, ok := r.Context().Value(ctxKeySessionID{}).(string); ok {
log = log.WithField("session", v)
}
log.Debug("request started")
defer func() {
log.WithFields(logrus.Fields{
@ -53,12 +62,12 @@ func (lh *logHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
"http.resp.bytes": rr.b}).Debugf("request complete")
}()
ctx := context.WithValue(r.Context(), ctxKeyLog{}, log)
ctx = context.WithValue(ctx, ctxKeyLog{}, log)
r = r.WithContext(ctx)
lh.next.ServeHTTP(rr, r)
}
func ensureSessionID(next http.HandlerFunc) http.HandlerFunc {
func ensureSessionID(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var sessionID string
c, err := r.Cookie(cookieSessionID)
@ -77,6 +86,6 @@ func ensureSessionID(next http.HandlerFunc) http.HandlerFunc {
}
ctx := context.WithValue(r.Context(), ctxKeySessionID{}, sessionID)
r = r.WithContext(ctx)
next(w, r)
next.ServeHTTP(w, r)
}
}