Esempio n. 1
0
// GetFlashes retieves all flash messages found in a cookie session associated with ctx..
//
// name is the session name which is used to store the flash messages. The flash
// messages can be stored in any session, but it is a good idea to separate
// session for flash messages from other sessions.
//
// key is the key that is used to identiry which flash messages are of interest.
func GetFlashes(ctx *base.Context, name, key string) (Flashes, error) {
	ss, err := ctx.GetSession(name)
	if err != nil {
		return nil, err
	}
	if v, ok := ss.Values[key]; ok {
		delete(ss.Values, key)
		serr := ss.Save(ctx.Request(), ctx.Response())
		if serr != nil {
			return nil, serr
		}
		return v.(Flashes), nil
	}
	return nil, errors.New("no flashes found")
}
Esempio n. 2
0
// Save saves flash messages to context
func (f *Flasher) Save(ctx *base.Context, name, key string) error {
	ss, err := ctx.GetSession(name)
	if err != nil {
		return err
	}
	var flashes Flashes
	if v, ok := ss.Values[key]; ok {
		flashes = v.(Flashes)
	}
	ss.Values[key] = append(flashes, f.f...)
	err = ss.Save(ctx.Request(), ctx.Response())
	if err != nil {
		return err
	}
	return nil
}