website/pkg/middleware/session.go

21 lines
433 B
Go
Raw Normal View History

package middleware
import (
"context"
"net/http"
2022-08-26 04:21:46 +00:00
"code.nonshy.com/nonshy/website/pkg/session"
)
// Session middleware.
func Session(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check for the session_id cookie.
sess := session.LoadOrNew(r)
ctx := context.WithValue(r.Context(), session.ContextKey, sess)
handler.ServeHTTP(w, r.WithContext(ctx))
})
}