M server/server.go => server/server.go +2 -0
@@ 34,6 34,7 @@ import (
"hg.code.netlandish.com/~netlandish/gobwebs/config"
"hg.code.netlandish.com/~netlandish/gobwebs/email"
"hg.code.netlandish.com/~netlandish/gobwebs/internal/localizer"
+ "hg.code.netlandish.com/~netlandish/gobwebs/sessions"
"hg.code.netlandish.com/~netlandish/gobwebs/storage"
"hg.code.netlandish.com/~netlandish/gobwebs/validate"
"petersanchez.com/carrier"
@@ 537,6 538,7 @@ func (s *Server) DefaultMiddlewareWithConfig(conf *MiddlewareConfig) *Server {
if conf.Sessions {
s.e.Use(session.LoadAndSave(s.Session)) // Must be first
+ s.e.Use(sessions.Middleware(s.Session))
}
if conf.ServerContext {
s.e.Use(Middleware(s))
A sessions/middleware.go => sessions/middleware.go +40 -0
@@ 0,0 1,40 @@
+package sessions
+
+import (
+ "context"
+ "errors"
+
+ "github.com/alexedwards/scs/v2"
+ "github.com/labstack/echo/v4"
+)
+
+var sessionCtxKey = &contextKey{"session"}
+
+type contextKey struct {
+ name string
+}
+
+// Context adds session manager object to context for immediate use
+func Context(ctx context.Context, session *scs.SessionManager) context.Context {
+ return context.WithValue(ctx, sessionCtxKey, session)
+}
+
+// ForContext pulls session manager value for context
+func ForContext(ctx context.Context) *scs.SessionManager {
+ session, ok := ctx.Value(sessionCtxKey).(*scs.SessionManager)
+ if !ok {
+ panic(errors.New("invalid session context"))
+ }
+ return session
+}
+
+// Middleware will add the provided *scs.SessionManager instance to the context
+func Middleware(session *scs.SessionManager) echo.MiddlewareFunc {
+ return func(next echo.HandlerFunc) echo.HandlerFunc {
+ return func(c echo.Context) error {
+ ctx := Context(c.Request().Context(), session)
+ c.SetRequest(c.Request().WithContext(ctx))
+ return next(c)
+ }
+ }
+}
A sessions/sessions.go => sessions/sessions.go +4 -0
@@ 0,0 1,4 @@
+// Package sessions implements a gobwebs Session interface
+//
+// This is a TODO still. For now it's a holder for session related middleware
+package sessions