can now handle complexe mongo urls

This commit is contained in:
didier.amyot committed 2014-05-17 18:01:55 -04:00
1 parent 79da771597
commit 2add8284e6
6 files changed
+76 -16

No files matched your search

+34 -7
View File
@@ -8,10 +8,12 @@ import (
"labix.org/v2/mgo" "labix.org/v2/mgo"
"labix.org/v2/mgo/bson" "labix.org/v2/mgo/bson"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
) )
type VultureBackend struct { type VultureBackend struct {
MongoURL string
Client *mgo.Session Client *mgo.Session
Db *mgo.Database Db *mgo.Database
Collection *mgo.Collection Collection *mgo.Collection
@@ -20,20 +22,25 @@ type VultureBackend struct {
func GetVultureBackend(req *http.Request) (*VultureBackend, error) { func GetVultureBackend(req *http.Request) (*VultureBackend, error) {
vars := mux.Vars(req) vars := mux.Vars(req)
serverURL, ok := vars["server"] serverName, ok := vars["server"]
if !ok { if !ok {
return nil, errors.New("server field not specified") return nil, errors.New("server field not specified")
} }
client, err := shared.GetMongoClient(serverURL) mongoURL, ok := params.MongoServers.GetServerUrl(serverName)
if !ok {
return nil, errors.New("no server for asked name.")
}
client, err := shared.GetMongoClient(mongoURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
vb := &VultureBackend{Client: client} vb := &VultureBackend{Client: client, MongoURL: mongoURL}
vb.setDatabaseCollection(req) vb.setDatabaseAndCollection(req)
return vb, nil return vb, nil
} }
func (this *VultureBackend) setDatabaseCollection(req *http.Request) { func (this *VultureBackend) setDatabaseAndCollection(req *http.Request) {
vars := mux.Vars(req) vars := mux.Vars(req)
database, ok := vars["database"] database, ok := vars["database"]
if !ok { if !ok {
@@ -49,7 +56,23 @@ func (this *VultureBackend) setDatabaseCollection(req *http.Request) {
} }
func (this *VultureBackend) GetDataBases() ([]string, error) { func (this *VultureBackend) GetDataBases() ([]string, error) {
return this.Client.DatabaseNames()
dbs, err := this.Client.DatabaseNames()
if err == nil {
return dbs, nil
} else if err.Error() == "unauthorized" {
return this.getDatabaseFromMongoURL()
} else {
return nil, err
}
}
func (this *VultureBackend) getDatabaseFromMongoURL() ([]string, error) {
u, e := url.Parse(this.MongoURL)
if e != nil {
return nil, errors.New("cannot extract database from url")
}
return []string{u.Path[1:len(u.Path)]}, nil
} }
func (this *VultureBackend) GetCollections() ([]string, error) { func (this *VultureBackend) GetCollections() ([]string, error) {
@@ -121,7 +144,11 @@ func (this *VultureBackend) GetAllDocuments(query interface{}) (interface{}, err
} }
func getAvailableServers(w http.ResponseWriter, request *http.Request) (interface{}, error) { func getAvailableServers(w http.ResponseWriter, request *http.Request) (interface{}, error) {
return params.MongoServers, nil ret := make([]string, len(params.MongoServers))
for i, ms := range params.MongoServers {
ret[i] = ms.Name
}
return ret, nil
} }
func getAvailableDataBases(w http.ResponseWriter, request *http.Request) (interface{}, error) { func getAvailableDataBases(w http.ResponseWriter, request *http.Request) (interface{}, error) {
+1 -1
View File
@@ -1,7 +1,7 @@
root = this; root = this;
root.filters.filter 'escape', () -> root.filters.filter 'escape', () ->
return window.escape; return window.encodeURIComponent;
root.filters.filter 'replaceAllCommasBySpaces', () -> root.filters.filter 'replaceAllCommasBySpaces', () ->
+1
View File
@@ -13,6 +13,7 @@ type errorWrapper struct {
type ApiHandler func(w http.ResponseWriter, request *http.Request) (interface{}, error) type ApiHandler func(w http.ResponseWriter, request *http.Request) (interface{}, error)
func (this ApiHandler) ServeHTTP(w http.ResponseWriter, request *http.Request) { func (this ApiHandler) ServeHTTP(w http.ResponseWriter, request *http.Request) {
log.Println("request for", request.URL)
ret, err := this(w, request) ret, err := this(w, request)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
+22 -2
View File
@@ -14,10 +14,26 @@ import (
const PARAM_FILE string = "params.yml" const PARAM_FILE string = "params.yml"
const STATIC_DIR string = "client" const STATIC_DIR string = "client"
type MongoServer struct {
Name string `yaml:"name"`
Url string `yaml:"url"`
}
type MongoServers []*MongoServer
func (this MongoServers) GetServerUrl(name string) (url string, found bool) {
for _, server := range this {
if server.Name == name {
return server.Url, true
}
}
return "", false
}
type Params struct { type Params struct {
Port int `yaml:"Port,omitempty"` Port int `yaml:"Port,omitempty"`
ServingAddress string `yaml:"ServingAddress,omitempty"` ServingAddress string `yaml:"ServingAddress,omitempty"`
MongoServers []string `yaml:"MongoServers,omitempty"` MongoServers MongoServers `yaml:"MongoServers,omitempty"`
} }
func (this *Params) LoadFromYamlFile(fileName string) error { func (this *Params) LoadFromYamlFile(fileName string) error {
@@ -47,7 +63,11 @@ func (this *Params) LoadFromYamlData(data []byte) error {
return nil return nil
} }
var params = &Params{8000, "localhost", []string{"localhost:27017"}} func GetDefaultParams() *Params {
return &Params{8000, "localhost", MongoServers{&MongoServer{Name: "localhost", Url: "localhost:27017"}}}
}
var params = GetDefaultParams()
func init() { func init() {
if err := params.LoadFromYamlFile(PARAM_FILE); err != nil { if err := params.LoadFromYamlFile(PARAM_FILE); err != nil {
+10 -5
View File
@@ -6,19 +6,25 @@ import (
"net/http/httptest" "net/http/httptest"
"os" "os"
"path" "path"
"reflect"
"testing" "testing"
) )
func TestLoadFromYamlDataAndInitialParams(t *testing.T) { func TestInitialParams(t *testing.T) {
params := GetDefaultParams()
if params.Port != 8000 { if params.Port != 8000 {
t.Error("wrong port") t.Error("wrong port")
} }
if params.ServingAddress != "localhost" { if params.ServingAddress != "localhost" {
t.Error("wrong png dpi") t.Error("wrong png dpi")
} }
if !reflect.DeepEqual(params.MongoServers, []string{"localhost:27017"}) { if len(params.MongoServers) != 1 {
t.Fail()
t.Error("bad default number of mongo servers", len(params.MongoServers))
return
}
ms := params.MongoServers[0]
if ms.Name != "localhost" || ms.Url != "localhost:27017" {
t.Error("bad ms", ms)
} }
} }
@@ -28,7 +34,6 @@ func createMockStaticFile() (*os.File, error) {
return nil, err return nil, err
} }
return f, ioutil.WriteFile(f.Name(), []byte("hello world"), 0600) return f, ioutil.WriteFile(f.Name(), []byte("hello world"), 0600)
} }
func TestServingStaticFiles(t *testing.T) { func TestServingStaticFiles(t *testing.T) {
+8 -1
View File
@@ -12,5 +12,12 @@ Port: 8000
# should use `0.0.0.0` # should use `0.0.0.0`
ServingAddress: localhost ServingAddress: localhost
# The list of mongo servers. Each server is an object with a field *name* that will
# be shown in the web user interface and a *url* field that points to the mongo server
MongoServers: MongoServers:
- localhost:27017 -
name: localhost
url: localhost:27017
# -
# name: example
# url: mongodb://username:password@ds90210.mongolab.com:52652/database_name