Allow configurable addresses for everything

This commit is contained in:
Asim
2015-05-16 00:34:02 +01:00
parent c77be7c571
commit 0e7bd77f4c
13 changed files with 143 additions and 41 deletions

View File

@@ -46,8 +46,13 @@ func (c *ConsulStore) NewItem(key string, value []byte) Item {
}
}
func NewConsulStore() Store {
client, _ := consul.NewClient(consul.DefaultConfig())
func NewConsulStore(addrs []string, opts ...Options) Store {
config := consul.DefaultConfig()
if len(addrs) > 0 {
config.Address = addrs[0]
}
client, _ := consul.NewClient(config)
return &ConsulStore{
Client: client,

View File

@@ -43,8 +43,12 @@ func (e *EtcdStore) NewItem(key string, value []byte) Item {
}
}
func NewEtcdStore() Store {
client := etcd.NewClient([]string{})
func NewEtcdStore(addrs []string, opts ...Options) Store {
if len(addrs) == 0 {
addrs = []string{"127.0.0.1:2379"}
}
client := etcd.NewClient(addrs)
return &EtcdStore{
Client: client,

View File

@@ -2,7 +2,6 @@ package store
import (
"errors"
"os"
mc "github.com/bradfitz/gomemcache/memcache"
)
@@ -47,19 +46,11 @@ func (m *MemcacheStore) NewItem(key string, value []byte) Item {
}
}
func NewMemcacheStore() Store {
server := os.Getenv("MEMCACHED_SERVICE_HOST")
port := os.Getenv("MEMCACHED_SERVICE_PORT")
if len(server) == 0 {
server = "127.0.0.1"
func NewMemcacheStore(addrs []string, opts ...Options) Store {
if len(addrs) == 0 {
addrs = []string{"127.0.0.1:11211"}
}
if len(port) == 0 {
port = "11211"
}
return &MemcacheStore{
Client: mc.New(server + ":" + port),
Client: mc.New(addrs...),
}
}

View File

@@ -41,7 +41,7 @@ func (m *MemoryStore) NewItem(key string, value []byte) Item {
}
}
func NewMemoryStore() Store {
func NewMemoryStore(addrs []string, opts ...Options) Store {
return &MemoryStore{
store: make(map[string]Item),
}

View File

@@ -7,8 +7,12 @@ type Store interface {
NewItem(string, []byte) Item
}
type options struct{}
type Options func(*options)
var (
DefaultStore = NewConsulStore()
DefaultStore = NewConsulStore([]string{})
)
func Get(key string) (Item, error) {