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
|
@ -10,9 +10,15 @@ import (
|
||||||
httpfrontend "github.com/chihaya/chihaya/frontend/http"
|
httpfrontend "github.com/chihaya/chihaya/frontend/http"
|
||||||
udpfrontend "github.com/chihaya/chihaya/frontend/udp"
|
udpfrontend "github.com/chihaya/chihaya/frontend/udp"
|
||||||
"github.com/chihaya/chihaya/middleware"
|
"github.com/chihaya/chihaya/middleware"
|
||||||
|
"github.com/chihaya/chihaya/middleware/jwt"
|
||||||
"github.com/chihaya/chihaya/storage/memory"
|
"github.com/chihaya/chihaya/storage/memory"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type hookConfig struct {
|
||||||
|
Name string `yaml:"name"`
|
||||||
|
Config interface{} `yaml:"config"`
|
||||||
|
}
|
||||||
|
|
||||||
// ConfigFile represents a namespaced YAML configation file.
|
// ConfigFile represents a namespaced YAML configation file.
|
||||||
type ConfigFile struct {
|
type ConfigFile struct {
|
||||||
MainConfigBlock struct {
|
MainConfigBlock struct {
|
||||||
|
@ -21,6 +27,8 @@ type ConfigFile struct {
|
||||||
HTTPConfig httpfrontend.Config `yaml:"http"`
|
HTTPConfig httpfrontend.Config `yaml:"http"`
|
||||||
UDPConfig udpfrontend.Config `yaml:"udp"`
|
UDPConfig udpfrontend.Config `yaml:"udp"`
|
||||||
Storage memory.Config `yaml:"storage"`
|
Storage memory.Config `yaml:"storage"`
|
||||||
|
PreHooks []hookConfig `yaml:"prehooks"`
|
||||||
|
PostHooks []hookConfig `yaml:"posthooks"`
|
||||||
} `yaml:"chihaya"`
|
} `yaml:"chihaya"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,3 +60,31 @@ func ParseConfigFile(path string) (*ConfigFile, error) {
|
||||||
|
|
||||||
return &cfgFile, nil
|
return &cfgFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateHooks creates instances of Hooks for all of the PreHooks and PostHooks
|
||||||
|
// configured in a ConfigFile.
|
||||||
|
func (cfg ConfigFile) CreateHooks() (preHooks, postHooks []middleware.Hook, err error) {
|
||||||
|
for _, hookCfg := range cfg.MainConfigBlock.PreHooks {
|
||||||
|
cfgBytes, err := yaml.Marshal(hookCfg.Config)
|
||||||
|
if err != nil {
|
||||||
|
panic("failed to remarshal valid YAML")
|
||||||
|
}
|
||||||
|
|
||||||
|
switch hookCfg.Name {
|
||||||
|
case "jwt":
|
||||||
|
var jwtCfg jwt.Config
|
||||||
|
err := yaml.Unmarshal(cfgBytes, &jwtCfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, errors.New("invalid JWT middleware config" + err.Error())
|
||||||
|
}
|
||||||
|
preHooks = append(preHooks, jwt.NewHook(jwtCfg))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, hookCfg := range cfg.MainConfigBlock.PostHooks {
|
||||||
|
switch hookCfg.Name {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -54,8 +54,12 @@ func rootCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO create Hooks
|
preHooks, postHooks, err := configFile.CreateHooks()
|
||||||
logic := middleware.NewLogic(cfg.Config, peerStore, nil, nil, nil, nil)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logic := middleware.NewLogic(cfg.Config, peerStore, preHooks, postHooks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,30 +18,20 @@ type Config struct {
|
||||||
|
|
||||||
var _ frontend.TrackerLogic = &Logic{}
|
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{
|
l := &Logic{
|
||||||
announceInterval: config.AnnounceInterval,
|
announceInterval: cfg.AnnounceInterval,
|
||||||
peerStore: peerStore,
|
peerStore: peerStore,
|
||||||
announcePreHooks: announcePreHooks,
|
preHooks: preHooks,
|
||||||
announcePostHooks: announcePostHooks,
|
postHooks: postHooks,
|
||||||
scrapePreHooks: scrapePreHooks,
|
|
||||||
scrapePostHooks: scrapePostHooks,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(l.announcePreHooks) == 0 {
|
if len(l.preHooks) == 0 {
|
||||||
l.announcePreHooks = []Hook{nopHook{}}
|
l.preHooks = []Hook{nopHook{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(l.announcePostHooks) == 0 {
|
if len(l.postHooks) == 0 {
|
||||||
l.announcePostHooks = []Hook{nopHook{}}
|
l.postHooks = []Hook{nopHook{}}
|
||||||
}
|
|
||||||
|
|
||||||
if len(l.scrapePreHooks) == 0 {
|
|
||||||
l.scrapePreHooks = []Hook{nopHook{}}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(l.scrapePostHooks) == 0 {
|
|
||||||
l.scrapePostHooks = []Hook{nopHook{}}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return l
|
return l
|
||||||
|
@ -52,10 +42,8 @@ func NewLogic(config Config, peerStore storage.PeerStore, announcePreHooks, anno
|
||||||
type Logic struct {
|
type Logic struct {
|
||||||
announceInterval time.Duration
|
announceInterval time.Duration
|
||||||
peerStore storage.PeerStore
|
peerStore storage.PeerStore
|
||||||
announcePreHooks []Hook
|
preHooks []Hook
|
||||||
announcePostHooks []Hook
|
postHooks []Hook
|
||||||
scrapePreHooks []Hook
|
|
||||||
scrapePostHooks []Hook
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleAnnounce generates a response for an Announce.
|
// HandleAnnounce generates a response for an Announce.
|
||||||
|
@ -63,7 +51,7 @@ func (l *Logic) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequ
|
||||||
resp := &bittorrent.AnnounceResponse{
|
resp := &bittorrent.AnnounceResponse{
|
||||||
Interval: l.announceInterval,
|
Interval: l.announceInterval,
|
||||||
}
|
}
|
||||||
for _, h := range l.announcePreHooks {
|
for _, h := range l.preHooks {
|
||||||
if err := h.HandleAnnounce(ctx, req, resp); err != nil {
|
if err := h.HandleAnnounce(ctx, req, resp); err != nil {
|
||||||
return nil, err
|
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
|
// AfterAnnounce does something with the results of an Announce after it has
|
||||||
// been completed.
|
// been completed.
|
||||||
func (l *Logic) AfterAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) {
|
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 {
|
if err := h.HandleAnnounce(ctx, req, resp); err != nil {
|
||||||
log.Println("chihaya: post-announce hooks failed:", err.Error())
|
log.Println("chihaya: post-announce hooks failed:", err.Error())
|
||||||
return
|
return
|
||||||
|
@ -88,7 +76,7 @@ func (l *Logic) HandleScrape(ctx context.Context, req *bittorrent.ScrapeRequest)
|
||||||
resp := &bittorrent.ScrapeResponse{
|
resp := &bittorrent.ScrapeResponse{
|
||||||
Files: make(map[bittorrent.InfoHash]bittorrent.Scrape),
|
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 {
|
if err := h.HandleScrape(ctx, req, resp); err != nil {
|
||||||
return nil, err
|
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
|
// AfterScrape does something with the results of a Scrape after it has been
|
||||||
// completed.
|
// completed.
|
||||||
func (l *Logic) AfterScrape(ctx context.Context, req *bittorrent.ScrapeRequest, resp *bittorrent.ScrapeResponse) {
|
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 {
|
if err := h.HandleScrape(ctx, req, resp); err != nil {
|
||||||
log.Println("chihaya: post-scrape hooks failed:", err.Error())
|
log.Println("chihaya: post-scrape hooks failed:", err.Error())
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue