-
Notifications
You must be signed in to change notification settings - Fork 201
add support for official mongodb driver - new version #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a9a337a
add mongo driver and upgrade go version to 1.18
bos-hieu a8b1c6c
add mongo driver and upgrade go version to 1.18
bos-hieu 8288ce7
update readme
bos-hieu 9b11e42
update readme
bos-hieu b3d78e5
revert session options
bos-hieu 24f1d52
update syntax
bos-hieu 57931a9
fix test case failed
bos-hieu c126a70
update example
bos-hieu 04626cd
fix feedbacks: moved import statement to down section and replaced ta…
bos-hieu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ coverage.out | |
| vendor/* | ||
| !/vendor/vendor.json | ||
| /gorm/test.db | ||
| .idea | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package mongodriver | ||
|
|
||
| import ( | ||
| "github.com/bos-hieu/mongostore" | ||
| "github.com/gin-contrib/sessions" | ||
| "go.mongodb.org/mongo-driver/mongo" | ||
| ) | ||
|
|
||
| var ( | ||
| _ sessions.Store = (*store)(nil) | ||
| ) | ||
|
|
||
| func NewStore(c *mongo.Collection, maxAge int, ensureTTL bool, keyPairs ...[]byte) sessions.Store { | ||
| return &store{mongostore.NewMongoStore(c, maxAge, ensureTTL, keyPairs...)} | ||
| } | ||
|
|
||
| type store struct { | ||
| *mongostore.MongoStore | ||
| } | ||
|
|
||
| func (c *store) Options(options sessions.Options) { | ||
| c.MongoStore.Options = options.ToGorillaOptions() | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.