Skip to content

Commit

Permalink
add support for official mongodb driver - new version (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
bos-hieu authored Apr 10, 2022
1 parent bd1e07d commit f48b1cf
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage.out
vendor/*
!/vendor/vendor.json
/gorm/test.db
.idea
174 changes: 111 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,13 @@ func main() {

### MongoDB

#### mgo
```go
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/mongo"
"github.com/gin-contrib/sessions/mongo/mongomgo"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo"
)
Expand All @@ -268,7 +269,54 @@ func main() {
}

c := session.DB("").C("sessions")
store := mongo.NewStore(c, 3600, true, []byte("secret"))
store := mongomgo.NewStore(c, 3600, true, []byte("secret"))
r.Use(sessions.Sessions("mysession", store))

r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
```

#### mongo-driver
```
package main
import (
"context"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/mongo/mongodriver"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
r := gin.Default()
mongoOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.NewClient(mongoOptions)
if err != nil {
// handle err
}
if err := client.Connect(context.Background()); err != nil {
// handle err
}
c := client.Database("test").Collection("sessions")
store := mongodriver.NewStore(c, 3600, true, []byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
Expand Down Expand Up @@ -330,38 +378,38 @@ func main() {
package main

import (
"github.com/gin-contrib/sessions"
gormsessions "github.com/gin-contrib/sessions/gorm"
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"github.com/gin-contrib/sessions"
gormsessions "github.com/gin-contrib/sessions/gorm"
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic(err)
}
store := gormsessions.NewStore(db, true, []byte("secret"))

r := gin.Default()
r.Use(sessions.Sessions("mysession", store))

r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic(err)
}
store := gormsessions.NewStore(db, true, []byte("secret"))

r := gin.Default()
r.Use(sessions.Sessions("mysession", store))

r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
```

Expand All @@ -371,40 +419,40 @@ func main() {
package main

import (
"database/sql"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/postgres"
"github.com/gin-gonic/gin"
"database/sql"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/postgres"
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
db, err := sql.Open("postgres", "postgresql://username:password@localhost:5432/database")
if err != nil {
// handle err
}

store, err := postgres.NewStore(db, []byte("secret"))
if err != nil {
// handle err
}

r.Use(sessions.Sessions("mysession", store))

r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
r := gin.Default()
db, err := sql.Open("postgres", "postgresql://username:password@localhost:5432/database")
if err != nil {
// handle err
}

store, err := postgres.NewStore(db, []byte("secret"))
if err != nil {
// handle err
}

r.Use(sessions.Sessions("mysession", store))

r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
```
43 changes: 43 additions & 0 deletions _example/mongo/mongodriver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"context"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/mongo/mongodriver"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
r := gin.Default()
mongoOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.NewClient(mongoOptions)
if err != nil {
// handle err
}

if err := client.Connect(context.Background()); err != nil {
// handle err
}

c := client.Database("test").Collection("sessions")
store := mongodriver.NewStore(c, 3600, true, []byte("secret"))
r.Use(sessions.Sessions("mysession", store))

r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
4 changes: 2 additions & 2 deletions _example/mongo/main.go → _example/mongo/mongomgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/mongo"
"github.com/gin-contrib/sessions/mongo/mongomgo"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo"
)
Expand All @@ -15,7 +15,7 @@ func main() {
}

c := session.DB("").C("sessions")
store := mongo.NewStore(c, 3600, true, []byte("secret"))
store := mongomgo.NewStore(c, 3600, true, []byte("secret"))
r.Use(sessions.Sessions("mysession", store))

r.GET("/incr", func(c *gin.Context) {
Expand Down
37 changes: 35 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/gin-contrib/sessions

go 1.13
go 1.18

require (
github.com/antonlindstrom/pgstore v0.0.0-20200229204646-b08ebf1105e0
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff
github.com/bos-hieu/mongostore v0.0.2
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1
github.com/gin-gonic/gin v1.7.4
Expand All @@ -13,10 +14,42 @@ require (
github.com/gorilla/context v1.1.1
github.com/gorilla/sessions v1.2.1
github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b
github.com/lib/pq v1.10.3 // indirect
github.com/memcachier/mc v2.0.1+incompatible
github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b
github.com/wader/gormstore/v2 v2.0.0
go.mongodb.org/mongo-driver v1.9.0
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.20.12
)

require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/protobuf v1.3.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.1 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/lib/pq v1.10.3 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect
golang.org/x/text v0.3.5 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
)
Loading

0 comments on commit f48b1cf

Please sign in to comment.