Skip to content
This repository was archived by the owner on Mar 21, 2019. It is now read-only.

Commit 2e654e9

Browse files
committed
init
0 parents  commit 2e654e9

File tree

3 files changed

+477
-0
lines changed

3 files changed

+477
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
flash [![Build Status](https://drone.io/github.com/tango-contrib/flash/status.png)](https://drone.io/github.com/tango-contrib/flash/latest) [![](http://gocover.io/_badge/github.com/tango-contrib/flash)](http://gocover.io/github.com/tango-contrib/flash)
2+
======
3+
4+
Middleware flash is a tool for share data between requests for [Tango](https://github.com/lunny/tango).
5+
6+
## Notice
7+
8+
This is a new version, it stores all data via [session](https://github.com/tango-contrib/session) not cookie. And it is slightly non-compitable with old version.
9+
10+
## Installation
11+
12+
go get github.com/tango-contrib/flash
13+
14+
## Simple Example
15+
16+
```Go
17+
18+
import "github.com/tango-contrib/session"
19+
20+
type FlashAction struct {
21+
flash.Flash
22+
}
23+
24+
func (x *FlashAction) Get() {
25+
x.Flash.Set("test", "test")
26+
}
27+
28+
func (x *FlashAction) Post() {
29+
x.Flash.Get("test").(string) == "test"
30+
}
31+
32+
func main() {
33+
t := tango.Classic()
34+
sessions := session.Sessions()
35+
t.Use(flash.Flashes(sessions))
36+
t.Any("/", new(FlashAction))
37+
t.Run()
38+
}
39+
```

flash.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package flash
2+
3+
import (
4+
"github.com/lunny/tango"
5+
"github.com/tango-contrib/session"
6+
)
7+
8+
var (
9+
FlashName = "tango_flash"
10+
FlashSeperator = "TANGOFLASH"
11+
12+
_ Flasher = &Flash{}
13+
)
14+
15+
type Data map[string]interface{}
16+
17+
// FlashData is a tools to maintain data when using across request.
18+
type Flash struct {
19+
readed Data
20+
flushed Data
21+
session *session.Session
22+
*Options
23+
saved bool
24+
}
25+
26+
func (f *Flash) setFlash(sess *session.Session, readed Data, opt *Options) {
27+
f.readed = readed
28+
f.flushed = make(Data)
29+
f.session = sess
30+
f.Options = opt
31+
}
32+
33+
func (f *Flash) FlushData() Data {
34+
return f.flushed
35+
}
36+
37+
func (f *Flash) Merge() {
38+
for k, v := range f.readed {
39+
f.flushed[k] = v
40+
}
41+
}
42+
43+
func (f *Flash) Data() Data {
44+
return f.readed
45+
}
46+
47+
func (f *Flash) Get(key string) interface{} {
48+
return f.readed[key]
49+
}
50+
51+
func (f *Flash) Set(key string, value interface{}) {
52+
f.readed[key] = value
53+
f.flushed[key] = value
54+
}
55+
56+
func (f *Flash) Add(kvs Data) {
57+
for k, v := range kvs {
58+
f.Set(k, v)
59+
}
60+
}
61+
62+
func (f *Flash) Save() {
63+
if f.saved {
64+
return
65+
}
66+
67+
for key, _ := range f.readed {
68+
f.session.Del(f.Options.FlashName+f.Options.FlashSeperator+key)
69+
}
70+
71+
var keys = make([]string, 0)
72+
for k, v := range f.flushed {
73+
f.session.Set(f.Options.FlashName+f.Options.FlashSeperator+k, v)
74+
keys = append(keys, k)
75+
}
76+
f.session.Set(f.Options.FlashName, keys)
77+
f.saved = true
78+
}
79+
80+
type Flasher interface {
81+
setFlash(*session.Session, Data, *Options)
82+
FlushData() Data
83+
Save()
84+
}
85+
86+
type Options struct {
87+
FlashName string
88+
FlashSeperator string
89+
}
90+
91+
func prepareOptions(opts []Options) Options {
92+
var opt Options
93+
if len(opts) > 0 {
94+
opt = opts[0]
95+
}
96+
97+
if len(opt.FlashName) == 0 {
98+
opt.FlashName = FlashName
99+
}
100+
if len(opt.FlashSeperator) == 0 {
101+
opt.FlashSeperator = FlashSeperator
102+
}
103+
return opt
104+
}
105+
106+
// Flash return a FlashData handler.
107+
func Flashes(sessions *session.Sessions, opts ...Options) tango.HandlerFunc {
108+
opt := prepareOptions(opts)
109+
return func(ctx *tango.Context) {
110+
var flasher Flasher
111+
var ok bool
112+
if action := ctx.Action(); action != nil {
113+
if flasher, ok = action.(Flasher); ok {
114+
sess := sessions.Session(ctx.Req(), ctx.ResponseWriter)
115+
fd := make(Data)
116+
if keys, has := sess.Get(opt.FlashName).([]string); has {
117+
for _, key := range keys {
118+
fd[key] = sess.Get(opt.FlashName+opt.FlashSeperator+key)
119+
}
120+
}
121+
flasher.setFlash(sess, fd, &opt)
122+
}
123+
}
124+
125+
ctx.Next()
126+
127+
if ok {
128+
flasher.Save()
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)