mirror of
https://github.com/cubixle/tuu.git
synced 2026-04-24 20:04:43 +01:00
made session, flash, work(ish)
This commit is contained in:
@@ -20,4 +20,6 @@ type Context interface {
|
||||
Redirect(status int, url string) error
|
||||
Env() string
|
||||
Logger() *logrus.Logger
|
||||
Session() *Session
|
||||
Flash() *Flash
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
@@ -72,7 +68,8 @@ func (r *DefaultRouter) addRoute(m, p string, h Handler) {
|
||||
Env: r.Options.Env,
|
||||
Middleware: r.Options.MiddlewareStack,
|
||||
Logger: r.Options.Logger,
|
||||
app: r.app,
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
8
route.go
8
route.go
@@ -3,6 +3,8 @@ package tuu
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
|
||||
gcontext "github.com/gorilla/context"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -14,13 +16,15 @@ type Route struct {
|
||||
Env string
|
||||
Middleware MiddlewareStack
|
||||
Logger *logrus.Logger
|
||||
app *App
|
||||
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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
12
tuu.go
12
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
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user