Commit 6fd76857 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

elastigo: switched to use url address

parent bccfea83
...@@ -6,6 +6,8 @@ import ( ...@@ -6,6 +6,8 @@ import (
ds "github.com/jbenet/datastore.go" ds "github.com/jbenet/datastore.go"
"github.com/mattbaird/elastigo/api" "github.com/mattbaird/elastigo/api"
"github.com/mattbaird/elastigo/core" "github.com/mattbaird/elastigo/core"
"net/url"
"strings"
) )
// Currently, elastigo does not allow connecting to multiple elasticsearch // Currently, elastigo does not allow connecting to multiple elasticsearch
...@@ -14,16 +16,11 @@ import ( ...@@ -14,16 +16,11 @@ import (
// //
// Thus, we use a global static variable (GlobalInstance), and return an // Thus, we use a global static variable (GlobalInstance), and return an
// error if NewDatastore is called twice with different addresses. // error if NewDatastore is called twice with different addresses.
var GlobalInstance Address var GlobalInstance string
type Address struct {
Host string
Port int
}
// Datastore uses a standard Go map for internal storage. // Datastore uses a standard Go map for internal storage.
type Datastore struct { type Datastore struct {
addr Address url string
index string index string
// Elastic search does not allow slashes in their object ids, // Elastic search does not allow slashes in their object ids,
...@@ -31,19 +28,32 @@ type Datastore struct { ...@@ -31,19 +28,32 @@ type Datastore struct {
KeyHash func(ds.Key) string KeyHash func(ds.Key) string
} }
func NewDatastore(addr Address, index string) (*Datastore, error) { func NewDatastore(urlstr string) (*Datastore, error) {
if GlobalInstance.Host != "" && GlobalInstance != addr { if GlobalInstance != "" && GlobalInstance != urlstr {
return nil, fmt.Errorf("elastigo only allows one client. See godoc.") return nil, fmt.Errorf("elastigo only allows one client. See godoc.")
} }
api.Domain = addr.Host uf := "http://<host>:<port>/<index>"
if addr.Port > 0 { u, err := url.Parse(urlstr)
api.Port = fmt.Sprintf("%d", addr.Port) if err != nil {
return nil, fmt.Errorf("error parsing url: %s (%s)", urlstr, uf)
}
host := strings.Split(u.Host, ":")
api.Domain = host[0]
if len(host) > 1 {
api.Port = host[1]
}
index := strings.Trim(u.Path, "/")
if strings.Contains(index, "/") {
e := "elastigo index cannot have slashes: %s (%s -> %s)"
return nil, fmt.Errorf(e, index, urlstr, uf)
} }
GlobalInstance = addr GlobalInstance = urlstr
return &Datastore{ return &Datastore{
addr: addr, url: urlstr,
index: index, index: index,
KeyHash: BlakeKeyHash, KeyHash: BlakeKeyHash,
}, nil }, nil
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment