storage: add storage interface and registration

This also fixes bugs in the Hooks registration.
This commit is contained in:
Jimmy Zelinskie
2016-08-04 15:54:30 -04:00
parent ae36a14949
commit 8a2d894191
2 changed files with 102 additions and 5 deletions

View File

@@ -16,9 +16,6 @@ package trakr
import "github.com/jzelinskie/trakr/bittorrent"
// HookConstructor is a function used to create a new instance of a Hook.
type HookConstructor func(interface{}) (Hook, error)
// Hook abstracts the concept of anything that needs to interact with a
// BitTorrent client's request and response to a BitTorrent tracker.
type Hook interface {
@@ -26,6 +23,9 @@ type Hook interface {
HandleScrape(context.Context, bittorrent.ScrapeRequest, bittorrent.ScrapeResponse) error
}
// HookConstructor is a function used to create a new instance of a Hook.
type HookConstructor func(interface{}) (Hook, error)
var preHooks = make(map[string]HookConstructor)
// RegisterPreHook makes a HookConstructor available by the provided name.
@@ -44,7 +44,7 @@ func RegisterPreHook(name string, con HookConstructor) {
// NewPreHook creates an instance of the given PreHook by name.
func NewPreHook(name string, config interface{}) (Hook, error) {
con := preHooks[name]
con, ok := preHooks[name]
if !ok {
return nil, fmt.Errorf("trakr: unknown PreHook %q (forgotten import?)", name)
}
@@ -69,7 +69,7 @@ func RegisterPostHook(name string, con HookConstructor) {
// NewPostHook creates an instance of the given PostHook by name.
func NewPostHook(name string, config interface{}) (Hook, error) {
con := preHooks[name]
con, ok := preHooks[name]
if !ok {
return nil, fmt.Errorf("trakr: unknown PostHook %q (forgotten import?)", name)
}