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

View File

@@ -11,17 +11,15 @@ import (
) )
func init() { func init() {
container.Register("directory", builder{}) container.Register("directory", build)
} }
type builder struct {}
type Config struct { type Config struct {
WhitelistPath string `yaml:"whitelist_path"` WhitelistPath string `yaml:"whitelist_path"`
BlacklistPath string `yaml:"blacklist_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) c := new(Config)
if err := yaml.Unmarshal(confBytes, c); err != nil { if err := yaml.Unmarshal(confBytes, c); err != nil {
return nil, fmt.Errorf("unable to deserialise configuration: %v", err) return nil, fmt.Errorf("unable to deserialise configuration: %v", err)

View File

@@ -10,11 +10,9 @@ import (
) )
func init() { func init() {
container.Register("list", builder{}) container.Register("list", build)
} }
type builder struct {}
type Config struct { type Config struct {
Whitelist []string `yaml:"whitelist"` Whitelist []string `yaml:"whitelist"`
Blacklist []string `yaml:"blacklist"` Blacklist []string `yaml:"blacklist"`
@@ -22,7 +20,7 @@ type Config struct {
var DUMMY struct{} var DUMMY struct{}
func (b builder) Build(confBytes []byte) (container.Container, error) { func build(confBytes []byte) (container.Container, error) {
c := new(Config) c := new(Config)
if err := yaml.Unmarshal(confBytes, c); err != nil { if err := yaml.Unmarshal(confBytes, c); err != nil {
return nil, fmt.Errorf("unable to deserialise configuration: %v", err) 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 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 return &hook{c}, nil
} else { } else {
return nil, err return nil, err
@@ -58,24 +58,23 @@ type hook struct {
hashContainer container.Container hashContainer container.Container
} }
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, _ *bittorrent.AnnounceResponse) (context.Context, error) {
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {
var err error var err error
if !h.hashContainer.Contains(req.InfoHash){ if !h.hashContainer.Contains(req.InfoHash) {
err = ErrTorrentUnapproved err = ErrTorrentUnapproved
} }
return ctx, err 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. // Scrapes don't require any protection.
return ctx, nil return ctx, nil
} }
func (h *hook) Stop() stop.Result { 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 st.Stop()
} }
return stop.AlreadyStopped return stop.AlreadyStopped