-
Notifications
You must be signed in to change notification settings - Fork 13
/
template.go
252 lines (213 loc) · 6.63 KB
/
template.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
244
245
246
247
248
249
250
251
252
package handler
import (
"bytes"
"fmt"
"html/template"
"net/http"
"os"
"strings"
"time"
gopi "github.com/djthorpe/gopi/v3"
fcgi "github.com/djthorpe/gopi/v3/pkg/http/fcgi"
)
/////////////////////////////////////////////////////////////////////
// TYPES
type Templates struct {
gopi.Unit
gopi.Server
gopi.Logger
*TemplateCache
*RenderCache
}
type TemplateHandler struct {
gopi.Logger
*TemplateCache
*RenderCache
path string
docroot string
}
type httpServer interface {
gopi.Server
Mux() *http.ServeMux
}
/////////////////////////////////////////////////////////////////////
// LIFECYCLE
func (this *Templates) New(gopi.Config) error {
this.Require(this.Server, this.Logger, this.TemplateCache, this.RenderCache)
// Return success
return nil
}
/////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// Serve registers a service to serve templates for a path
func (this *Templates) Serve(path, docroot string) error {
// Crete a TemplateHandler
if handler, err := this.NewTemplateHandler(path, docroot); err != nil {
return err
} else if err := this.Server.RegisterService(path, handler); err != nil {
return err
}
// Return success
return nil
}
// RegisterRenderer registers a document renderer
func (this *Templates) RegisterRenderer(r gopi.HttpRenderer) error {
return this.RenderCache.Register(r)
}
// Env returns the process environment for a request
func (this *Templates) Env(req *http.Request) map[string]string {
return fcgi.ProcessEnv(req)
}
// Render returns content and modified time for a path
func (this *Templates) Render(req *http.Request) (gopi.HttpRenderContext, error) {
handler, _ := this.Server.(httpServer).Mux().Handler(req)
if handler_, ok := handler.(*TemplateHandler); ok == false {
return gopi.HttpRenderContext{}, Error(req, http.StatusNotFound)
} else {
return handler_.Serve(req)
}
}
/////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
func (this *Templates) NewTemplateHandler(path, docroot string) (http.Handler, error) {
h := new(TemplateHandler)
h.TemplateCache = this.TemplateCache
h.RenderCache = this.RenderCache
h.Logger = this.Logger
// Check path argument
if strings.HasPrefix(path, "/") == false {
return nil, gopi.ErrBadParameter.WithPrefix("NewTemplateHandler: ", path)
} else {
h.path = path
}
// Check docroot argument
if stat, err := os.Stat(docroot); err != nil {
return nil, gopi.ErrBadParameter.WithPrefix("NewTemplateHandler: ", err)
} else if stat.IsDir() == false {
return nil, gopi.ErrBadParameter.WithPrefix("NewTemplateHandler: ", docroot)
} else {
h.docroot = docroot
}
// Return success
return h, nil
}
// Serve is internal version of renderer
func (this *TemplateHandler) Serve(req *http.Request) (gopi.HttpRenderContext, error) {
// Get renderer or return NOT IMPLEMENTED error
renderer := this.RenderCache.Get(this.docroot, req)
if renderer == nil {
return gopi.HttpRenderContext{}, Error(req, http.StatusNotImplemented)
} else {
return renderer.ServeContent(this.docroot, req)
}
}
// ServeHTTP a template through a renderer
func (this *TemplateHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Get renderer or return NOT IMPLEMENTED error
renderer := this.RenderCache.Get(this.docroot, req)
if renderer == nil {
this.ServeError(w, Error(req, http.StatusNotImplemented))
return
}
this.Debugf("ServeHTTP: req=%v renderer=%v", req.URL, renderer)
// Check for If-Modified-Since header on content
if ifmodified := req.Header.Get("If-Modified-Since"); ifmodified != "" {
if date, err := time.Parse(http.TimeFormat, ifmodified); err == nil {
if renderer.IsModifiedSince(this.docroot, req, date) == false {
this.Debugf(" If-Modified-Since %v: Returning %v", ifmodified, http.StatusNotModified)
this.ServeError(w, Error(req, http.StatusNotModified))
return
}
}
}
// Render Content
ctx, err := renderer.ServeContent(this.docroot, req)
if err != nil {
this.ServeError(w, err)
return
} else if ctx.Content == nil {
this.ServeError(w, Error(req, http.StatusNoContent))
return
}
// Get template and template modification time
var tmpl *template.Template
var modtime time.Time
if ctx.Template != "" {
if tmpl, modtime, err = this.TemplateCache.Get(ctx.Template); err != nil {
this.Debugf(" Template %q: Error: %v", ctx.Template, err)
this.ServeError(w, Error(req, http.StatusNotFound, err.Error()))
return
}
}
// Update modification time for page if the template is later
if ctx.Modified.IsZero() == false && modtime.After(ctx.Modified) {
this.Debugf(" Template updates modification time: %v", modtime)
ctx.Modified = modtime
}
// Set default type
if ctx.Type == "" {
ctx.Type = "application/octet-stream"
}
// Set headers
w.Header().Set("Content-Type", ctx.Type)
if ctx.Modified.IsZero() == false {
w.Header().Set("Last-Modified", ctx.Modified.Format(http.TimeFormat))
} else {
w.Header().Set("Cache-Control", "no-cache")
}
// If no template then we expect the content to be []byte
if tmpl == nil {
if data, ok := ctx.Content.([]byte); ok {
w.Header().Set("Content-Length", fmt.Sprint(len(data)))
w.WriteHeader(http.StatusOK)
if req.Method != http.MethodHead {
w.Write(data)
}
return
} else {
this.ServeError(w, Error(req, http.StatusInternalServerError))
return
}
}
// Debugging
/*
if this.Logger.IsDebug() {
if json, err := json.MarshalIndent(ctx.Content, " ", " "); err == nil {
this.Debugf(string(json))
}
}
*/
// Execute through a template
data := new(bytes.Buffer)
if err := tmpl.Execute(data, ctx.Content); err != nil {
this.ServeError(w, Error(req, http.StatusInternalServerError, err.Error()))
return
}
// Set content length and write data
w.Header().Set("Content-Length", fmt.Sprint(data.Len()))
w.WriteHeader(http.StatusOK)
if req.Method != http.MethodHead {
w.Write(data.Bytes())
}
}
// Serve error
func (this *TemplateHandler) ServeError(w http.ResponseWriter, err error) {
if err_, ok := err.(gopi.HttpError); ok == false {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else if err_.Code() == http.StatusPermanentRedirect || err_.Code() == http.StatusTemporaryRedirect {
this.Debugf(" Code: %v", err_.Error())
this.Debugf(" Location: %v", err_.Path())
w.Header().Set("Location", err_.Path())
http.Error(w, err_.Error(), err_.Code())
} else {
http.Error(w, err_.Error(), err_.Code())
}
}
/////////////////////////////////////////////////////////////////////
// STRINGIFY
func (this *Templates) String() string {
str := "<http.templates"
str += " " + this.TemplateCache.String()
str += " " + this.RenderCache.String()
return str + ">"
}