-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from iris-contrib/mongo
Mongo store
- Loading branch information
Showing
7 changed files
with
746 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,24 @@ | ||
This is free and unencumbered software released into the public domain. | ||
|
||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
|
||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
For more information, please refer to <https://unlicense.org> |
This file contains 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 |
---|---|---|
@@ -1 +1,67 @@ | ||
# sessiondb | ||
|
||
[![Go Report](https://goreportcard.com/badge/github.com/iris-contrib/sessiondb)](https://goreportcard.com/report/github.com/iris-contrib/sessiondb) | ||
|
||
Dgraph & MongoDB stores for `sessions` of [kataras/iris](https://github.com/kataras/go-sessions). | ||
|
||
## How to use | ||
|
||
Full examples can be found in the [examples](https://github.com/iris-contrib/sessiondb/tree/main/examples) folder. | ||
|
||
### Mongo | ||
|
||
[![Mongo Reference](https://pkg.go.dev/badge/github.com/iris-contrib/sessiondb/mongostore.svg)](https://pkg.go.dev/github.com/iris-contrib/sessiondb/mongostore) | ||
|
||
To include `mongostore` run | ||
```sh | ||
go get github.com/iris-contrib/sessiondb/mongostore | ||
``` | ||
|
||
#### Example | ||
|
||
```go | ||
// replace with your running mongo server settings: | ||
cred := options.Credential{ | ||
AuthSource: "admin", | ||
Username: "user", | ||
Password: "password", | ||
} | ||
|
||
clientOpts := options.Client().ApplyURI("mongodb://127.0.0.1:27017").SetAuth(cred) | ||
db, _ := mongostore.New(clientOpts, "sessions") | ||
|
||
sess := sessions.New(sessions.Config{Cookie: "sessionscookieid"}) | ||
|
||
sess.UseDatabase(db) | ||
``` | ||
|
||
### Dgraph | ||
|
||
[![Dgraph Reference](https://pkg.go.dev/badge/github.com/iris-contrib/sessiondb/dgraphstore.svg)](https://pkg.go.dev/github.com/iris-contrib/sessiondb/dgraphstore) | ||
|
||
To include `dgraphstore` run | ||
```sh | ||
go get github.com/iris-contrib/sessiondb/dgraphstore | ||
``` | ||
|
||
#### Example | ||
|
||
```go | ||
// replace with your server settings: | ||
conn, _ := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure()) | ||
db, _ := dgraphstore.NewFromDB(conn) | ||
|
||
sess := sessions.New(sessions.Config{Cookie: "sessionscookieid"}) | ||
|
||
sess.UseDatabase(db) | ||
``` | ||
|
||
## Contribute | ||
|
||
Development of each store is done on branches. If you plan to work with an existing store checkout the corresponding branch. If you intent to implement a new store then create a new branch named after the DB you are using. | ||
|
||
The repository is using [go-submodules](https://github.com/go-modules-by-example/index/tree/master/009_submodules). *The rationale behind this optimisation is to avoid the user having to download unnecessary drivers/libraries but keep the maintenance tight and clean.* | ||
|
||
For releasing a new version of each individual store (submodule) you need to | ||
1. merge from corresponding branch to `master` | ||
2. tag with appropriate store name and particular store's version ie. `git tag mongostore/v0.1.1` |
This file contains 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,73 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/bh90210/go-sessions-stores/dgraphstore" | ||
"github.com/kataras/go-sessions/v3" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func main() { | ||
// replace with your server settings: | ||
conn, _ := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure()) | ||
db, err := dgraphstore.NewFromDB(conn) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
defer db.Close() | ||
|
||
sess := sessions.New(sessions.Config{Cookie: "sessionscookieid"}) | ||
|
||
// | ||
// IMPORTANT: | ||
// | ||
sess.UseDatabase(db) | ||
|
||
// the rest of the code stays the same. | ||
app := http.NewServeMux() | ||
|
||
app.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(fmt.Sprintf("You should navigate to the /set, /get, /delete, /clear,/destroy instead"))) | ||
}) | ||
app.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) { | ||
s := sess.Start(w, r) | ||
//set session values | ||
s.Set("name", "iris") | ||
|
||
//test if setted here | ||
w.Write([]byte(fmt.Sprintf("All ok session setted to: %s", s.GetString("name")))) | ||
}) | ||
|
||
app.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) { | ||
// get a specific key, as string, if no found returns just an empty string | ||
name := sess.Start(w, r).GetString("name") | ||
|
||
w.Write([]byte(fmt.Sprintf("The name on the /set was: %s", name))) | ||
}) | ||
|
||
app.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request) { | ||
// delete a specific key | ||
sess.Start(w, r).Delete("name") | ||
}) | ||
|
||
app.HandleFunc("/clear", func(w http.ResponseWriter, r *http.Request) { | ||
// removes all entries | ||
sess.Start(w, r).Clear() | ||
}) | ||
|
||
app.HandleFunc("/destroy", func(w http.ResponseWriter, r *http.Request) { | ||
//destroy, removes the entire session data and cookie | ||
sess.Destroy(w, r) | ||
}) | ||
|
||
app.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) { | ||
// updates expire date with a new date | ||
sess.ShiftExpiration(w, r) | ||
}) | ||
|
||
http.ListenAndServe(":8081", app) | ||
} |
This file contains 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,79 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/bh90210/go-sessions-stores/mongostore" | ||
"github.com/kataras/go-sessions/v3" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
func main() { | ||
// replace with your running mongo' server settings: | ||
cred := options.Credential{ | ||
AuthSource: "admin", | ||
Username: "user", | ||
Password: "password", | ||
} | ||
|
||
clientOpts := options.Client().ApplyURI("mongodb://127.0.0.1:27017").SetAuth(cred) | ||
db, err := mongostore.New(clientOpts, "sessions") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// defer db.Close() | ||
|
||
sess := sessions.New(sessions.Config{Cookie: "sessionscookieid"}) | ||
|
||
// | ||
// IMPORTANT: | ||
// | ||
sess.UseDatabase(db) | ||
|
||
// the rest of the code stays the same. | ||
app := http.NewServeMux() | ||
|
||
app.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(fmt.Sprintf("You should navigate to the /set, /get, /delete, /clear,/destroy instead"))) | ||
}) | ||
app.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) { | ||
s := sess.Start(w, r) | ||
//set session values | ||
s.Set("name", "iris") | ||
|
||
//test if setted here | ||
w.Write([]byte(fmt.Sprintf("All ok session setted to: %s", s.GetString("name")))) | ||
}) | ||
|
||
app.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) { | ||
// get a specific key, as string, if no found returns just an empty string | ||
name := sess.Start(w, r).GetString("name") | ||
|
||
w.Write([]byte(fmt.Sprintf("The name on the /set was: %s", name))) | ||
}) | ||
|
||
app.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request) { | ||
// delete a specific key | ||
sess.Start(w, r).Delete("name") | ||
}) | ||
|
||
app.HandleFunc("/clear", func(w http.ResponseWriter, r *http.Request) { | ||
// removes all entries | ||
sess.Start(w, r).Clear() | ||
}) | ||
|
||
app.HandleFunc("/destroy", func(w http.ResponseWriter, r *http.Request) { | ||
//destroy, removes the entire session data and cookie | ||
sess.Destroy(w, r) | ||
}) | ||
|
||
app.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) { | ||
// updates expire date with a new date | ||
sess.ShiftExpiration(w, r) | ||
}) | ||
|
||
log.Fatal(http.ListenAndServe(":8081", app)) | ||
} |
Oops, something went wrong.