Skip to content

Commit f48b1cf

Browse files
authored
add support for official mongodb driver - new version (#154)
1 parent bd1e07d commit f48b1cf

10 files changed

Lines changed: 310 additions & 84 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ coverage.out
22
vendor/*
33
!/vendor/vendor.json
44
/gorm/test.db
5+
.idea

README.md

Lines changed: 111 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,13 @@ func main() {
250250

251251
### MongoDB
252252

253+
#### mgo
253254
```go
254255
package main
255256

256257
import (
257258
"github.com/gin-contrib/sessions"
258-
"github.com/gin-contrib/sessions/mongo"
259+
"github.com/gin-contrib/sessions/mongo/mongomgo"
259260
"github.com/gin-gonic/gin"
260261
"github.com/globalsign/mgo"
261262
)
@@ -268,7 +269,54 @@ func main() {
268269
}
269270

270271
c := session.DB("").C("sessions")
271-
store := mongo.NewStore(c, 3600, true, []byte("secret"))
272+
store := mongomgo.NewStore(c, 3600, true, []byte("secret"))
273+
r.Use(sessions.Sessions("mysession", store))
274+
275+
r.GET("/incr", func(c *gin.Context) {
276+
session := sessions.Default(c)
277+
var count int
278+
v := session.Get("count")
279+
if v == nil {
280+
count = 0
281+
} else {
282+
count = v.(int)
283+
count++
284+
}
285+
session.Set("count", count)
286+
session.Save()
287+
c.JSON(200, gin.H{"count": count})
288+
})
289+
r.Run(":8000")
290+
}
291+
```
292+
293+
#### mongo-driver
294+
```
295+
package main
296+
297+
import (
298+
"context"
299+
"github.com/gin-contrib/sessions"
300+
"github.com/gin-contrib/sessions/mongo/mongodriver"
301+
"github.com/gin-gonic/gin"
302+
"go.mongodb.org/mongo-driver/mongo"
303+
"go.mongodb.org/mongo-driver/mongo/options"
304+
)
305+
306+
func main() {
307+
r := gin.Default()
308+
mongoOptions := options.Client().ApplyURI("mongodb://localhost:27017")
309+
client, err := mongo.NewClient(mongoOptions)
310+
if err != nil {
311+
// handle err
312+
}
313+
314+
if err := client.Connect(context.Background()); err != nil {
315+
// handle err
316+
}
317+
318+
c := client.Database("test").Collection("sessions")
319+
store := mongodriver.NewStore(c, 3600, true, []byte("secret"))
272320
r.Use(sessions.Sessions("mysession", store))
273321
274322
r.GET("/incr", func(c *gin.Context) {
@@ -330,38 +378,38 @@ func main() {
330378
package main
331379

332380
import (
333-
"github.com/gin-contrib/sessions"
334-
gormsessions "github.com/gin-contrib/sessions/gorm"
335-
"github.com/gin-gonic/gin"
336-
"gorm.io/driver/sqlite"
337-
"gorm.io/gorm"
381+
"github.com/gin-contrib/sessions"
382+
gormsessions "github.com/gin-contrib/sessions/gorm"
383+
"github.com/gin-gonic/gin"
384+
"gorm.io/driver/sqlite"
385+
"gorm.io/gorm"
338386
)
339387

340388
func main() {
341-
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
342-
if err != nil {
343-
panic(err)
344-
}
345-
store := gormsessions.NewStore(db, true, []byte("secret"))
346-
347-
r := gin.Default()
348-
r.Use(sessions.Sessions("mysession", store))
349-
350-
r.GET("/incr", func(c *gin.Context) {
351-
session := sessions.Default(c)
352-
var count int
353-
v := session.Get("count")
354-
if v == nil {
355-
count = 0
356-
} else {
357-
count = v.(int)
358-
count++
359-
}
360-
session.Set("count", count)
361-
session.Save()
362-
c.JSON(200, gin.H{"count": count})
363-
})
364-
r.Run(":8000")
389+
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
390+
if err != nil {
391+
panic(err)
392+
}
393+
store := gormsessions.NewStore(db, true, []byte("secret"))
394+
395+
r := gin.Default()
396+
r.Use(sessions.Sessions("mysession", store))
397+
398+
r.GET("/incr", func(c *gin.Context) {
399+
session := sessions.Default(c)
400+
var count int
401+
v := session.Get("count")
402+
if v == nil {
403+
count = 0
404+
} else {
405+
count = v.(int)
406+
count++
407+
}
408+
session.Set("count", count)
409+
session.Save()
410+
c.JSON(200, gin.H{"count": count})
411+
})
412+
r.Run(":8000")
365413
}
366414
```
367415

@@ -371,40 +419,40 @@ func main() {
371419
package main
372420

373421
import (
374-
"database/sql"
375-
"github.com/gin-contrib/sessions"
376-
"github.com/gin-contrib/sessions/postgres"
377-
"github.com/gin-gonic/gin"
422+
"database/sql"
423+
"github.com/gin-contrib/sessions"
424+
"github.com/gin-contrib/sessions/postgres"
425+
"github.com/gin-gonic/gin"
378426
)
379427

380428
func main() {
381-
r := gin.Default()
382-
db, err := sql.Open("postgres", "postgresql://username:password@localhost:5432/database")
383-
if err != nil {
384-
// handle err
385-
}
386-
387-
store, err := postgres.NewStore(db, []byte("secret"))
388-
if err != nil {
389-
// handle err
390-
}
391-
392-
r.Use(sessions.Sessions("mysession", store))
393-
394-
r.GET("/incr", func(c *gin.Context) {
395-
session := sessions.Default(c)
396-
var count int
397-
v := session.Get("count")
398-
if v == nil {
399-
count = 0
400-
} else {
401-
count = v.(int)
402-
count++
403-
}
404-
session.Set("count", count)
405-
session.Save()
406-
c.JSON(200, gin.H{"count": count})
407-
})
408-
r.Run(":8000")
429+
r := gin.Default()
430+
db, err := sql.Open("postgres", "postgresql://username:password@localhost:5432/database")
431+
if err != nil {
432+
// handle err
433+
}
434+
435+
store, err := postgres.NewStore(db, []byte("secret"))
436+
if err != nil {
437+
// handle err
438+
}
439+
440+
r.Use(sessions.Sessions("mysession", store))
441+
442+
r.GET("/incr", func(c *gin.Context) {
443+
session := sessions.Default(c)
444+
var count int
445+
v := session.Get("count")
446+
if v == nil {
447+
count = 0
448+
} else {
449+
count = v.(int)
450+
count++
451+
}
452+
session.Set("count", count)
453+
session.Save()
454+
c.JSON(200, gin.H{"count": count})
455+
})
456+
r.Run(":8000")
409457
}
410458
```

_example/mongo/mongodriver/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"github.com/gin-contrib/sessions"
6+
"github.com/gin-contrib/sessions/mongo/mongodriver"
7+
"github.com/gin-gonic/gin"
8+
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
)
11+
12+
func main() {
13+
r := gin.Default()
14+
mongoOptions := options.Client().ApplyURI("mongodb://localhost:27017")
15+
client, err := mongo.NewClient(mongoOptions)
16+
if err != nil {
17+
// handle err
18+
}
19+
20+
if err := client.Connect(context.Background()); err != nil {
21+
// handle err
22+
}
23+
24+
c := client.Database("test").Collection("sessions")
25+
store := mongodriver.NewStore(c, 3600, true, []byte("secret"))
26+
r.Use(sessions.Sessions("mysession", store))
27+
28+
r.GET("/incr", func(c *gin.Context) {
29+
session := sessions.Default(c)
30+
var count int
31+
v := session.Get("count")
32+
if v == nil {
33+
count = 0
34+
} else {
35+
count = v.(int)
36+
count++
37+
}
38+
session.Set("count", count)
39+
session.Save()
40+
c.JSON(200, gin.H{"count": count})
41+
})
42+
r.Run(":8000")
43+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"github.com/gin-contrib/sessions"
5-
"github.com/gin-contrib/sessions/mongo"
5+
"github.com/gin-contrib/sessions/mongo/mongomgo"
66
"github.com/gin-gonic/gin"
77
"github.com/globalsign/mgo"
88
)
@@ -15,7 +15,7 @@ func main() {
1515
}
1616

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

2121
r.GET("/incr", func(c *gin.Context) {

go.mod

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module github.com/gin-contrib/sessions
22

3-
go 1.13
3+
go 1.18
44

55
require (
66
github.com/antonlindstrom/pgstore v0.0.0-20200229204646-b08ebf1105e0
77
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff
8+
github.com/bos-hieu/mongostore v0.0.2
89
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
910
github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1
1011
github.com/gin-gonic/gin v1.7.4
@@ -13,10 +14,42 @@ require (
1314
github.com/gorilla/context v1.1.1
1415
github.com/gorilla/sessions v1.2.1
1516
github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b
16-
github.com/lib/pq v1.10.3 // indirect
1717
github.com/memcachier/mc v2.0.1+incompatible
1818
github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b
1919
github.com/wader/gormstore/v2 v2.0.0
20+
go.mongodb.org/mongo-driver v1.9.0
2021
gorm.io/driver/sqlite v1.1.4
2122
gorm.io/gorm v1.20.12
2223
)
24+
25+
require (
26+
github.com/gin-contrib/sse v0.1.0 // indirect
27+
github.com/go-playground/locales v0.13.0 // indirect
28+
github.com/go-playground/universal-translator v0.17.0 // indirect
29+
github.com/go-playground/validator/v10 v10.4.1 // indirect
30+
github.com/go-stack/stack v1.8.0 // indirect
31+
github.com/golang/protobuf v1.3.3 // indirect
32+
github.com/golang/snappy v0.0.1 // indirect
33+
github.com/gorilla/securecookie v1.1.1 // indirect
34+
github.com/jinzhu/inflection v1.0.0 // indirect
35+
github.com/jinzhu/now v1.1.1 // indirect
36+
github.com/json-iterator/go v1.1.9 // indirect
37+
github.com/klauspost/compress v1.13.6 // indirect
38+
github.com/leodido/go-urn v1.2.0 // indirect
39+
github.com/lib/pq v1.10.3 // indirect
40+
github.com/mattn/go-isatty v0.0.12 // indirect
41+
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
42+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
43+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
44+
github.com/pkg/errors v0.9.1 // indirect
45+
github.com/ugorji/go/codec v1.1.7 // indirect
46+
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
47+
github.com/xdg-go/scram v1.0.2 // indirect
48+
github.com/xdg-go/stringprep v1.0.2 // indirect
49+
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
50+
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
51+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
52+
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect
53+
golang.org/x/text v0.3.5 // indirect
54+
gopkg.in/yaml.v2 v2.2.8 // indirect
55+
)

0 commit comments

Comments
 (0)