made session, flash, work(ish)

This commit is contained in:
2018-04-07 19:56:42 +01:00
parent b8f8c995c1
commit 98fad75b95
6 changed files with 41 additions and 37 deletions

View File

@@ -20,4 +20,6 @@ type Context interface {
Redirect(status int, url string) error Redirect(status int, url string) error
Env() string Env() string
Logger() *logrus.Logger Logger() *logrus.Logger
Session() *Session
Flash() *Flash
} }

View File

@@ -12,7 +12,7 @@ import (
"github.com/sirupsen/logrus" "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 := make(map[string]interface{})
data["path"] = r.Path data["path"] = r.Path
data["env"] = r.Env 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) params.Set(k, v)
} }
sessionStore, _ := a.cfg.SessionStore.Get(req, a.cfg.SessionName) sessionStore, _ := r.Session.Get(req, r.SessionName)
session := &Session{ session := &Session{
Session: sessionStore, Session: sessionStore,
req: req, req: req,
@@ -110,6 +110,11 @@ func (d *DefaultContext) Render(status int, rr render.Renderer) error {
return err return err
} }
if d.Session() != nil {
d.Flash().Clear()
d.Flash().persist(d.Session())
}
d.Response().Header().Set("Content-Type", rr.ContentType()) d.Response().Header().Set("Content-Type", rr.ContentType())
d.Response().WriteHeader(status) d.Response().WriteHeader(status)
_, err = io.Copy(d.Response(), bb) _, err = io.Copy(d.Response(), bb)

View File

@@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/gorilla/sessions"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -22,11 +23,6 @@ type DefaultRouter struct {
Routes []*Route Routes []*Route
StaticRoutes []*StaticRoute StaticRoutes []*StaticRoute
Options *RouterOptions Options *RouterOptions
app *App
}
func (r *DefaultRouter) SetApp(app *App) {
r.app = app
} }
func (r *DefaultRouter) GET(path string, h Handler) { func (r *DefaultRouter) GET(path string, h Handler) {
@@ -72,7 +68,8 @@ func (r *DefaultRouter) addRoute(m, p string, h Handler) {
Env: r.Options.Env, Env: r.Options.Env,
Middleware: r.Options.MiddlewareStack, Middleware: r.Options.MiddlewareStack,
Logger: r.Options.Logger, Logger: r.Options.Logger,
app: r.app, Session: r.Options.SessionStore,
SessionName: "_tuu_session_",
}) })
} }
@@ -81,6 +78,7 @@ type RouterOptions struct {
Prefix string Prefix string
MiddlewareStack MiddlewareStack MiddlewareStack MiddlewareStack
Logger *logrus.Logger Logger *logrus.Logger
SessionStore sessions.Store
} }
type RouterOption func(*RouterOptions) type RouterOption func(*RouterOptions)
@@ -108,3 +106,9 @@ func RouterLogger(l *logrus.Logger) RouterOption {
o.Logger = l o.Logger = l
} }
} }
func RouterSessionStore(store sessions.Store) RouterOption {
return func(o *RouterOptions) {
o.SessionStore = store
}
}

View File

@@ -3,6 +3,8 @@ package tuu
import ( import (
"net/http" "net/http"
"github.com/gorilla/sessions"
gcontext "github.com/gorilla/context" gcontext "github.com/gorilla/context"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@@ -14,13 +16,15 @@ type Route struct {
Env string Env string
Middleware MiddlewareStack Middleware MiddlewareStack
Logger *logrus.Logger Logger *logrus.Logger
app *App Session sessions.Store
SessionName string
} }
func (r *Route) ServeHTTP(res http.ResponseWriter, req *http.Request) { func (r *Route) ServeHTTP(res http.ResponseWriter, req *http.Request) {
defer gcontext.Clear(req) 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) err := r.Middleware.handler(r)(c)

View File

@@ -5,7 +5,6 @@ import "net/http"
type Handler func(Context) error type Handler func(Context) error
type Router interface { type Router interface {
SetApp(app *App)
GET(path string, h Handler) GET(path string, h Handler)
POST(path string, h Handler) POST(path string, h Handler)
Static(path string, root http.FileSystem) Static(path string, root http.FileSystem)

12
tuu.go
View File

@@ -9,26 +9,16 @@ import (
"os/signal" "os/signal"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/gorilla/sessions"
) )
type Config struct { type Config struct {
IPAddr string IPAddr string
Port string Port string
Env string Env string
SessionName string
SessionStore sessions.Store
} }
func New(r Router, cfg Config) *App { func New(r Router, cfg Config) *App {
cfg.SessionName = "_tuu_session" return &App{router: r, cfg: cfg}
// TODO: make session secret actual, secret.
cfg.SessionStore = sessions.NewCookieStore([]byte("secret"))
app := &App{router: r, cfg: cfg}
r.SetApp(app)
return app
} }
type App struct { type App struct {