reprogl
25 строк · 518.0 Байт
1package middlewares2
3import (4"context"5"log"6"net/http"7
8"xelbot.com/reprogl/session"9)
10
11func Session(next http.Handler, logger *log.Logger) http.Handler {12return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {13sessionData := session.FromRequest(r, logger)14
15ctx := r.Context()16ctx = context.WithValue(ctx, session.CtxKey, sessionData)17
18sw := &session.ResponseWriter{ResponseWriter: w}19sw.SetSessionData(sessionData)20
21next.ServeHTTP(sw, r.WithContext(ctx))22
23sw.CheckAndWrite()24})25}
26