Beispiel #1
0
func (c *SessionStorage) Get(key string, ctx echo.Context) string {
	s := X.X(ctx).Session()
	if s == nil {
		return ""
	}
	val, _ := s.Get(key).(string)
	return val
}
Beispiel #2
0
func (c *SessionStorage) Set(key, val string, ctx echo.Context) {
	s := X.X(ctx).Session()
	if s == nil {
		return
	}
	s.Set(key, val)
	s.Save()
}
Beispiel #3
0
func (c *Config) Write(b []byte, ctx echo.Context) bool {
	if !c.HtmlCacheOn || ctx.Request().Method() != `GET` || X.X(ctx).Code != http.StatusOK {
		return false
	}
	tmpl := X.MustString(ctx, `webx:saveHtmlFile`)
	if tmpl == `` {
		return false
	}
	if err := com.WriteFile(tmpl, b); err != nil {
		ctx.Object().Echo().Logger().Debug(err)
	}
	return true
}
Beispiel #4
0
func Sessions(name string, store ss.Store) echo.MiddlewareFunc {
	return echo.MiddlewareFunc(func(h echo.Handler) echo.Handler {
		return echo.HandlerFunc(func(ctx echo.Context) error {
			c := X.X(ctx)
			s := ss.NewMySession(store, name, ctx)
			if se, ok := interface{}(c).(Sessionser); ok {
				se.InitSession(s)
			}
			err := h.Handle(c)
			s.Save()
			return err
		})
	})
}
Beispiel #5
0
func (a *Language) Middleware() echo.MiddlewareFunc {
	return echo.MiddlewareFunc(func(h echo.Handler) echo.Handler {
		return echo.HandlerFunc(func(c echo.Context) error {
			lang := a.DetectURI(c.Response(), c.Request())
			c.SetFunc("Lang", func() string {
				return lang
			})
			c.SetFunc("T", func(key string, args ...interface{}) string {
				return i18n.T(lang, key, args...)
			})
			X.X(c).Language = lang
			return h.Handle(c)
		})
	})
}
Beispiel #6
0
func (c *Config) Middleware() echo.MiddlewareFunc {
	return echo.MiddlewareFunc(func(h echo.Handler) echo.Handler {
		return echo.HandlerFunc(func(ctx echo.Context) error {
			if c.Read(ctx) {
				return nil
			}
			if err := h.Handle(ctx); err != nil {
				return err
			}
			if X.X(ctx).Exit {
				return nil
			}
			c.Write(ctx.Response().Body(), ctx)
			return nil
		})
	})
}
Beispiel #7
0
func (c *CookieStorage) Set(key, val string, ctx echo.Context) {
	X.X(ctx).SetCookie(key, val)
}
Beispiel #8
0
func (c *CookieStorage) Get(key string, ctx echo.Context) string {
	return X.X(ctx).GetCookie(key)
}
Beispiel #9
0
func (c *Config) Read(ctx echo.Context) bool {
	ct := X.X(ctx)
	req := ctx.Request()
	if !c.HtmlCacheOn || req.Method() != `GET` {
		return false
	}
	p := strings.Trim(req.URL().Path(), `/`)
	if p == `` {
		p = `index`
	}
	s := strings.SplitN(p, `/`, 3)

	if c.htmlCacheRules == nil {
		c.htmlCacheRules = make(map[string]*Rule)
		for key, rule := range c.HtmlCacheRules {
			c.htmlCacheRules[key] = c.Rule(rule)
		}
	}

	var rule *Rule
	switch len(s) {
	case 2:
		k := s[0] + `:` + s[1]
		if v, ok := c.htmlCacheRules[k]; ok {
			rule = v
		} else if v, ok := c.htmlCacheRules[s[1]]; ok {
			rule = v
		} else {
			k = s[0] + `:`
			if v, ok := c.htmlCacheRules[k]; ok {
				rule = v
			}
		}
	case 1:
		k := s[0] + `:`
		if v, ok := c.htmlCacheRules[k]; ok {
			rule = v
		}
	}
	var saveFile string = c.SaveFileName(rule, ctx)
	if saveFile == "" {
		return false
	}
	if ct.Format != `` {
		saveFile += `.` + ct.Format
	}
	mtime, expired := c.Expired(rule, ctx, saveFile)
	if expired {
		ctx.Set(`webx:saveHtmlFile`, saveFile)
		return false
	}
	//_ = mtime
	//ctx.File(saveFile, ``, false)

	if !HttpCache(ctx, mtime, nil) {
		html, err := com.ReadFile(saveFile)
		if err != nil {
			ctx.Object().Echo().Logger().Error(err)
		}
		Output(html, ct)
	}

	ct.Exit = true
	return true
}