Awesome
nigroni-mgo-session
nigroni-mgo-session
is a simple Negroni middleware/handler for easy mgo session management in requests. It handles creating a new mongo session in the begning of a request and handles closing the mongo session at the end of request.
Getting Started
-
Before getting started make sure we have a copy of MongoDB running and is accessible. Running it locally would be recommended, but if thats not an option you can run on a remote server. Makes sure the mongo server is accessible, and you have a Mongo collection created within the database. If you haven't installed
nigroni-mgo-session
, you can run the below command to install it.go get github.com/joeljames/nigroni-mgo-session
-
This package will be used with nigroni and mgo. If you haven't installed the dependencies you could install run the below command.
-
Now lets add an example which demonstrates the usage of this middleware. I have added comments to the example to explain the usage.
-
Now, that you have a running instance of MongoDB you can Shell into mongo by running the command below.
mongo
-
Create your database and collection and insert some data into the collection.
use db db.createCollection("widget"); db.widget.insert({'id': '123'}); db.widget.insert({'id': '456'});
-
Set your
DATABASE_URL
andDATABASE_NAME
environment variable.export DATABASE_URL=mongodb://username:password@localhost:27017/db export DATABASE_NAME=db
-
If you have the package and dependency downloaded, you could simple copy the above example to a file
example.go
and then run the commandgo run example.go
to start up the server.
package main import ( "fmt" "net/http" "os" "github.com/codegangsta/negroni" nigronimgosession "github.com/joeljames/nigroni-mgo-session" ) func main() { // Use the MongoDB `DATABASE_URL` from the env dbURL := os.Getenv("DATABASE_URL") // Use the MongoDB `DATABASE_NAME` from the env dbName := os.Getenv("DATABASE_NAME") // Set the MongoDB collection name dbColl := "widget" fmt.Println("Connecting to MongoDB: ", dbURL) fmt.Println("Database Name: ", dbName) fmt.Println("Collection Name: ", dbColl) // Creating the database accessor here. // Pointer to this database accessor will be passed to the middleware. dbAccessor, err := nigronimgosession.NewDatabaseAccessor(dbURL, dbName, dbColl) if err != nil { panic(err) } n := negroni.Classic() // Registering the middleware here. n.Use(nigronimgosession.NewDatabase(*dbAccessor).Middleware()) mux := http.NewServeMux() mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { // You can access the nms object from the request context. // The nms object is stored in key `nigronimgosession.KEY`. ctx := request.Context() nms := ctx.Value(nigronimgosession.KEY).(*nigronimgosession.NMS) // Now lets perform a count query using mgo db object. count, _ := nms.DB.C("widget").Find(nil).Count() fmt.Fprintf(writer, "Determining the count in the collection using the db object. \n\n") fmt.Fprintf(writer, "Total number of object in the mongo database: %d \n\n", count) // You can access the mgo session object from the request object. // The session object is stored in key `mgoSession`. count2, _ := nms.Session.DB(dbName).C("widget").Find(nil).Count() fmt.Fprintf(writer, "Determining the count in the collection using the session object. \n\n") fmt.Fprintf(writer, "Total number of object in the mongo database: %d \n\n", count2) }) n.UseHandler(mux) n.Run(":3000") }
-
-
Assuming you are running the server locally, you can hit
http://localhost:3000/
to see the output.
Contribution
-
Fork the package on Github
git clone https://github.com/joeljames/nigroni-mgo-session.git cd nigroni-mgo-session
-
Create a new local branch to submit a pull request.
git checkout -b name-of-feature
-
Commit your changes
git commit -m "Detailed commit message" git push origin name-of-feature
-
Submit a pull request.