/
plugin.go
58 lines (46 loc) · 1.27 KB
/
plugin.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
package aposteriori
import (
"io"
"net/http"
"sync"
"github.com/sirkon/goproxy/internal/errors"
"github.com/sirkon/goproxy"
)
// FileCache caching primitive
type FileCache interface {
Get(name string) (io.ReadCloser, error)
Set(name string, data io.Reader) error
}
// New aposteriori plugin constructor
func New(next goproxy.Plugin, cache FileCache) goproxy.Plugin {
return &plugin{next: next, cache: cache}
}
// NewCachePriority aposteriori plugin constructor with cache-priority behavior
func NewCachePriority(next goproxy.Plugin, cache FileCache, availablity map[string]map[string]struct{}) goproxy.Plugin {
return &plugin{next: next, cache: cache, registry: availablity}
}
type plugin struct {
sync.Mutex
next goproxy.Plugin
cache FileCache
registry map[string]map[string]struct{} // registry is <module path> → <version>
}
func (p *plugin) Module(req *http.Request, prefix string) (goproxy.Module, error) {
next, err := p.next.Module(req, prefix)
if err != nil {
return nil, errors.Wrapf(err, "aposteriori delegation error")
}
return &module{
next: next,
parent: p,
}, nil
}
func (p *plugin) Leave(source goproxy.Module) error {
return nil
}
func (p *plugin) Close() error {
return nil
}
func (p *plugin) String() string {
return "aposteriori"
}