Skip to content

Commit

Permalink
Removed echo-contrib dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikestefanello committed Jun 15, 2024
1 parent 5531e0b commit 71f7de8
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 18 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ Here's a simple example of loading data from a session and saving new values:

```go
func SomeFunction(ctx echo.Context) error {
sess, err := session.Get("some-session-key", ctx)
sess, err := session.Get(ctx, "some-session-key")
if err != nil {
return err
}
Expand Down Expand Up @@ -1256,7 +1256,6 @@ Thank you to all of the following amazing projects for making this possible.
- [bulma](https://github.com/jgthms/bulma)
- [docker](https://www.docker.com/)
- [echo](https://github.com/labstack/echo)
- [echo-contrib](https://github.com/labstack/echo-contrib)
- [ent](https://github.com/ent/ent)
- [go](https://go.dev/)
- [gocache](https://github.com/eko/gocache)
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ require (
github.com/eko/gocache/store/redis/v4 v4.2.1
github.com/go-playground/validator/v10 v10.19.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/gorilla/context v1.1.2
github.com/gorilla/sessions v1.2.2
github.com/hibiken/asynq v0.24.1
github.com/jackc/pgx/v4 v4.18.3
github.com/labstack/echo-contrib v0.17.1
github.com/labstack/echo/v4 v4.12.0
github.com/labstack/gommon v0.4.2
github.com/redis/go-redis/v9 v9.5.1
Expand Down Expand Up @@ -44,7 +44,6 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/context v1.1.2 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl/v2 v2.20.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo-contrib v0.17.1 h1:7I/he7ylVKsDUieaGRZ9XxxTYOjfQwVzHzUYrNykfCU=
github.com/labstack/echo-contrib v0.17.1/go.mod h1:SnsCZtwHBAZm5uBSAtQtXQHI3wqEA73hvTn0bYMKnZA=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
Expand Down
3 changes: 3 additions & 0 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const (

// LoggerKey is the key value used to store a structured logger in context
LoggerKey = "logger"

// SessionKey is the key value used to store the session data in context
SessionKey = "session"
)

// IsCanceledError determines if an error is due to a context cancelation
Expand Down
3 changes: 1 addition & 2 deletions pkg/handlers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
echomw "github.com/labstack/echo/v4/middleware"
"github.com/mikestefanello/pagoda/config"
"github.com/mikestefanello/pagoda/pkg/middleware"
Expand Down Expand Up @@ -40,7 +39,7 @@ func BuildRouter(c *services.Container) error {
echomw.TimeoutWithConfig(echomw.TimeoutConfig{
Timeout: c.Config.App.Timeout,
}),
session.Middleware(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))),
middleware.Session(sessions.NewCookieStore([]byte(c.Config.App.EncryptionKey))),
middleware.LoadAuthenticatedUser(c.Auth),
middleware.ServeCachedPage(c.TemplateRenderer),
echomw.CSRFWithConfig(echomw.CSRFConfig{
Expand Down
19 changes: 19 additions & 0 deletions pkg/middleware/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package middleware

import (
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/session"
)

// Session sets the session storage in the request context
func Session(store sessions.Store) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
defer context.Clear(ctx.Request())
session.Store(ctx, store)
return next(ctx)
}
}
}
24 changes: 24 additions & 0 deletions pkg/middleware/session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package middleware

import (
"testing"

"github.com/gorilla/sessions"
"github.com/mikestefanello/pagoda/pkg/session"
"github.com/mikestefanello/pagoda/pkg/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSession(t *testing.T) {
ctx, _ := tests.NewContext(c.Web, "/")
_, err := session.Get(ctx, "test")
assert.Equal(t, session.ErrStoreNotFound, err)

store := sessions.NewCookieStore([]byte("secret"))
err = tests.ExecuteMiddleware(ctx, Session(store))
require.NoError(t, err)

_, err = session.Get(ctx, "test")
assert.NotEqual(t, session.ErrStoreNotFound, err)
}
4 changes: 2 additions & 2 deletions pkg/msg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package msg

import (
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/log"
"github.com/mikestefanello/pagoda/pkg/session"
)

// Type is a message type
Expand Down Expand Up @@ -78,7 +78,7 @@ func Get(ctx echo.Context, typ Type) []string {

// getSession gets the flash message session
func getSession(ctx echo.Context) (*sessions.Session, error) {
sess, err := session.Get(sessionName, ctx)
sess, err := session.Get(ctx, sessionName)
if err != nil {
log.Ctx(ctx).Error("cannot load flash message session",
"error", err,
Expand Down
8 changes: 4 additions & 4 deletions pkg/services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/mikestefanello/pagoda/ent/passwordtoken"
"github.com/mikestefanello/pagoda/ent/user"
"github.com/mikestefanello/pagoda/pkg/context"
"github.com/mikestefanello/pagoda/pkg/session"

"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"golang.org/x/crypto/bcrypt"
)
Expand Down Expand Up @@ -62,7 +62,7 @@ func NewAuthClient(cfg *config.Config, orm *ent.Client) *AuthClient {

// Login logs in a user of a given ID
func (c *AuthClient) Login(ctx echo.Context, userID int) error {
sess, err := session.Get(authSessionName, ctx)
sess, err := session.Get(ctx, authSessionName)
if err != nil {
return err
}
Expand All @@ -73,7 +73,7 @@ func (c *AuthClient) Login(ctx echo.Context, userID int) error {

// Logout logs the requesting user out
func (c *AuthClient) Logout(ctx echo.Context) error {
sess, err := session.Get(authSessionName, ctx)
sess, err := session.Get(ctx, authSessionName)
if err != nil {
return err
}
Expand All @@ -83,7 +83,7 @@ func (c *AuthClient) Logout(ctx echo.Context) error {

// GetAuthenticatedUserID returns the authenticated user's ID, if the user is logged in
func (c *AuthClient) GetAuthenticatedUserID(ctx echo.Context) (int, error) {
sess, err := session.Get(authSessionName, ctx)
sess, err := session.Get(ctx, authSessionName)
if err != nil {
return 0, err
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package session

import (
"errors"

"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/pkg/context"
)

// ErrStoreNotFound indicates that the session store was not present in the context
var ErrStoreNotFound = errors.New("session store not found")

// Get returns a session
func Get(ctx echo.Context, name string) (*sessions.Session, error) {
s := ctx.Get(context.SessionKey)
if s == nil {
return nil, ErrStoreNotFound
}
store := s.(sessions.Store)
return store.Get(ctx.Request(), name)
}

// Store sets the session storage in the context
func Store(ctx echo.Context, store sessions.Store) {
ctx.Set(context.SessionKey, store)
}
23 changes: 23 additions & 0 deletions pkg/session/session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package session

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

func TestGetStore(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
ctx := e.NewContext(req, httptest.NewRecorder())
_, err := Get(ctx, "test")
assert.Equal(t, ErrStoreNotFound, err)

Store(ctx, sessions.NewCookieStore([]byte("secret")))
_, err = Get(ctx, "test")
assert.NoError(t, err)
}
5 changes: 2 additions & 3 deletions pkg/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"time"

"github.com/mikestefanello/pagoda/ent"
"github.com/mikestefanello/pagoda/pkg/session"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
)

Expand All @@ -29,8 +29,7 @@ func NewContext(e *echo.Echo, url string) (echo.Context, *httptest.ResponseRecor

// InitSession initializes a session for a given Echo context
func InitSession(ctx echo.Context) {
mw := session.Middleware(sessions.NewCookieStore([]byte("secret")))
_ = ExecuteMiddleware(ctx, mw)
session.Store(ctx, sessions.NewCookieStore([]byte("secret")))
}

// ExecuteMiddleware executes a middleware function on a given Echo context
Expand Down
2 changes: 1 addition & 1 deletion templates/layouts/auth.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
</div>
</section>

{{template "footer"}}
{{template "footer" .}}
</body>
</html>

0 comments on commit 71f7de8

Please sign in to comment.