-
Notifications
You must be signed in to change notification settings - Fork 8
/
twhandler.go
280 lines (236 loc) · 7.05 KB
/
twhandler.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Package twhandler provides an HTTP handler that performs processing on CSS files and serves them.
package twhandler
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
"sync"
"github.com/cespare/xxhash"
"github.com/gotailwindcss/tailwind"
)
// New returns a Handler. TODO explain args
// The internal cache is enabled on the Handler returned.
func New(fs http.FileSystem, pathPrefix string, dist tailwind.Dist) *Handler {
return NewFromFunc(fs, pathPrefix, func(w io.Writer) *tailwind.Converter {
return tailwind.New(w, dist)
})
}
// allows things like purger to be set
func NewFromFunc(fs http.FileSystem, pathPrefix string, converterFunc func(w io.Writer) *tailwind.Converter) *Handler {
return &Handler{
converterFunc: converterFunc,
fs: fs,
pathPrefix: pathPrefix,
cache: make(map[string]cacheValue),
headerFunc: defaultHeaderFunc,
}
}
func defaultHeaderFunc(w http.ResponseWriter, r *http.Request) {
cc := w.Header().Get("Cache-Control")
if cc == "" {
// Force browser to check each time, but 304 still works.
w.Header().Set("Cache-Control", "no-cache")
}
}
// // TODO: probably a shorthand that sets up the purger, etc. would make sense
// // enables purging for all default file types on dir
// func NewDev(dir, pathPrefix string, dist tailwind.Dist) {
// }
// Handler serves an HTTP response for a CSS file that is process using tailwind.
type Handler struct {
// dist tailwind.Dist
converterFunc func(w io.Writer) *tailwind.Converter
fs http.FileSystem
notFound http.Handler
pathPrefix string
writeCloserFunc func(w http.ResponseWriter, r *http.Request) io.WriteCloser
cache map[string]cacheValue
rwmu sync.RWMutex
headerFunc func(w http.ResponseWriter, r *http.Request)
}
// SetMaxAge calls SetHeaderFunc with a function that sets the Cache-Control header (if not already set)
// with a corresponding maximum timeout specified in seconds. If cache-breaking
// URLs are in use, this is a good option to set in production.
func (h *Handler) SetMaxAge(n int) {
h.SetHeaderFunc(func(w http.ResponseWriter, r *http.Request) {
cc := w.Header().Get("Cache-Control")
if cc == "" {
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", n))
}
})
}
// SetHeaderFunc assigns a function that gets called immediately before a valid response is served.
// It was added so applications could customize cache headers. By default, the Cache-Control
// header will be set to "no-cache" if it was not set earlier (causing the browser to check
// each time for an updated resource - which may result in a full response or a 304).
func (h *Handler) SetHeaderFunc(f func(w http.ResponseWriter, r *http.Request)) {
h.headerFunc = f
}
// SetNotFoundHandler assigns the handler that gets called when something is not found.
func (h *Handler) SetNotFoundHandler(nfh http.Handler) {
h.notFound = nfh
}
// SetCache with false will disable the cache.
func (h *Handler) SetCache(enabled bool) {
if enabled {
h.cache = make(map[string]cacheValue)
} else {
h.cache = nil
}
}
// TODO: be sure to have clear example showing brotli
func (h *Handler) SetWriteCloserFunc(f func(w http.ResponseWriter, r *http.Request) io.WriteCloser) {
h.writeCloserFunc = f
}
// not sure if we need something like this...
// // SetAllowFileFunc
// func (h *Handler) SetAllowFileFunc(f func(p string) bool) {
// }
// ServeHTTP implements http.Handler.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p := path.Clean(r.URL.Path)
p = path.Clean(strings.TrimPrefix(p, h.pathPrefix))
f, err := h.fs.Open(p)
if err != nil {
code := 500
if os.IsPermission(err) {
code = 403
} else if os.IsNotExist(err) {
if h.notFound != nil {
h.notFound.ServeHTTP(w, r)
return
}
code = 404
}
http.Error(w, fmt.Sprintf("error opening %s: %v", r.URL.Path, err), code)
return
}
defer f.Close()
w.Header().Set("Content-Type", "text/css")
st, err := f.Stat()
if err != nil {
http.Error(w, fmt.Sprintf("stat failed for %s: %v", r.URL.Path, err), 500)
return
}
if h.headerFunc != nil {
h.headerFunc(w, r)
}
if h.cache != nil { // if cache enabled
h.rwmu.RLock()
cv, ok := h.cache[p]
h.rwmu.RUnlock()
if ok {
// if h.check304(w, r, cv) {
// return
// }
wc := h.makeW(w, r)
defer wc.Close()
// handle 304s properly with ServeContent
http.ServeContent(
&wwrap{Writer: wc, ResponseWriter: w},
r,
p,
st.ModTime(),
strings.NewReader(cv.content),
)
return
}
cv.content, cv.hash, err = h.process(w, r, f)
if err != nil {
http.Error(w, fmt.Sprintf("processing failed on %s: %v", r.URL.Path, err), 500)
return
}
h.rwmu.Lock()
h.cache[p] = cv
h.rwmu.Unlock()
return
}
_, _, err = h.process(w, r, f)
if err != nil {
http.Error(w, fmt.Sprintf("processing failed on %s: %v", r.URL.Path, err), 500)
return
}
// // ck := cacheKey{
// // tsnano: st.ModTime().UnixNano(),
// // size: st.Size(),
// // path: p,
// // }
// conv := tailwind.New(w, h.dist)
// conv.AddReader(p, f, false)
// err = conv.Run()
// if err != nil {
// http.Error(w, err.Error(), 500)
// return
// }
}
func (h *Handler) makeW(w http.ResponseWriter, r *http.Request) io.WriteCloser {
var wc io.WriteCloser
if h.writeCloserFunc != nil {
wc = h.writeCloserFunc(w, r)
} else {
wc = &nopWriteCloser{Writer: w}
}
return wc
}
// func (h *Handler) send(w http.ResponseWriter, r *http.Request, r io.Reader) {
// var wc io.WriteCloser
// if h.writeCloserFunc != nil {
// wc = h.writeCloserFunc(w, r)
// } else {
// wc = &nopWriteCloser{Writer: w}
// }
// defer wc.Close()
// }
// // see if we can respond with a 304, returns true if we did
// func (h *Handler) check304(w http.ResponseWriter, r *http.Request, cv cacheValue) bool {
// // if t, err := time.Parse(TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
// // TODO: use etag
// return false
// }
func (h *Handler) process(w http.ResponseWriter, r *http.Request, rd io.Reader) (content string, hash uint64, reterr error) {
wc := h.makeW(w, r)
defer wc.Close()
var outbuf bytes.Buffer
// outbuf.Grow(4096)
d := xxhash.New()
// write to response (optionally via compressor from makeW), cache buffer, and hash calc'er at the same time
mw := io.MultiWriter(wc, &outbuf, d)
conv := h.converterFunc(mw)
// conv := tailwind.New(mw, h.dist)
conv.AddReader(r.URL.Path, rd, false)
err := conv.Run()
if err != nil {
reterr = err
return
}
return outbuf.String(), d.Sum64(), nil
}
type nopWriteCloser struct {
io.Writer
}
func (n *nopWriteCloser) Close() error {
return nil
}
// type cacheKey struct {
// path string
// size int64
// tsnano int64
// }
type cacheValue struct {
size int64 // in bytes
tsnano int64 // file mod time
content string // output
hash uint64 // for e-tag
}
// wwrap wraps a ResponseWriter allowing us to override where the Write calls go
type wwrap struct {
io.Writer
http.ResponseWriter
}
func (w *wwrap) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}