DriverConfig.Params enables generic db parameters

This commit is contained in:
Jimmy Zelinskie 2014-07-03 17:37:13 -04:00
parent b1311b3118
commit ff5dc0920d
4 changed files with 16 additions and 29 deletions

View file

@ -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,

View file

@ -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.

View file

@ -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.

View file

@ -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
}