Change containter builder struct to func

This commit is contained in:
Širhoe Biazhkovič
2021-09-05 18:22:10 +03:00
parent 114e8c0f06
commit d0e13b3273
4 changed files with 11 additions and 18 deletions

View File

@@ -6,9 +6,7 @@ import (
"sync"
)
type Builder interface {
Build([]byte) (Container, error)
}
type Builder func ([]byte) (Container, error)
var (
buildersMU sync.Mutex
@@ -43,7 +41,7 @@ func GetContainer(name string, confBytes []byte) (Container, error) {
if builder, exist := builders[name]; !exist {
err = ErrContainerDoesNotExist
} else {
cn, err = builder.Build(confBytes)
cn, err = builder(confBytes)
}
return cn, err
}

View File

@@ -11,17 +11,15 @@ import (
)
func init() {
container.Register("directory", builder{})
container.Register("directory", build)
}
type builder struct {}
type Config struct {
WhitelistPath string `yaml:"whitelist_path"`
BlacklistPath string `yaml:"blacklist_path"`
}
func (b builder) Build(confBytes []byte) (container.Container, error) {
func build(confBytes []byte) (container.Container, error) {
c := new(Config)
if err := yaml.Unmarshal(confBytes, c); err != nil {
return nil, fmt.Errorf("unable to deserialise configuration: %v", err)

View File

@@ -10,11 +10,9 @@ import (
)
func init() {
container.Register("list", builder{})
container.Register("list", build)
}
type builder struct {}
type Config struct {
Whitelist []string `yaml:"whitelist"`
Blacklist []string `yaml:"blacklist"`
@@ -22,7 +20,7 @@ type Config struct {
var DUMMY struct{}
func (b builder) Build(confBytes []byte) (container.Container, error) {
func build(confBytes []byte) (container.Container, error) {
c := new(Config)
if err := yaml.Unmarshal(confBytes, c); err != nil {
return nil, fmt.Errorf("unable to deserialise configuration: %v", err)

View File

@@ -44,7 +44,7 @@ func (d driver) NewHook(optionBytes []byte) (middleware.Hook, error) {
return nil, err
}
if c, err := container.GetContainer(cfg.Name, confBytes); err == nil{
if c, err := container.GetContainer(cfg.Name, confBytes); err == nil {
return &hook{c}, nil
} else {
return nil, err
@@ -58,24 +58,23 @@ type hook struct {
hashContainer container.Container
}
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, _ *bittorrent.AnnounceResponse) (context.Context, error) {
var err error
if !h.hashContainer.Contains(req.InfoHash){
if !h.hashContainer.Contains(req.InfoHash) {
err = ErrTorrentUnapproved
}
return ctx, err
}
func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) (context.Context, error) {
func (h *hook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest, _ *bittorrent.ScrapeResponse) (context.Context, error) {
// Scrapes don't require any protection.
return ctx, nil
}
func (h *hook) Stop() stop.Result {
if st, isOk := h.hashContainer.(stop.Stopper); isOk{
if st, isOk := h.hashContainer.(stop.Stopper); isOk {
return st.Stop()
}
return stop.AlreadyStopped