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. // Duration wraps a time.Duration and adds JSON marshalling.
type Duration struct { type Duration struct{ time.Duration }
time.Duration
}
// MarshalJSON transforms a duration into JSON. // MarshalJSON transforms a duration into JSON.
func (d *Duration) MarshalJSON() ([]byte, error) { 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 // DriverConfig is the configuration used to connect to a tracker.Driver or
// a backend.Driver. // a backend.Driver.
type DriverConfig struct { type DriverConfig struct {
Driver string `json:"driver"` Name string `json:"driver"`
Network string `json:"network` Params map[string]string `json:"params,omitempty"`
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"`
} }
// Config is a configuration for a Server. // Config is a configuration for a Server.
@ -66,10 +54,10 @@ type Config struct {
var DefaultConfig = Config{ var DefaultConfig = Config{
Addr: ":6881", Addr: ":6881",
Tracker: DriverConfig{ Tracker: DriverConfig{
Driver: "mock", Name: "mock",
}, },
Backend: DriverConfig{ Backend: DriverConfig{
Driver: "mock", Name: "mock",
}, },
Private: false, Private: false,
Freeleech: 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 // Driver represents an interface to a long-running connection with a
// consistent data store. // consistent data store.
type Driver interface { type Driver interface {
New(*config.DriverConfig) Conn New(*config.DriverConfig) (Conn, error)
} }
// Register makes a database driver available by the provided name. // 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. // Open creates a connection specified by a models configuration.
func Open(conf *config.DriverConfig) (Conn, error) { func Open(cfg *config.DriverConfig) (Conn, error) {
driver, ok := drivers[conf.Driver] driver, ok := drivers[cfg.Name]
if !ok { if !ok {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"backend: unknown driver %q (forgotten import?)", "backend: unknown driver %q (forgotten import?)",
conf.Driver, cfg.Name,
) )
} }
pool := driver.New(conf) return driver.New(cfg)
return pool, nil
} }
// Conn represents a connection to the data store. // Conn represents a connection to the data store.

View file

@ -24,8 +24,8 @@ type Mock struct {
deltaHistoryM sync.RWMutex deltaHistoryM sync.RWMutex
} }
func (d *driver) New(conf *config.DriverConfig) backend.Conn { func (d *driver) New(conf *config.DriverConfig) (backend.Conn, error) {
return &Mock{} return &Mock{}, nil
} }
// Close returns 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. // Open creates a pool of data store connections specified by a models configuration.
func Open(conf *config.DriverConfig) (Pool, error) { func Open(cfg *config.DriverConfig) (Pool, error) {
driver, ok := drivers[conf.Driver] driver, ok := drivers[cfg.Name]
if !ok { if !ok {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"unknown driver %q (forgotten import?)", "unknown driver %q (forgotten import?)",
conf.Driver, cfg.Name,
) )
} }
pool := driver.New(conf) pool := driver.New(cfg)
return pool, nil return pool, nil
} }