-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagecontext.go
166 lines (139 loc) · 3.94 KB
/
pagecontext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package devportal
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"text/template"
"time"
humanize "github.com/dustin/go-humanize"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"github.com/russross/blackfriday"
)
// TemplateContext is a struct that is used to render templates.
type TemplateContext struct {
root string
Req *http.Request
Account AccountInfo
Args []interface{}
}
// Include includes another file.
func (c *TemplateContext) Include(filename string, args ...interface{}) (string, error) {
file, err := os.Open(filepath.Join(c.root, filename))
if err != nil {
return "", err
}
defer file.Close()
body, err := ioutil.ReadAll(file)
if err != nil {
return "", err
}
tpl, err := template.New(filename).Parse(string(body))
if err != nil {
return "", err
}
c.Args = args
var buf bytes.Buffer
err = tpl.Execute(&buf, c)
if err != nil {
return "", err
}
return buf.String(), nil
}
// Markdown returns the HTML contents of the markdown contained in filename
// (relative to the site root).
func (c *TemplateContext) Markdown(body string) (string, error) {
renderer := blackfriday.HtmlRenderer(0, "", "")
var extns int
extns |= blackfriday.EXTENSION_TABLES
extns |= blackfriday.EXTENSION_FENCED_CODE
extns |= blackfriday.EXTENSION_STRIKETHROUGH
extns |= blackfriday.EXTENSION_DEFINITION_LISTS
markdown := blackfriday.Markdown([]byte(body), renderer, extns)
return string(markdown), nil
}
// OwnedPlugins gets a list of plugins owned by the current user.
func (c *TemplateContext) OwnedPlugins() []Plugin {
acct := c.Req.Context().Value(CtxKey("account")).(AccountInfo)
plugins, err := loadAllPlugins(acct.ID)
if err != nil {
log.Printf("Error loading plugins owned by %s: %v", acct.ID, err)
return nil
}
return plugins
}
func (c *TemplateContext) LoadAccount(acctID string) (AccountInfo, error) {
return loadAccount(acctID)
}
func (c *TemplateContext) LoadPlugin(id string) (Plugin, error) {
return loadPlugin(id)
}
func (c *TemplateContext) Notifications() (NotificationList, error) {
return loadNotifications(c.Account.ID)
}
func (c *TemplateContext) PathVar(name string) string {
return mux.Vars(c.Req)[name]
}
func (c *TemplateContext) Context(key string) interface{} {
return c.Req.Context().Value(CtxKey(key))
}
func (c *TemplateContext) When(then time.Time) string {
return humanize.Time(then)
}
func (c *TemplateContext) PathMatches(other string) bool {
return strings.HasPrefix(c.Req.URL.Path, other)
}
func (c *TemplateContext) Now(layout string) string {
return time.Now().Format(layout)
}
var cookies = sessions.NewCookieStore(
securecookie.GenerateRandomKey(64),
securecookie.GenerateRandomKey(32),
securecookie.GenerateRandomKey(64),
securecookie.GenerateRandomKey(32),
)
func renderTemplatedPage(w http.ResponseWriter, r *http.Request, templatePage string) {
acct, _ := r.Context().Value(CtxKey("account")).(AccountInfo) // may be nil; is OK for some pages
ctx := &TemplateContext{
root: SiteRoot,
Req: r,
Account: acct,
}
tmpl, err := template.ParseFiles(filepath.Join(SiteRoot, templatePage))
if err != nil {
if os.IsNotExist(err) {
http.Error(w, "404 page not found", http.StatusNotFound)
return
}
log.Printf("template parsing: %v", err)
http.Error(w, "error rendering page; please file issue at https://github.com/caddyserver/website", http.StatusInternalServerError)
return
}
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)
err = tmpl.Execute(buf, ctx)
if err != nil {
log.Printf("template execution: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, err = buf.WriteTo(w)
if err != nil {
log.Printf("writing template buffer to wire: %v", err)
return
}
}
// CtxKey is string used for context.Context values.
type CtxKey string
var bufPool = &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}