gophertype/pkg/middleware/session.go
Noah Petherbridge 898f82fb79 Modernize Backend Go App
* Remove Negroni in favor of the standard net/http server.
* Remove gorilla/mux in favor of the standard net/http NewServeMux.
* Remove gorilla/sessions in favor of Redis session_id cookie.
* Remove the hacky glue controllers setup in favor of regular defined routes
  in the router.go file directly.
* Update all Go dependencies for Go 1.24
* Move and centralize all the HTTP middlewares.
* Add middlewares for Logging and Recovery to replace Negroni's.
2025-04-03 22:45:34 -07:00

21 lines
433 B
Go

package middleware
import (
"context"
"net/http"
"git.kirsle.net/apps/gophertype/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))
})
}