first commit. Way too late.
This commit is contained in:
commit
da7cacb57e
35 files changed
+1355
No files matched your search
@@ -0,0 +1,140 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bitbucket.org/damyot/vulture/shared"
|
||||
"errors"
|
||||
"github.com/gorilla/mux"
|
||||
"labix.org/v2/mgo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type VultureBackend struct {
|
||||
Client *mgo.Session
|
||||
Db *mgo.Database
|
||||
Collection *mgo.Collection
|
||||
}
|
||||
|
||||
func GetVultureBackend(req *http.Request) (*VultureBackend, error) {
|
||||
vars := mux.Vars(req)
|
||||
serverURL, ok := vars["server"]
|
||||
if !ok {
|
||||
return nil, errors.New("server field not specified")
|
||||
}
|
||||
client, err := shared.GetMongoClient(serverURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
vb := &VultureBackend{Client: client}
|
||||
vb.setDatabaseCollection(req)
|
||||
return vb, nil
|
||||
}
|
||||
|
||||
func (this *VultureBackend) setDatabaseCollection(req *http.Request) {
|
||||
vars := mux.Vars(req)
|
||||
database, ok := vars["database"]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
this.Db = this.Client.DB(database)
|
||||
collection, ok := vars["collection"]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
this.Collection = this.Db.C(collection)
|
||||
|
||||
}
|
||||
|
||||
func (this *VultureBackend) GetDataBases() ([]string, error) {
|
||||
return this.Client.DatabaseNames()
|
||||
}
|
||||
|
||||
func (this *VultureBackend) GetCollections() ([]string, error) {
|
||||
if this.Db == nil {
|
||||
return nil, errors.New("no database set")
|
||||
}
|
||||
return this.Db.CollectionNames()
|
||||
}
|
||||
|
||||
func (this *VultureBackend) getIndexes() (interface{}, error) {
|
||||
idxs, err := this.Collection.Indexes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret := make([]map[string]interface{}, len(idxs))
|
||||
for i, idx := range idxs {
|
||||
ret[i] = map[string]interface{}{"name": idx.Name, "keys": idx.Key}
|
||||
}
|
||||
return ret, nil
|
||||
|
||||
}
|
||||
|
||||
func (this *VultureBackend) getMetaData(query *mgo.Query) (map[string]interface{}, error) {
|
||||
meta := make(map[string]interface{})
|
||||
var err error
|
||||
if meta["count"], err = query.Count(); err != nil {
|
||||
return meta, err
|
||||
}
|
||||
if meta["indexes"], err = this.getIndexes(); err != nil {
|
||||
return meta, err
|
||||
}
|
||||
|
||||
return meta, nil
|
||||
|
||||
}
|
||||
|
||||
func (this *VultureBackend) GetDocumentByIndex(index int) (map[string]interface{}, error) {
|
||||
doc := make(map[string]interface{})
|
||||
query := this.Collection.Find(nil)
|
||||
if err := query.Skip(index).One(&doc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.wrapDocumentWithMetadata(doc, query)
|
||||
}
|
||||
|
||||
func (this *VultureBackend) wrapDocumentWithMetadata(doc map[string]interface{}, query *mgo.Query) (map[string]interface{}, error) {
|
||||
ret := make(map[string]interface{})
|
||||
var err error
|
||||
ret["document"] = doc
|
||||
if ret["meta"], err = this.getMetaData(query); err != nil {
|
||||
return ret, err
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func getAvailableServers(w http.ResponseWriter, request *http.Request) (interface{}, error) {
|
||||
return params.MongoServers, nil
|
||||
}
|
||||
|
||||
func getAvailableDataBases(w http.ResponseWriter, request *http.Request) (interface{}, error) {
|
||||
vb, err := GetVultureBackend(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return vb.GetDataBases()
|
||||
}
|
||||
|
||||
func getAvailableCollections(w http.ResponseWriter, request *http.Request) (interface{}, error) {
|
||||
vb, err := GetVultureBackend(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return vb.GetCollections()
|
||||
}
|
||||
|
||||
func getDocumentByIndex(w http.ResponseWriter, request *http.Request) (interface{}, error) {
|
||||
vb, err := GetVultureBackend(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
vars := mux.Vars(request)
|
||||
indexAsString, ok := vars["index"]
|
||||
if !ok {
|
||||
return nil, errors.New("index field not specified")
|
||||
}
|
||||
index, err := strconv.Atoi(indexAsString)
|
||||
if err != nil {
|
||||
return nil, errors.New("index field is not an inteder ( " + indexAsString + ")")
|
||||
}
|
||||
return vb.GetDocumentByIndex(index)
|
||||
}
|
||||
Reference in new issue
Block a user