-
Notifications
You must be signed in to change notification settings - Fork 6
/
host.go
61 lines (45 loc) · 1.26 KB
/
host.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
package stitcher
import (
"context"
"log"
"time"
"github.com/mailgun/groupcache/v2"
)
// Host represents a single VHOSTed site
type Host struct {
Hostname string `hcl:"hostname"`
Routes []Route `hcl:"route,block"`
Cache *groupcache.Group
MaxCache int64 `hcl:"max_cache,optional"`
}
// Init handles host specific initialization
func (host *Host) Init() {
maxCache := host.MaxCache
if maxCache == 0 {
maxCache = 1 << 20
}
// Create a new group cache with a max cache size of 3MB
host.Cache = groupcache.NewGroup(host.Hostname, maxCache, groupcache.GetterFunc(
func(ctx context.Context, id string, dest groupcache.Sink) error {
v := ctx.Value(requestContextKey("request"))
r, ok := v.(ContentContextValue)
if ok {
content, err := r.Content.Render(r.Site, r.ContextData)
if err != nil {
log.Printf("Error: %v - '%+v'\n", err, r.Content)
return err
}
ttl, err := time.ParseDuration(r.Content.CacheTTL)
if err != nil {
log.Printf("Error parsing TTL duration: '%v' for key '%s' defaulting to a TTL of one minute\n", err, id)
ttl = time.Minute * 1
}
if err := dest.SetString(content, time.Now().Add(ttl)); err != nil {
log.Println("SetString", err)
return err
}
}
return nil
},
))
}