context: add Push action

Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
Tw
2017-04-24 15:25:04 +08:00
parent aa7ecb02af
commit 761a32a080
4 changed files with 66 additions and 7 deletions

View File

@@ -29,6 +29,20 @@ type Context struct {
Req *http.Request
URL *url.URL
Args []interface{} // defined by arguments to .Include
// just used for adding preload links for server push
responseHeader http.Header
}
// NewContextWithHeader creates a context with given response header.
//
// To plugin developer:
// The returned context's exported fileds remain empty,
// you should then initialize them if you want.
func NewContextWithHeader(rh http.Header) Context {
return Context{
responseHeader: rh,
}
}
// Include returns the contents of filename relative to the site root.
@@ -410,6 +424,15 @@ func (c Context) RandomString(minLen, maxLen int) string {
return string(result)
}
// Push adds a preload link in response header for server push
func (c Context) Push(link string) string {
if c.responseHeader == nil {
return ""
}
c.responseHeader.Add("Link", "<"+link+">; rel=preload")
return ""
}
// buffer pool for .Include context actions
var includeBufs = sync.Pool{
New: func() interface{} {