Esempio n. 1
0
// AddFlashToCtx takes flash messages stored in a cookie which is associated with the
// request found in ctx, and puts them inside the ctx object. The flash messages can then
// be retrived by calling ctx.Get( FlashKey).
//
// NOTE When there are no flash messages then nothing is set.
func AddFlashToCtx(ctx *base.Context, name, key string) error {
	f, err := GetFlashes(ctx, name, key)
	if err != nil {
		return err
	}
	ctx.SetData(key, f)
	return nil
}
Esempio n. 2
0
// executes the method fn on Controller ctrl, it sets context.
func (r *Router) handleController(ctx *base.Context, fn string, ctrl controller.Controller) {
	ctrl.New(ctx)
	// execute the method
	// TODO: better error handling?
	if x := ita.New(ctrl).Call(fn); x.Error() != nil {
		ctx.Set(http.StatusInternalServerError)
		_, _ = ctx.Write([]byte(x.Error().Error()))
		ctx.TextPlain()
		_ = ctx.Commit()
		return
	}
	err := ctx.Commit()
	if err != nil {
		//TODO:  Log error
	}
}
Esempio n. 3
0
// preparebase.Context sets view,config and model on the ctx.
func (r *Router) prepareContext(ctx *base.Context) {
	if r.Options != nil {
		if r.Options.View != nil {
			ctx.Set(r.Options.View)
		}
		if r.Options.Config != nil {
			ctx.Cfg = r.Options.Config
		}
		if r.Options.Model != nil {
			ctx.DB = r.Options.Model
		}
		if r.Options.Log != nil {
			ctx.Log = r.Options.Log
		}
		if r.Options.SessionStore != nil {
			ctx.SessionStore = r.Options.SessionStore
		}
	}

	// It is a good idea to ensure that a well prepared context always has the
	// Log field set.
	if ctx.Log == nil {
		ctx.Log = defaultLogger
	}
}
Esempio n. 4
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. 5
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
}