-
Notifications
You must be signed in to change notification settings - Fork 0
/
party.go
198 lines (174 loc) · 5.28 KB
/
party.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package party
import (
"bytes"
"html/template"
"net/http"
"path/filepath"
"sync"
)
const (
templateDir = "./template"
partialDir = templateDir + "/partial"
)
type templateHandler interface {
String() (string, error)
ServeHTTP(w http.ResponseWriter, r *http.Request)
Data(key string) (interface{}, bool)
SetData(key string, value interface{}) templateHandler
AddDataMap(data map[string]interface{}) templateHandler
ClearData() templateHandler
}
// New create a templateHandler. If development is true, returns a dynamic template
// which provide more development flexibility. Otherwise returns a static template
// that maximize the speed
func New(filename string, data map[string]interface{}, development bool) templateHandler {
if data == nil {
data = make(map[string]interface{})
}
data["IsDevelopment"] = development
if development {
return newDynamicTemplateHandler(filename, data)
} else {
return newStaticTemplateHandler(filename, data)
}
}
///////////////////////////////////////////////////////////////////////
// dynamicTemplateHandler - Flexible but slower, used in development.
///////////////////////////////////////////////////////////////////////
type dynamicTemplateHandler struct {
once sync.Once
funcs template.FuncMap
filename string
data map[string]interface{}
}
func newDynamicTemplateHandler(filename string, data map[string]interface{}) *dynamicTemplateHandler {
t := &dynamicTemplateHandler{
filename: filename,
data: data,
}
t.once.Do(func() {
t.funcs = template.FuncMap{
"include": func(part string) (template.HTML, error) {
buf := bytes.NewBuffer(nil)
pt := template.Must(template.ParseFiles(filepath.Join(partialDir, part)))
err := pt.Execute(buf, t.data)
return template.HTML(buf.String()), err
},
}
})
return t
}
func (t *dynamicTemplateHandler) String() (string, error) {
buf := bytes.NewBuffer(nil)
tpl := template.Must(template.New(t.filename).Funcs(t.funcs).ParseFiles(filepath.Join(templateDir, t.filename)))
if err := tpl.ExecuteTemplate(buf, t.filename, t.data); err != nil {
return "", err
}
return buf.String(), nil
}
func (t *dynamicTemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tpl := template.Must(template.New(t.filename).
Funcs(t.funcs).
ParseFiles(filepath.Join(templateDir, t.filename)))
if err := tpl.ExecuteTemplate(w, t.filename, t.data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// Data get template data
func (t *dynamicTemplateHandler) Data(key string) (interface{}, bool) {
v, found := t.data[key]
return v, found
}
// SetData set template data
func (t *dynamicTemplateHandler) SetData(key string, value interface{}) templateHandler {
t.data[key] = value
return t
}
// AddDataMap add template data map
func (t *dynamicTemplateHandler) AddDataMap(m map[string]interface{}) templateHandler {
if len(t.data) == 0 {
t.data = m
} else {
for k, v := range m {
t.data[k] = v
}
}
return t
}
// ClearData delete template data
func (t *dynamicTemplateHandler) ClearData() templateHandler {
t.data = map[string]interface{}{}
return t
}
//////////////////////////////////////////////////////////
// staticTemplateHandler - Faster, used in production.
//////////////////////////////////////////////////////////
// staticTemplateHandler handles HTTP request by rendering a template
type staticTemplateHandler struct {
once sync.Once
filename string
templ *template.Template
data map[string]interface{}
}
// newStaticStaticTemplateHandler create a new Template Handler
func newStaticTemplateHandler(filename string, data map[string]interface{}) *staticTemplateHandler {
if data == nil {
data = map[string]interface{}{}
}
t := &staticTemplateHandler{
filename: filename,
data: data,
}
t.once.Do(func() {
funcs := template.FuncMap{
"include": func(part string) (template.HTML, error) {
buf := bytes.NewBuffer(nil)
pt := template.Must(template.ParseFiles(filepath.Join(partialDir, part)))
err := pt.Execute(buf, t.data)
return template.HTML(buf.String()), err
},
}
t.templ = template.Must(template.New(t.filename).Funcs(funcs).
ParseFiles(filepath.Join(templateDir, t.filename)))
})
return t
}
func (t *staticTemplateHandler) String() (string, error) {
buf := bytes.NewBuffer(nil)
if err := t.templ.Execute(buf, t.data); err != nil {
return "", err
}
return buf.String(), nil
}
// ServeHTTP provide HTTP service for staticTemplateHandler
func (t *staticTemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := t.templ.Execute(w, t.data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// Data get template data
func (t *staticTemplateHandler) Data(key string) (interface{}, bool) {
v, found := t.data[key]
return v, found
}
// SetData set template data
func (t *staticTemplateHandler) SetData(key string, value interface{}) templateHandler {
t.data[key] = value
return t
}
// AddDataMap add template data map
func (t *staticTemplateHandler) AddDataMap(m map[string]interface{}) templateHandler {
if len(t.data) == 0 {
t.data = m
} else {
for k, v := range m {
t.data[k] = v
}
}
return t
}
// ClearData delete template data
func (t *staticTemplateHandler) ClearData() templateHandler {
t.data = map[string]interface{}{}
return t
}