Пример #1
0
//
// Returns all collections in database
//
func (d *Resource) DatabaseCollectionsHandler(req *restful.Request, resp *restful.Response) {
	// Mongo session
	session, needclose, err := d.SessMng.Get("local") //getParam("alias", req)
	if err != nil {
		WriteError(err, resp)
		return
	}
	if needclose {
		defer session.Close()
	}

	// Database request parameter
	dbname := "loudshout" //getParam("database", req)

	// Get collections from database
	collections, err := session.DB(dbname).CollectionNames()
	if err != nil {
		WriteError(err, resp)
		return
	}

	if collections == nil {
		err = errors.New("Unknown database: " + dbname)
		WriteError(err, resp)
		return
	}

	// Write collections back to client
	WriteResponse(collections, resp)
}
Пример #2
0
// GET http://localhost:8181/stats/local/landskape/systems
func (s *Resource) CollectionStatisticsHandler(req *restful.Request, resp *restful.Response) {
	// Mongo session
	session, needsClose, err := s.SessMng.Get(req.PathParameter("alias"))
	if err != nil {
		WriteError(err, resp)
		return
	}
	if needsClose {
		defer session.Close()
	}

	var (
		// Request parameters
		collection = req.PathParameter("collection")
		database   = req.PathParameter("database")
		// Statistics result
		result = bson.M{}
	)

	// Get statistics for collection
	err = session.DB(database).Run(bson.M{"collStats": collection, "scale": 1}, &result)
	if err != nil {
		WriteError(err, resp)
		return
	}

	// Write result to console
	fmt.Printf("stats result:%#v", result)

	// Write result back to client
	WriteResponse(result, resp)
}