Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
sujit-baniya committed May 4, 2021
1 parent 2f033ce commit fac1136
Showing 1 changed file with 32 additions and 84 deletions.
116 changes: 32 additions & 84 deletions flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,41 @@ import (
)

type Flash struct {
CookiePrefix string
Data fiber.Map
cookiePrefix string
data fiber.Map
}

var DefaultFlash = &Flash{
CookiePrefix: "Fiber-App",
Data: fiber.Map{},
cookiePrefix: "Fiber-App",
data: fiber.Map{},
}

var cookieKeyValueParser = regexp.MustCompile("\x00([^:]*):([^\x00]*)\x00")

func New(cookiePrefix string) {
DefaultFlash = &Flash{
CookiePrefix: cookiePrefix,
Data: fiber.Map{},
cookiePrefix: cookiePrefix,
data: fiber.Map{},
}
}

func (f *Flash) Error(c *fiber.Ctx) {
var flashValue string
f.Data["error"] = true
for key, value := range f.Data {
flashValue += "\x00" + key + ":" + fmt.Sprintf("%v", value) + "\x00"
}
c.Cookie(&fiber.Cookie{
Name: f.CookiePrefix + "-Flash",
Value: url.QueryEscape(flashValue),
})
}

func (f *Flash) Success(c *fiber.Ctx) {
var flashValue string
f.Data["success"] = true
for key, value := range f.Data {
flashValue += "\x00" + key + ":" + fmt.Sprintf("%v", value) + "\x00"
}
c.Cookie(&fiber.Cookie{
Name: f.CookiePrefix + "-Flash",
Value: url.QueryEscape(flashValue),
})
}

func (f *Flash) Get(c *fiber.Ctx) fiber.Map {
t := fiber.Map{}
f.Data = nil
cookieValue := c.Cookies(f.CookiePrefix + "-Flash")
f.data = nil
cookieValue := c.Cookies(f.cookiePrefix + "-Flash")
if cookieValue != "" {
parseKeyValueCookie(cookieValue, func(key string, val interface{}) {
t[key] = val
})
f.Data = t
f.data = t
}
c.Set("Set-Cookie", f.CookiePrefix+"-Flash=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; HttpOnly")
return f.Data
c.Set("Set-Cookie", f.cookiePrefix+"-Flash=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; HttpOnly")
return f.data
}

func (f *Flash) Redirect(c *fiber.Ctx, location string, data interface{}, status ...int) error {

f.Data = data.(fiber.Map)
f.data = data.(fiber.Map)
if len(status) > 0 {
return c.Redirect(location, status[0])
} else {
Expand All @@ -75,84 +51,56 @@ func (f *Flash) Redirect(c *fiber.Ctx, location string, data interface{}, status
}

func (f *Flash) WithError(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
f.Data = data
f.Error(c)
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)
f.data = data
f.success(c)
return c
}

func Error(c *fiber.Ctx) {
var flashValue string
DefaultFlash.Data["error"] = true
for key, value := range DefaultFlash.Data {
flashValue += "\x00" + key + ":" + fmt.Sprintf("%v", value) + "\x00"
}
c.Cookie(&fiber.Cookie{
Name: DefaultFlash.CookiePrefix + "-Flash",
Value: url.QueryEscape(flashValue),
})
func (f *Flash) error(c *fiber.Ctx) {
f.data["error"] = true
f.setCookie(c)
}

func Success(c *fiber.Ctx) {
func (f *Flash) success(c *fiber.Ctx) {
f.data["success"] = true
f.setCookie(c)
}

func (f *Flash) setCookie(c *fiber.Ctx) {
var flashValue string
DefaultFlash.Data["success"] = true
for key, value := range DefaultFlash.Data {
for key, value := range f.data {
flashValue += "\x00" + key + ":" + fmt.Sprintf("%v", value) + "\x00"
}
c.Cookie(&fiber.Cookie{
Name: DefaultFlash.CookiePrefix + "-Flash",
Name: f.cookiePrefix + "-Flash",
Value: url.QueryEscape(flashValue),
})
}

func Get(c *fiber.Ctx) fiber.Map {
t := fiber.Map{}
DefaultFlash.Data = nil
cookieValue := c.Cookies(DefaultFlash.CookiePrefix + "-Flash")
if cookieValue != "" {
parseKeyValueCookie(cookieValue, func(key string, val interface{}) {
t[key] = val
})
DefaultFlash.Data = t
}
c.Set("Set-Cookie", DefaultFlash.CookiePrefix+"-Flash=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; HttpOnly")
return DefaultFlash.Data
return DefaultFlash.Get(c)
}

func Redirect(c *fiber.Ctx, location string, data interface{}, status ...int) error {

DefaultFlash.Data = data.(fiber.Map)
if len(status) > 0 {
return c.Redirect(location, status[0])
} else {
return c.Redirect(location, fiber.StatusFound)
}
return DefaultFlash.Redirect(c, location, data)
}

func WithError(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
DefaultFlash.Data = data
DefaultFlash.Error(c)
return c
return DefaultFlash.WithError(c, data)
}

func WithSuccess(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
DefaultFlash.Data = data
DefaultFlash.Success(c)
return c
}

func SetData(c *fiber.Ctx, data fiber.Map) fiber.Map {
DefaultFlash.Data = data
return data
return DefaultFlash.WithSuccess(c, data)
}

func WithData(c *fiber.Ctx, data fiber.Map) *fiber.Ctx {
DefaultFlash.Data = data
DefaultFlash.data = data
return c
}

Expand Down

0 comments on commit fac1136

Please sign in to comment.