/
combiner.go
73 lines (52 loc) · 1.54 KB
/
combiner.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
package gocaveman
import (
"context"
"fmt"
"net/http"
)
type FileCombiner struct {
PageIndex *PageIndex
NextHandler http.Handler
}
func (fc *FileCombiner) SetNextHandler(next http.Handler) ChainedHandler {
fc.NextHandler = next
return fc
}
// func (fc *FileCombiner) AddHandler(h http.Handler) StackableHandler {
// return NewStackableHandler(fc, h)
// }
// func (fc *FileCombiner) SetNextHandler(next http.Handler) ChainedHandler {
// fc.NextHandler = next
// return MakeChainedHandler(next)
// }
func CtxWithCombinedPath(ctx context.Context, pathType string, resourcePath string) context.Context {
return context.WithValue(ctx, "combined_path_"+pathType, resourcePath)
}
func (fc *FileCombiner) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// two things:
// * if it's a page then lookup PageMeta, examine includes, work out whatever ID to give it and attach to context
// * if it's for the combined file, then look it up and return it
ctx := r.Context()
ctx = CtxWithCombinedPath(ctx, "css", "/path/to/combined.css")
if fc.NextHandler == nil {
panic(fmt.Errorf("FileCombiner requires a NextHandler"))
}
fc.NextHandler.ServeHTTP(w, r.WithContext(ctx))
// UpdateChainWR(w, r.WithContext(ctx))
}
// fc := &FileCombiner
// h = fc
// h.AddHandler
// func f() {
// h := 0
// fc := &FileCombiner{}
// h = h.SetNextHandler(fc)
// }
// func MakeChainedHandler(h http.Handler) ChainedHandler {
// // if h is a ChainedHandler, return it
// if ch, ok := h.(ChainedHandler); ok {
// return ch
// }
// // if not then
// return
// }