Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
middleware.SessionSaver is now built-in.
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Sep 12, 2017
1 parent 30aa89f commit 3c8d9cb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func New(opts Options) *App {
}
a.Use(a.PanicHandler)
a.Use(RequestLogger)
a.Use(sessionSaver)

return a
}
Expand Down
6 changes: 0 additions & 6 deletions generators/newapp/templates/actions/app.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ func App() *buffalo.App {
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
}))

{{ if .asWeb -}}
// Automatically save the session if the underlying
// Handler does not return an error.
app.Use(middleware.SessionSaver)
{{ end -}}

{{ if .asAPI -}}
// Set the request content type to JSON
app.Use(middleware.SetContentType("application/json"))
Expand Down
6 changes: 1 addition & 5 deletions middleware/csrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package middleware

import (
"fmt"
"runtime"

"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/middleware/csrf"
Expand All @@ -25,9 +24,6 @@ var (
// CSRF is deprecated, and will be removed in v0.10.0. Use csrf.New instead.
var CSRF = func(next buffalo.Handler) buffalo.Handler {
warningMsg := "middleware.CSRF is deprecated, and will be removed in v0.10.0. Use csrf.New instead."
_, file, no, ok := runtime.Caller(1)
if ok {
warningMsg = fmt.Sprintf("%s Called from %s:%d", warningMsg, file, no)
}
fmt.Println(warningMsg)
return csrf.Middleware(next)
}
14 changes: 6 additions & 8 deletions middleware/session_saver.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package middleware

import (
"fmt"

"github.com/gobuffalo/buffalo"
"github.com/pkg/errors"
)

// SessionSaver will automatically save a session if the
// request was successful.
// SessionSaver is deprecated, and will be removed in v0.10.0. This now happens automatically. This middleware is no longer required.
func SessionSaver(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
err := next(c)
if err != nil {
return errors.WithStack(err)
}
return c.Session().Save()
warningMsg := "SessionSaver is deprecated, and will be removed in v0.10.0. This now happens automatically. This middleware is no longer required."
fmt.Println(warningMsg)
return next(c)
}
}
10 changes: 5 additions & 5 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,15 @@ func Test_Router_Group_Middleware(t *testing.T) {

a := testApp()
a.Use(func(h Handler) Handler { return h })
r.Len(a.Middleware.stack, 3)
r.Len(a.Middleware.stack, 4)

g := a.Group("/api/v1")
r.Len(a.Middleware.stack, 3)
r.Len(g.Middleware.stack, 3)
r.Len(a.Middleware.stack, 4)
r.Len(g.Middleware.stack, 4)

g.Use(func(h Handler) Handler { return h })
r.Len(a.Middleware.stack, 3)
r.Len(g.Middleware.stack, 4)
r.Len(a.Middleware.stack, 4)
r.Len(g.Middleware.stack, 5)
}

func Test_Router_Redirect(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/gorilla/sessions"
"github.com/pkg/errors"
)

// Session wraps the "github.com/gorilla/sessions" API
Expand Down Expand Up @@ -60,3 +61,13 @@ func (a *App) getSession(r *http.Request, w http.ResponseWriter) *Session {
res: w,
}
}

func sessionSaver(next Handler) Handler {
return func(c Context) error {
err := next(c)
if err != nil {
return errors.WithStack(err)
}
return c.Session().Save()
}
}

0 comments on commit 3c8d9cb

Please sign in to comment.