Commit c5235aa0 authored by Loïck Bonniot's avatar Loïck Bonniot

Remove env vars from mgdb library

parent a7c4c50d
Pipeline #165 passed with stage
......@@ -16,8 +16,8 @@ var (
keySize, validity int
pid *authority.PlatformID
// MongoDB connection
dbName, dbEnvVarName string
dbManager *mgdb.MongoManager
dbURI string
dbManager *mgdb.MongoManager
)
func init() {
......@@ -33,8 +33,7 @@ func init() {
flag.IntVar(&keySize, "keySize", 512, "Encoding size for the private key")
flag.IntVar(&validity, "validity", 21, "Root certificate's validity duration (days)")
flag.StringVar(&dbName, "dbn", "myDatabase", "Name of the mongo database to connect to")
flag.StringVar(&dbEnvVarName, "dbenv", mgdb.DefaultDBUrl, "Name of the environment variable containing the server url in standard MongoDB format")
flag.StringVar(&dbURI, "db", "mongodb://localhost/dfss", "Name of the environment variable containing the server url in standard MongoDB format")
flag.Usage = func() {
fmt.Println("DFSS platform v" + dfss.Version)
......@@ -46,7 +45,7 @@ func init() {
fmt.Println("\nThe commands are:")
fmt.Println(" init [cn, country, keySize, org, path, unit, validity]")
fmt.Println(" create and save the platform's private key and root certificate")
fmt.Println(" start [path, dbn, dbenv]")
fmt.Println(" start [path, db]")
fmt.Println(" start the platform after loading its private key and root certificate")
fmt.Println(" help print this help")
fmt.Println(" version print dfss client version")
......@@ -82,7 +81,7 @@ func main() {
// TODO: use pid
_ = pid
dbManager, err := mgdb.NewManager(dbName, dbEnvVarName)
dbManager, err := mgdb.NewManager(dbURI)
if err != nil {
fmt.Println("An error occured during the connection to Mongo DB")
fmt.Println(err)
......
......@@ -5,14 +5,14 @@ It uses the mgo driver, but aims at simplifying the queries to the database
## Mongo Manager ##
The struct handling the connection is MongoManager. It requires the environment variable MONGOHQ_URL in order to initialize a connection : it is the uri containing the informations to connect to mongo.
The struct handling the connection is MongoManager. It requires an uri in order to initialize a connection : it is the uri containing the informations to connect to mongo.
For example, in a test environment, we may have :
MONGOHQ_URL=localhost (require a mongo instance running on default port 27017)
uri=localhost (require a mongo instance running on default port 27017)
In a prod environment however, it will more likely be :
MONGOHQ_URL=adm1n:AStr0ngPassw0rd@10.0.4.4:27017
uri=adm1n:AStr0ngPassw0rd@10.0.4.4:27017
## Declaring an entity ##
......
......@@ -3,26 +3,8 @@ package mgdb
import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"os"
)
const DefaultDBUrl = "MGDB_URL"
// errorConnection represents an error to be thrown upon connection
type errorConnection struct {
s string
}
// Return the string contained in the error
func (e *errorConnection) Error() string {
return e.s
}
// Creates a new error with the given message
func newErrorConnection(message string) error {
return &errorConnection{message}
}
// MongoManager is aimed at handling the Mongo connection through the mgo driver
type MongoManager struct {
......@@ -35,14 +17,9 @@ type MongoManager struct {
Collections map[string]*MongoCollection
}
// NewManager a new Manager, the environment variable 'connection' needs to be set
// NewManager a new Manager, the parameter `uri` needs to be set
// up with mongo uri, else it throws an error
func NewManager(database, connection string) (*MongoManager, error) {
uri := os.Getenv(connection)
if uri == "" {
err := newErrorConnection("No uri provided, please set the " + connection + " environment variable to connect to mongo")
return nil, err
}
func NewManager(uri string) (*MongoManager, error) {
sess, err := mgo.Dial(uri)
......@@ -50,7 +27,7 @@ func NewManager(database, connection string) (*MongoManager, error) {
return nil, err
}
db := sess.DB(database)
db := sess.DB("") // Use name provided in connection string, or test by default
return &MongoManager{
sess,
db,
......
......@@ -22,18 +22,16 @@ type hand struct {
var collection *MongoCollection
var manager *MongoManager
var err error
var dbURI string
func TestMain(m *testing.M) {
// Setup
fmt.Println("Try to connect to : " + os.Getenv(DefaultDBUrl))
db := os.Getenv(DefaultDBUrl)
if db == "" {
db = "demo"
dbURI = os.Getenv("DFSS_MONGO_URI")
if dbURI == "" {
dbURI = "mongodb://localhost/dfss"
}
manager, err = NewManager(db, DefaultDBUrl)
manager, err = NewManager(dbURI)
collection = manager.Get("demo")
// Run
......@@ -177,7 +175,6 @@ func TestMongoDeleteByID(t *testing.T) {
func ExampleMongoManager() {
//Define an animal to be use in further tests
type animal struct {
Name string `key:"_id" bson:"_id"`
Race string `key:"race" bson:"race"`
......@@ -185,7 +182,7 @@ func ExampleMongoManager() {
}
//Initializes a MongoManager for the 'demo' database
manager, err := NewManager("demo", DefaultDBUrl)
manager, err := NewManager(dbURI)
if err != nil { /* Handle error */
}
......
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