-
Notifications
You must be signed in to change notification settings - Fork 578
/
ui.go
243 lines (211 loc) · 6 KB
/
ui.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package ui
import (
"bytes"
"errors"
"fmt"
html_template "html/template"
"io"
"log"
"net/http"
"path/filepath"
"runtime"
text_template "text/template"
"github.com/hound-search/hound/config"
)
// An http.Handler for the dev-mode case.
type devHandler struct {
// A simple file server for serving non-template assets
http.Handler
// the collection of templated assets
content map[string]*content
// the root asset dir
root string
// the config we are running on
cfg *config.Config
}
// An http.Handler for the prd-mode case.
type prdHandler struct {
// The collection of templated assets w/ their templates pre-parsed
content map[string]*content
// The config object as a json string
cfgJson string
// the config we are running on
cfg *config.Config
}
func (h *devHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
// See if we have templated content for this path
cr := h.content[p]
if cr == nil {
// if not, serve up files
h.Handler.ServeHTTP(w, r)
return
}
// If so, render the HTML
w.Header().Set("Content-Type", "text/html;charset=utf-8")
if err := renderForDev(w, h.root, cr, h.cfg, r); err != nil {
log.Panic(err)
}
}
// Renders a templated asset in dev-mode. This simply embeds external script tags
// for the source elements.
func renderForDev(w io.Writer, root string, c *content, cfg *config.Config, r *http.Request) error {
var err error
// For more context, see: https://github.com/etsy/hound/issues/239
switch c.tplType {
case "html":
// Use html/template to parse the html template
c.tpl, err = html_template.ParseFiles(filepath.Join(root, c.template))
if err != nil {
return err
}
case "xml", "text":
// Use text/template to parse the xml or text templates
// We are using text/template here for parsing xml to keep things
// consistent with html/template parsing.
c.tpl, err = text_template.ParseFiles(filepath.Join(root, c.template))
if err != nil {
return err
}
default:
return errors.New("invalid tplType for content")
}
json, err := cfg.ToJsonString()
if err != nil {
return err
}
var buf bytes.Buffer
for _, path := range c.sources {
fmt.Fprintf(&buf, "<script src=\"http://localhost:8080/ui/%s\"></script>", path)
}
return c.tpl.Execute(w, map[string]interface{}{
"ReactVersion": ReactVersion,
"jQueryVersion": JQueryVersion,
"ReposAsJson": json,
"Title": cfg.Title,
"Source": html_template.HTML(buf.String()),
"Host": r.Host,
})
}
// Serve an asset over HTTP. This ensures we get proper support for range
// requests and if-modified-since checks.
func serveAsset(w http.ResponseWriter, r *http.Request, name string) {
n, err := AssetInfo(name)
if err != nil {
http.NotFound(w, r)
return
}
a, err := Asset(name)
if err != nil {
http.NotFound(w, r)
return
}
http.ServeContent(w, r, n.Name(), n.ModTime(), bytes.NewReader(a))
}
func (h *prdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
// see if we have a templated asset for this path
ct := h.content[p]
if ct != nil {
// if so, render it
if err := renderForPrd(w, ct, h.cfg, h.cfgJson, r); err != nil {
log.Panic(err)
}
return
}
// otherwise, we need to find the asset in the bundled asset
// data. Assets are relative to the asset directory, so we need
// to remove the leading '/' in the path.
serveAsset(w, r, p[1:])
}
// Renders a templated asset in prd-mode. This strategy will embed
// the sources directly in a script tag on the templated page.
func renderForPrd(w io.Writer, c *content, cfg *config.Config, cfgJson string, r *http.Request) error {
var buf bytes.Buffer
buf.WriteString("<script>")
for _, src := range c.sources {
a, err := Asset(src)
if err != nil {
return err
}
buf.Write(a)
}
buf.WriteString("</script>")
return c.tpl.Execute(w, map[string]interface{}{
"ReactVersion": ReactVersion,
"jQueryVersion": JQueryVersion,
"ReposAsJson": cfgJson,
"Title": cfg.Title,
"Source": html_template.HTML(buf.String()),
"Host": r.Host,
})
}
// Used for dev-mode only. Determime the asset directory where
// we can find all our web files for direct serving.
func assetDir() string {
_, file, _, _ := runtime.Caller(0)
dir, err := filepath.Abs(
filepath.Join(filepath.Dir(file), "assets"))
if err != nil {
log.Panic(err)
}
return dir
}
// Create an http.Handler for dev-mode.
func newDevHandler(cfg *config.Config) (http.Handler, error) {
root := assetDir()
return &devHandler{
Handler: http.FileServer(http.Dir(root)),
content: contents,
root: root,
cfg: cfg,
}, nil
}
// Create an http.Handler for prd-mode.
func newPrdHandler(cfg *config.Config) (http.Handler, error) {
for _, cnt := range contents {
a, err := Asset(cnt.template)
if err != nil {
return nil, err
}
// For more context, see: https://github.com/etsy/hound/issues/239
switch cnt.tplType {
case "html":
// Use html/template to parse the html template
cnt.tpl, err = html_template.New(cnt.template).Parse(string(a))
if err != nil {
return nil, err
}
case "xml", "text":
// Use text/template to parse the xml or text templates
// We are using text/template here for parsing xml to keep things
// consistent with html/template parsing.
cnt.tpl, err = text_template.New(cnt.template).Parse(string(a))
if err != nil {
return nil, err
}
default:
return nil, errors.New("invalid tplType for content")
}
}
json, err := cfg.ToJsonString()
if err != nil {
return nil, err
}
return &prdHandler{
content: contents,
cfg: cfg,
cfgJson: json,
}, nil
}
// Create an http.Handler for serving the web assets. If dev is true,
// the http.Handler that is returned will serve assets directly our of
// the source directories making rapid web development possible. If dev
// is false, the http.Handler will serve assets out of data embedded
// in the executable.
func Content(dev bool, cfg *config.Config) (http.Handler, error) {
if dev {
return newDevHandler(cfg)
}
return newPrdHandler(cfg)
}