From 98fad75b955d98dd54e2e48f9b2fa5326671d296 Mon Sep 17 00:00:00 2001 From: lrodham Date: Sat, 7 Apr 2018 19:56:42 +0100 Subject: [PATCH] made session, flash, work(ish) --- context.go | 2 ++ default_context.go | 9 +++++++-- default_router.go | 28 ++++++++++++++++------------ route.go | 20 ++++++++++++-------- router.go | 1 - tuu.go | 18 ++++-------------- 6 files changed, 41 insertions(+), 37 deletions(-) diff --git a/context.go b/context.go index 899325a..72be6d3 100644 --- a/context.go +++ b/context.go @@ -20,4 +20,6 @@ type Context interface { Redirect(status int, url string) error Env() string Logger() *logrus.Logger + Session() *Session + Flash() *Flash } diff --git a/default_context.go b/default_context.go index f27aff5..da5bfce 100644 --- a/default_context.go +++ b/default_context.go @@ -12,7 +12,7 @@ import ( "github.com/sirupsen/logrus" ) -func (a *App) newContext(r Route, res http.ResponseWriter, req *http.Request) *DefaultContext { +func newContext(r Route, res http.ResponseWriter, req *http.Request) *DefaultContext { data := make(map[string]interface{}) data["path"] = r.Path data["env"] = r.Env @@ -23,7 +23,7 @@ func (a *App) newContext(r Route, res http.ResponseWriter, req *http.Request) *D params.Set(k, v) } - sessionStore, _ := a.cfg.SessionStore.Get(req, a.cfg.SessionName) + sessionStore, _ := r.Session.Get(req, r.SessionName) session := &Session{ Session: sessionStore, req: req, @@ -110,6 +110,11 @@ func (d *DefaultContext) Render(status int, rr render.Renderer) error { return err } + if d.Session() != nil { + d.Flash().Clear() + d.Flash().persist(d.Session()) + } + d.Response().Header().Set("Content-Type", rr.ContentType()) d.Response().WriteHeader(status) _, err = io.Copy(d.Response(), bb) diff --git a/default_router.go b/default_router.go index ad10a4e..e4f18c7 100644 --- a/default_router.go +++ b/default_router.go @@ -5,6 +5,7 @@ import ( "net/http" "strings" + "github.com/gorilla/sessions" "github.com/sirupsen/logrus" ) @@ -22,11 +23,6 @@ type DefaultRouter struct { Routes []*Route StaticRoutes []*StaticRoute Options *RouterOptions - app *App -} - -func (r *DefaultRouter) SetApp(app *App) { - r.app = app } func (r *DefaultRouter) GET(path string, h Handler) { @@ -66,13 +62,14 @@ func (r *DefaultRouter) addRoute(m, p string, h Handler) { } r.Routes = append(r.Routes, &Route{ - Method: m, - Path: p, - Handler: h, - Env: r.Options.Env, - Middleware: r.Options.MiddlewareStack, - Logger: r.Options.Logger, - app: r.app, + Method: m, + Path: p, + Handler: h, + Env: r.Options.Env, + Middleware: r.Options.MiddlewareStack, + Logger: r.Options.Logger, + Session: r.Options.SessionStore, + SessionName: "_tuu_session_", }) } @@ -81,6 +78,7 @@ type RouterOptions struct { Prefix string MiddlewareStack MiddlewareStack Logger *logrus.Logger + SessionStore sessions.Store } type RouterOption func(*RouterOptions) @@ -108,3 +106,9 @@ func RouterLogger(l *logrus.Logger) RouterOption { o.Logger = l } } + +func RouterSessionStore(store sessions.Store) RouterOption { + return func(o *RouterOptions) { + o.SessionStore = store + } +} diff --git a/route.go b/route.go index 4befd5a..ac3bd77 100644 --- a/route.go +++ b/route.go @@ -3,24 +3,28 @@ package tuu import ( "net/http" + "github.com/gorilla/sessions" + gcontext "github.com/gorilla/context" "github.com/sirupsen/logrus" ) type Route struct { - Method string - Path string - Handler Handler - Env string - Middleware MiddlewareStack - Logger *logrus.Logger - app *App + Method string + Path string + Handler Handler + Env string + Middleware MiddlewareStack + Logger *logrus.Logger + Session sessions.Store + SessionName string } func (r *Route) ServeHTTP(res http.ResponseWriter, req *http.Request) { defer gcontext.Clear(req) - c := r.app.newContext(*r, res, req) + c := newContext(*r, res, req) + defer c.Flash().persist(c.Session()) err := r.Middleware.handler(r)(c) diff --git a/router.go b/router.go index 460fba8..184b031 100644 --- a/router.go +++ b/router.go @@ -5,7 +5,6 @@ import "net/http" type Handler func(Context) error type Router interface { - SetApp(app *App) GET(path string, h Handler) POST(path string, h Handler) Static(path string, root http.FileSystem) diff --git a/tuu.go b/tuu.go index e98cfa9..c857720 100644 --- a/tuu.go +++ b/tuu.go @@ -9,26 +9,16 @@ import ( "os/signal" "github.com/gorilla/mux" - "github.com/gorilla/sessions" ) type Config struct { - IPAddr string - Port string - Env string - SessionName string - SessionStore sessions.Store + IPAddr string + Port string + Env string } func New(r Router, cfg Config) *App { - cfg.SessionName = "_tuu_session" - // TODO: make session secret actual, secret. - cfg.SessionStore = sessions.NewCookieStore([]byte("secret")) - app := &App{router: r, cfg: cfg} - - r.SetApp(app) - - return app + return &App{router: r, cfg: cfg} } type App struct {