main: add CreateHooks() method for ConfigFile
This change simplifies middleware.Logic to having only one list of PreHooks and one list of PostHooks.
This commit is contained in:
parent
6e790eed74
commit
e39da6b4e6
3 changed files with 59 additions and 31 deletions
middleware
|
@ -18,30 +18,20 @@ type Config struct {
|
|||
|
||||
var _ frontend.TrackerLogic = &Logic{}
|
||||
|
||||
func NewLogic(config Config, peerStore storage.PeerStore, announcePreHooks, announcePostHooks, scrapePreHooks, scrapePostHooks []Hook) *Logic {
|
||||
func NewLogic(cfg Config, peerStore storage.PeerStore, preHooks, postHooks []Hook) *Logic {
|
||||
l := &Logic{
|
||||
announceInterval: config.AnnounceInterval,
|
||||
peerStore: peerStore,
|
||||
announcePreHooks: announcePreHooks,
|
||||
announcePostHooks: announcePostHooks,
|
||||
scrapePreHooks: scrapePreHooks,
|
||||
scrapePostHooks: scrapePostHooks,
|
||||
announceInterval: cfg.AnnounceInterval,
|
||||
peerStore: peerStore,
|
||||
preHooks: preHooks,
|
||||
postHooks: postHooks,
|
||||
}
|
||||
|
||||
if len(l.announcePreHooks) == 0 {
|
||||
l.announcePreHooks = []Hook{nopHook{}}
|
||||
if len(l.preHooks) == 0 {
|
||||
l.preHooks = []Hook{nopHook{}}
|
||||
}
|
||||
|
||||
if len(l.announcePostHooks) == 0 {
|
||||
l.announcePostHooks = []Hook{nopHook{}}
|
||||
}
|
||||
|
||||
if len(l.scrapePreHooks) == 0 {
|
||||
l.scrapePreHooks = []Hook{nopHook{}}
|
||||
}
|
||||
|
||||
if len(l.scrapePostHooks) == 0 {
|
||||
l.scrapePostHooks = []Hook{nopHook{}}
|
||||
if len(l.postHooks) == 0 {
|
||||
l.postHooks = []Hook{nopHook{}}
|
||||
}
|
||||
|
||||
return l
|
||||
|
@ -50,12 +40,10 @@ func NewLogic(config Config, peerStore storage.PeerStore, announcePreHooks, anno
|
|||
// Logic is an implementation of the TrackerLogic that functions by
|
||||
// executing a series of middleware hooks.
|
||||
type Logic struct {
|
||||
announceInterval time.Duration
|
||||
peerStore storage.PeerStore
|
||||
announcePreHooks []Hook
|
||||
announcePostHooks []Hook
|
||||
scrapePreHooks []Hook
|
||||
scrapePostHooks []Hook
|
||||
announceInterval time.Duration
|
||||
peerStore storage.PeerStore
|
||||
preHooks []Hook
|
||||
postHooks []Hook
|
||||
}
|
||||
|
||||
// HandleAnnounce generates a response for an Announce.
|
||||
|
@ -63,7 +51,7 @@ func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequ
|
|||
resp := &bittorrent.AnnounceResponse{
|
||||
Interval: l.announceInterval,
|
||||
}
|
||||
for _, h := range l.announcePreHooks {
|
||||
for _, h := range l.preHooks {
|
||||
if err := h.HandleAnnounce(ctx, req, resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -75,7 +63,7 @@ func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequ
|
|||
// AfterAnnounce does something with the results of an Announce after it has
|
||||
// been completed.
|
||||
func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) {
|
||||
for _, h := range l.announcePostHooks {
|
||||
for _, h := range l.postHooks {
|
||||
if err := h.HandleAnnounce(ctx, req, resp); err != nil {
|
||||
log.Println("chihaya: post-announce hooks failed:", err.Error())
|
||||
return
|
||||
|
@ -88,7 +76,7 @@ func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest)
|
|||
resp := &bittorrent.ScrapeResponse{
|
||||
Files: make(map[bittorrent.InfoHash]bittorrent.Scrape),
|
||||
}
|
||||
for _, h := range l.scrapePreHooks {
|
||||
for _, h := range l.preHooks {
|
||||
if err := h.HandleScrape(ctx, req, resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -100,7 +88,7 @@ func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest)
|
|||
// AfterScrape does something with the results of a Scrape after it has been
|
||||
// completed.
|
||||
func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) {
|
||||
for _, h := range l.scrapePostHooks {
|
||||
for _, h := range l.postHooks {
|
||||
if err := h.HandleScrape(ctx, req, resp); err != nil {
|
||||
log.Println("chihaya: post-scrape hooks failed:", err.Error())
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue