本文整理汇总了Golang中github.com/webx-top/echo.Context.Object方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Object方法的具体用法?Golang Context.Object怎么用?Golang Context.Object使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/webx-top/echo.Context
的用法示例。
在下文中一共展示了Context.Object方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HttpCache
func HttpCache(ctx echo.Context, eTag interface{}, etagValidator func(oldEtag, newEtag string) bool) bool {
var etag string
if eTag == nil {
etag = fmt.Sprintf(`%v`, time.Now().UTC().Unix())
} else {
etag = fmt.Sprintf(`%v`, eTag)
}
resp := ctx.Response()
//resp.Header().Set(`Connection`, `keep-alive`)
resp.Header().Set(`X-Cache`, `HIT from Webx-Page-Cache`)
if inm := ctx.Request().Header().Get("If-None-Match"); inm != `` {
var valid bool
if etagValidator != nil {
valid = etagValidator(inm, etag)
} else {
valid = inm == etag
}
if valid {
resp.Header().Del(`Content-Type`)
resp.Header().Del(`Content-Length`)
resp.WriteHeader(http.StatusNotModified)
ctx.Object().Echo().Logger().Debugf(`%v is not modified.`, ctx.Path())
return true
}
}
resp.Header().Set(`Etag`, etag)
resp.Header().Set(`Cache-Control`, `public,max-age=1`)
return false
}
示例2: Expired
func (c *Config) Expired(rule *Rule, ctx echo.Context, saveFile string) (int64, bool) {
var expired int64
if rule.ExpireTime > 0 {
expired = int64(rule.ExpireTime)
} else if rule.ExpireFunc != nil {
return rule.ExpireFunc(saveFile, ctx)
} else {
switch c.HtmlCacheTime.(type) {
case int:
expired = int64(c.HtmlCacheTime.(int))
case int64:
expired = c.HtmlCacheTime.(int64)
case func(string, echo.Context) (int64, bool):
fn := c.HtmlCacheTime.(func(string, echo.Context) (int64, bool))
return fn(saveFile, ctx)
}
}
mtime, err := com.FileMTime(saveFile)
if err != nil {
ctx.Object().Echo().Logger().Debug(err)
}
if mtime == 0 {
return mtime, true
}
if time.Now().Local().Unix() > mtime+expired {
return mtime, true
}
return mtime, false
}
示例3: Write
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
}
示例4: Read
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
}