-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecDevelop.go
More file actions
178 lines (160 loc) · 4.66 KB
/
execDevelop.go
File metadata and controls
178 lines (160 loc) · 4.66 KB
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
package treetop
import (
"html/template"
"log"
"net/http"
)
// DeveloperExecutor wraps another executor, it will re-generate the view handler for
// every request. This can be used to live-reload templates during development.
//
// Example:
//
// exec := DeveloperExecutor{FileExecutor{}}
// mux.Handle("/hello", exec.NewViewHandler(v))
//
// Note: this is for development use only, it is not suitable for production systems
type DeveloperExecutor struct {
ViewExecutor
}
// NewViewHandler will create a special handler that will reload the templates
// for ever request. Any template errors that occur will be rendered to the client.
//
// Note: this is for development use only, it is not suitable for production systems
func (de *DeveloperExecutor) NewViewHandler(view *View, includes ...*View) ViewHandler {
// dry run to capture errors up front
_ = de.ViewExecutor.NewViewHandler(view, includes...)
return &devHandler{
view: view,
incl: includes,
exec: de.ViewExecutor,
}
}
// devHandler will load and execute the view template for every request.
type devHandler struct {
pageOnly bool
templateOnly bool
view *View
incl []*View
exec ViewExecutor
}
// FragmentOnly creates a new Handler that only responds to fragment requests
func (h *devHandler) FragmentOnly() ViewHandler {
return &devHandler{
templateOnly: true,
pageOnly: h.pageOnly,
view: h.view,
incl: h.incl,
exec: h.exec,
}
}
// PageOnly create a new handler that will only respond to non-fragment (full page) requests
func (h *devHandler) PageOnly() ViewHandler {
return &devHandler{
pageOnly: true,
templateOnly: h.templateOnly,
view: h.view,
incl: h.incl,
exec: h.exec,
}
}
// ServeHTTP for the development handler will generate a new ViewHandler from the
// executor and the view definitions for each request.
// If an error occurs a HTML page will be rendered with the details for debug purposes.
//
// NOTE: This is intended for development, it is not suitable for production use.
func (h *devHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
handler := h.exec.NewViewHandler(h.view, h.incl...)
errs := h.exec.FlushErrors()
if len(errs) > 0 {
w.WriteHeader(http.StatusInternalServerError)
if err := writeDebugErrorPage(w, handler, errs); err != nil {
panic(err)
}
return
}
if h.pageOnly {
handler = handler.PageOnly()
}
if h.templateOnly {
handler = handler.FragmentOnly()
}
if th, ok := handler.(*TemplateHandler); ok && th.ServeTemplateError == nil {
th.ServeTemplateError = func(err error, resp Response, req *http.Request) {
log.Printf("DeveloperExecutor error: %s", err)
if status := resp.Status(0); status > 0 {
resp.WriteHeader(status)
} else {
resp.WriteHeader(http.StatusInternalServerError)
}
if err := writeDebugErrorPage(resp, handler, err); err != nil {
panic(err)
}
}
}
handler.ServeHTTP(w, req)
}
// devErrorTemplate is used to draw debug pages when a template error is
// encountered in a devHandler endpoint
var devErrorTemplate *template.Template
func init() {
devErrorTemplate = template.Must(template.New("dev_error").Parse(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Treetop Template Error</title>
<style type="text/css" media="screen">
body {
line-height: 140%;
margin: 50px;
}
code {font-size: 120%;}
pre code {
background-color: #eee;
border: 1px solid #999;
display: block;
padding: 20px;
}
</style>
</head>
<body>
<h1>Treetop Endpoint Error</h1>
<h3>Errors:</h3>
<pre><code>{{ .Output }}</code></pre>
{{ if .PageView }}
<h3>Page View:</h3>
<pre><code>{{ .PageView }}</code></pre>
{{ end }}
{{ if .TemplateView }}
<h3>Template View:</h3>
<pre><code>{{ .TemplateView }}</code> </pre>
{{ end }}
{{ range $index, $ps := .Includes }}
<h3>Postscript[{{ $index }}]:</h3>
<pre><code>{{ $ps }}</code></pre>
{{ end }}
</body>
</html>
`))
}
// writeDebugErrorPage will write a HTML page will information about the error
// and whatever it can get about the handler endpoint
func writeDebugErrorPage(w http.ResponseWriter, handler ViewHandler, err error) error {
errData := struct {
Output string
PageView string
TemplateView string
Includes []string
}{
Output: err.Error(),
}
if th, ok := handler.(*TemplateHandler); ok {
errData.PageView = SprintViewTree(th.Page)
errData.TemplateView = SprintViewTree(th.Partial)
for _, incl := range th.Includes {
errData.Includes = append(errData.Includes, SprintViewTree(incl))
}
}
return devErrorTemplate.Execute(w, errData)
}