diff options
author | Alexander NeonXP Kiryukhin <i@neonxp.ru> | 2024-07-29 02:47:35 +0300 |
---|---|---|
committer | Alexander NeonXP Kiryukhin <i@neonxp.ru> | 2024-07-29 02:47:35 +0300 |
commit | 96e2ce2e9d363a6296f9411ecb00168520258874 (patch) | |
tree | 09aa7fffe10eab84ae0edd39e570355984ba0148 /middleware | |
parent | 12ed72e4e1da181a6c87319a50d3b4142788b4c0 (diff) |
Отказ от echo
Diffstat (limited to 'middleware')
-rw-r--r-- | middleware/session/store.go | 30 | ||||
-rw-r--r-- | middleware/user.go | 38 |
2 files changed, 15 insertions, 53 deletions
diff --git a/middleware/session/store.go b/middleware/session/store.go deleted file mode 100644 index f22c64d..0000000 --- a/middleware/session/store.go +++ /dev/null @@ -1,30 +0,0 @@ -package session - -import ( - "net/http" - - "github.com/gorilla/sessions" - "github.com/uptrace/bun" -) - -type SessionStore struct { - orm *bun.DB -} - -// Get should return a cached session. -func (s *SessionStore) Get(r *http.Request, name string) (*sessions.Session, error) { - panic("not implemented") // TODO: Implement -} - -// New should create and return a new session. -// -// Note that New should never return a nil session, even in the case of -// an error if using the Registry infrastructure to cache the session. -func (s *SessionStore) New(r *http.Request, name string) (*sessions.Session, error) { - panic("not implemented") // TODO: Implement -} - -// Save should persist session to the underlying store implementation. -func (s *SessionStore) Save(r *http.Request, w http.ResponseWriter, ss *sessions.Session) error { - panic("not implemented") // TODO: Implement -} diff --git a/middleware/user.go b/middleware/user.go index eaca6e9..f4eb12a 100644 --- a/middleware/user.go +++ b/middleware/user.go @@ -2,33 +2,25 @@ package middleware import ( "context" + "net/http" - "github.com/labstack/echo-contrib/session" - "github.com/labstack/echo/v4" "gitrepo.ru/neonxp/gorum/contextlib" - "gitrepo.ru/neonxp/gorum/models" + "go.neonxp.ru/mux" + "go.neonxp.ru/mux/middleware/session" ) -func UserMiddleware() echo.MiddlewareFunc { - return func(next echo.HandlerFunc) echo.HandlerFunc { - return func(c echo.Context) error { - sess, err := session.Get("session", c) - if err != nil { - return err +func UserMiddleware() mux.Middleware { + return func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + session := session.FromRequest(r) + user := (*session)["user"] + if user == nil { + h.ServeHTTP(w, r) + return } - u, okSess := sess.Values["user"] - if !okSess { - return next(c) - } - user, okUser := u.(models.User) - if !okUser { - return next(c) - } - ctx := context.WithValue(c.Request().Context(), contextlib.UserKey, user) - req := c.Request().WithContext(ctx) - c.SetRequest(req) - - return next(c) - } + h.ServeHTTP(w, r.WithContext( + context.WithValue(r.Context(), contextlib.UserKey, user), + )) + }) } } |