diff --git a/config/config.go b/config/config.go index 7c20e3a..ecd3eb3 100644 --- a/config/config.go +++ b/config/config.go @@ -13,9 +13,7 @@ import ( ) // Duration wraps a time.Duration and adds JSON marshalling. -type Duration struct { - time.Duration -} +type Duration struct{ time.Duration } // MarshalJSON transforms a duration into JSON. func (d *Duration) MarshalJSON() ([]byte, error) { @@ -33,18 +31,8 @@ func (d *Duration) UnmarshalJSON(b []byte) error { // DriverConfig is the configuration used to connect to a tracker.Driver or // a backend.Driver. type DriverConfig struct { - Driver string `json:"driver"` - Network string `json:"network` - Host string `json:"host"` - Port string `json:"port"` - Username string `json:"user"` - Password string `json:"pass"` - Schema string `json:"schema,omitempty"` - Encoding string `json:"encoding,omitempty"` - Prefix string `json:"prefix,omitempty"` - - MaxIdleConns int `json:"max_idle_conns,omitempty"` - IdleTimeout *Duration `json:"idle_timeout,omitempty"` + Name string `json:"driver"` + Params map[string]string `json:"params,omitempty"` } // Config is a configuration for a Server. @@ -66,10 +54,10 @@ type Config struct { var DefaultConfig = Config{ Addr: ":6881", Tracker: DriverConfig{ - Driver: "mock", + Name: "mock", }, Backend: DriverConfig{ - Driver: "mock", + Name: "mock", }, Private: false, Freeleech: false, diff --git a/drivers/backend/backend.go b/drivers/backend/backend.go index 4c0f17e..40e4ae4 100644 --- a/drivers/backend/backend.go +++ b/drivers/backend/backend.go @@ -19,7 +19,7 @@ var drivers = make(map[string]Driver) // Driver represents an interface to a long-running connection with a // consistent data store. type Driver interface { - New(*config.DriverConfig) Conn + New(*config.DriverConfig) (Conn, error) } // Register makes a database driver available by the provided name. @@ -36,16 +36,15 @@ func Register(name string, driver Driver) { } // Open creates a connection specified by a models configuration. -func Open(conf *config.DriverConfig) (Conn, error) { - driver, ok := drivers[conf.Driver] +func Open(cfg *config.DriverConfig) (Conn, error) { + driver, ok := drivers[cfg.Name] if !ok { return nil, fmt.Errorf( "backend: unknown driver %q (forgotten import?)", - conf.Driver, + cfg.Name, ) } - pool := driver.New(conf) - return pool, nil + return driver.New(cfg) } // Conn represents a connection to the data store. diff --git a/drivers/backend/mock/driver.go b/drivers/backend/mock/driver.go index 20b7fca..2850bd1 100644 --- a/drivers/backend/mock/driver.go +++ b/drivers/backend/mock/driver.go @@ -24,8 +24,8 @@ type Mock struct { deltaHistoryM sync.RWMutex } -func (d *driver) New(conf *config.DriverConfig) backend.Conn { - return &Mock{} +func (d *driver) New(conf *config.DriverConfig) (backend.Conn, error) { + return &Mock{}, nil } // Close returns nil. diff --git a/drivers/tracker/tracker.go b/drivers/tracker/tracker.go index 2d85d0d..ede01b1 100644 --- a/drivers/tracker/tracker.go +++ b/drivers/tracker/tracker.go @@ -47,15 +47,15 @@ func Register(name string, driver Driver) { } // Open creates a pool of data store connections specified by a models configuration. -func Open(conf *config.DriverConfig) (Pool, error) { - driver, ok := drivers[conf.Driver] +func Open(cfg *config.DriverConfig) (Pool, error) { + driver, ok := drivers[cfg.Name] if !ok { return nil, fmt.Errorf( "unknown driver %q (forgotten import?)", - conf.Driver, + cfg.Name, ) } - pool := driver.New(conf) + pool := driver.New(cfg) return pool, nil }