diff --git a/src/frontend/main.go b/src/frontend/main.go index dc24284..f2b6cab 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -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) { diff --git a/src/frontend/middleware.go b/src/frontend/middleware.go index b8957e2..a736dbe 100644 --- a/src/frontend/middleware.go +++ b/src/frontend/middleware.go @@ -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) } }