forked from sujit-baniya/flash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash.go
211 lines (178 loc) · 4.86 KB
/
flash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package flash
import (
"fmt"
"net/url"
"regexp"
"time"
"github.com/gofiber/fiber/v2"
)
type Flash struct {
data fiber.Map
config Config
}
type Config struct {
Name string `json:"name"`
Value string `json:"value"`
Path string `json:"path"`
Domain string `json:"domain"`
MaxAge int `json:"max_age"`
Expires time.Time `json:"expires"`
Secure bool `json:"secure"`
HTTPOnly bool `json:"http_only"`
SameSite string `json:"same_site"`
SessionOnly bool `json:"session_only"`
}
var DefaultFlash *Flash
func init() {
Default(Config{
Name: "fiber-app-flash",
})
}
var cookieKeyValueParser = regexp.MustCompile("\x00([^:]*):([^\x00]*)\x00")
func Default(config Config) {
DefaultFlash = New(config)
}
func New(config Config) *Flash {
if config.SameSite == "" {
config.SameSite = "Lax"
}
return &Flash{
config: config,
data: fiber.Map{},
}
}
func (f *Flash) Get(c *fiber.Ctx) fiber.Map {
t := fiber.Map{}
f.data = nil
cookieValue := c.Cookies(f.config.Name)
if cookieValue != "" {
parseKeyValueCookie(cookieValue, func(key string, val interface{}) {
t[key] = val
})
f.data = t
}
c.Set("Set-Cookie", f.config.Name+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; HttpOnly; SameSite="+f.config.SameSite)
if f.data == nil {
f.data = fiber.Map{}
}
return f.data
}
func (f *Flash) Redirect(c *fiber.Ctx, location string, data interface{}, status ...int) error {
f.data = data.(fiber.Map)
if len(status) > 0 {
return c.Redirect(location, status[0])
} else {
return c.Redirect(location, fiber.StatusFound)
}
}
func (f *Flash) RedirectToRoute(c *fiber.Ctx, routeName string, data fiber.Map, status ...int) error {
f.data = data
if len(status) > 0 {
return c.RedirectToRoute(routeName, data, status[0])
} else {
return c.RedirectToRoute(routeName, data, fiber.StatusFound)
}
}
func (f *Flash) RedirectBack(c *fiber.Ctx, fallback string, data fiber.Map, status ...int) error {
f.data = data
if len(status) > 0 {
return c.RedirectBack(fallback, status[0])
} else {
return c.RedirectBack(fallback, fiber.StatusFound)
}
}
func (f *Flash) WithError(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
f.data = data
f.error(c)
return c
}
func (f *Flash) WithSuccess(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
f.data = data
f.success(c)
return c
}
func (f *Flash) WithWarn(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
f.data = data
f.warn(c)
return c
}
func (f *Flash) WithInfo(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
f.data = data
f.info(c)
return c
}
func (f *Flash) WithData(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
f.data = data
f.setCookie(c)
return c
}
func (f *Flash) error(c *fiber.Ctx) {
f.data["error"] = true
f.setCookie(c)
}
func (f *Flash) success(c *fiber.Ctx) {
f.data["success"] = true
f.setCookie(c)
}
func (f *Flash) warn(c *fiber.Ctx) {
f.data["warn"] = true
f.setCookie(c)
}
func (f *Flash) info(c *fiber.Ctx) {
f.data["info"] = true
f.setCookie(c)
}
func (f *Flash) setCookie(c *fiber.Ctx) {
var flashValue string
for key, value := range f.data {
flashValue += "\x00" + key + ":" + fmt.Sprintf("%v", value) + "\x00"
}
c.Cookie(&fiber.Cookie{
Name: f.config.Name,
Value: url.QueryEscape(flashValue),
SameSite: f.config.SameSite,
Secure: f.config.Secure,
Path: f.config.Path,
Domain: f.config.Domain,
MaxAge: f.config.MaxAge,
Expires: f.config.Expires,
HTTPOnly: f.config.HTTPOnly,
SessionOnly: f.config.SessionOnly,
})
}
func Get(c *fiber.Ctx) fiber.Map {
return DefaultFlash.Get(c)
}
func Redirect(c *fiber.Ctx, location string, data interface{}, status ...int) error {
return DefaultFlash.Redirect(c, location, data, status...)
}
func RedirectToRoute(c *fiber.Ctx, routeName string, data fiber.Map, status ...int) error {
return DefaultFlash.RedirectToRoute(c, routeName, data, status...)
}
func RedirectBack(c *fiber.Ctx, fallback string, data fiber.Map, status ...int) error {
return DefaultFlash.RedirectBack(c, fallback, data, status...)
}
func WithError(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
return DefaultFlash.WithError(c, data)
}
func WithSuccess(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
return DefaultFlash.WithSuccess(c, data)
}
func WithWarn(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
return DefaultFlash.WithWarn(c, data)
}
func WithInfo(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
return DefaultFlash.WithInfo(c, data)
}
func WithData(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
return DefaultFlash.WithData(c, data)
}
// parseKeyValueCookie takes the raw (escaped) cookie value and parses out key values.
func parseKeyValueCookie(val string, cb func(key string, val interface{})) {
val, _ = url.QueryUnescape(val)
if matches := cookieKeyValueParser.FindAllStringSubmatch(val, -1); matches != nil {
for _, match := range matches {
cb(match[1], match[2])
}
}
}