Middleware which sets up an established database session according to a request at *gin.Context
- Support database
- MongoDB
- Redis
- RethinkDB
TODO
var option *Option = &mdsession.Option{
Dn: DnName, // uniq datastore name
Name: Name, // Key to the `*gin.Context`
Make: true, // To connect every time
}
var options *Options = &mdsession.Options{
Sets: []*Option{option},
}
g := gin.New()
// append gin middleware
g.Use(MiddlewareMongoDB(options))
g.GET("/test", func(c *gin.Context) {
ret, err := c.Get(Name)
s, ok := ret.(*mgo.Session)
if ret != nil && err == nil && ok {
c.String(200, "OK dbs: " + fmt.Sprint(s.DatabaseNames()))
} else {
c.String(500, "NG")
}
})
Please read the test code.
$ go get github.com/mattn/gom # package manager
$ make init
$ make test
$ go get github.com/smartystreets/goconvey // Coverage library
$ make cover
MIT License