-
Notifications
You must be signed in to change notification settings - Fork 13
/
template.go
176 lines (149 loc) · 4.52 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
package handler
import (
"bytes"
"fmt"
"html/template"
"net/http"
"time"
gopi "github.com/djthorpe/gopi/v3"
)
/////////////////////////////////////////////////////////////////////
// TYPES
type Templates struct {
gopi.Unit
gopi.Server
gopi.Logger
*TemplateCache
*RenderCache
}
/////////////////////////////////////////////////////////////////////
// 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 string) error {
if err := this.Server.RegisterService(path, this); 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)
}
// Serve error
func (this *Templates) 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())
}
}
// Serve a template through a renderer
func (this *Templates) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Get renderer or return NOT IMPLEMENTED error
renderer := this.RenderCache.Get(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(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(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())
}
}
/////////////////////////////////////////////////////////////////////
// STRINGIFY
func (this *Templates) String() string {
str := "<http.templates"
str += " " + this.TemplateCache.String()
str += " " + this.RenderCache.String()
return str + ">"
}