github.com/sirkon/goproxy@v1.4.8/plugin/aposteriori/plugin.go (about)

     1  package aposteriori
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"sync"
     7  
     8  	"github.com/sirkon/goproxy/internal/errors"
     9  
    10  	"github.com/sirkon/goproxy"
    11  )
    12  
    13  // FileCache caching primitive
    14  type FileCache interface {
    15  	Get(name string) (io.ReadCloser, error)
    16  	Set(name string, data io.Reader) error
    17  }
    18  
    19  // New aposteriori plugin constructor
    20  func New(next goproxy.Plugin, cache FileCache) goproxy.Plugin {
    21  	return &plugin{next: next, cache: cache}
    22  }
    23  
    24  // NewCachePriority aposteriori plugin constructor with cache-priority behavior
    25  func NewCachePriority(next goproxy.Plugin, cache FileCache, availablity map[string]map[string]struct{}) goproxy.Plugin {
    26  	return &plugin{next: next, cache: cache, registry: availablity}
    27  }
    28  
    29  type plugin struct {
    30  	sync.Mutex
    31  	next     goproxy.Plugin
    32  	cache    FileCache
    33  	registry map[string]map[string]struct{} // registry is <module path> → <version>
    34  }
    35  
    36  func (p *plugin) Module(req *http.Request, prefix string) (goproxy.Module, error) {
    37  	next, err := p.next.Module(req, prefix)
    38  	if err != nil {
    39  		return nil, errors.Wrapf(err, "aposteriori delegation error")
    40  	}
    41  
    42  	return &module{
    43  		next:   next,
    44  		parent: p,
    45  	}, nil
    46  }
    47  
    48  func (p *plugin) Leave(source goproxy.Module) error {
    49  	return nil
    50  }
    51  
    52  func (p *plugin) Close() error {
    53  	return nil
    54  }
    55  
    56  func (p *plugin) String() string {
    57  	return "aposteriori"
    58  }