https://github.com/thecomputerm/surrealstore
A SurrealDB session store backend for gorilla/sessions
https://github.com/thecomputerm/surrealstore
golang gorilla-sessions surrealdb
Last synced: 17 days ago
JSON representation
A SurrealDB session store backend for gorilla/sessions
- Host: GitHub
- URL: https://github.com/thecomputerm/surrealstore
- Owner: TheComputerM
- License: mit
- Created: 2025-01-31T19:54:08.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-01-31T20:34:34.000Z (3 months ago)
- Last Synced: 2025-03-08T08:44:48.056Z (about 2 months ago)
- Topics: golang, gorilla-sessions, surrealdb
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# surrealstore
A session store backend for [gorilla/sessions](https://github.com/gorilla/sessions) using [SurrealDB](https://surrealdb.com/).
```sh
go get -u github.com/thecomputerm/surrealstore
```## Example
```go
package examplesimport (
"log"
"net/http"
"time""github.com/surrealdb/surrealdb.go"
"github.com/thecomputerm/surrealstore"
)// ExampleHandler is an example that displays the usage of PGStore.
func ExampleHandler(w http.ResponseWriter, r *http.Request) {
db, err := surrealdb.New("ws://localhost:8000")
if err != nil {
log.Fatalln("Requires a real instance of surrealdb listening on localhost:8000.")
}if _, err = db.SignIn(&surrealdb.Auth{
Username: "root",
Password: "root",
}); err != nil {
log.Fatalf("Failed to sign in to surrealdb: %v", err)
}if err = db.Use("default", "auth"); err != nil {
log.Fatalf("Failed to use correct namespace and db: %v", err)
}// Fetch new store.
store, err := surrealstore.NewSurrealStore(db, []byte("secret-key"))
if err != nil {
log.Fatalf("Failed to create a store: %v", err)
}// Run a background goroutine to clean up expired sessions from the database.
defer store.StopCleanup(store.Cleanup(time.Minute * 5))// Get a session.
session, err := store.Get(r, "session-key")
if err != nil {
log.Fatalf("Error getting session: %v", err)
}// Add a value.
session.Values["foo"] = "bar"// Save.
if err = session.Save(r, w); err != nil {
log.Fatalf("Error saving session: %v", err)
}// Delete session.
session.Options.MaxAge = -1
if err = session.Save(r, w); err != nil {
log.Fatalf("Error saving session: %v", err)
}
}
```To integrate it with [Gin](https://github.com/gin-gonic/gin), create a store mimicking the [postgres driver](https://github.com/gin-contrib/sessions/blob/master/postgres/postgres.go).
## Thanks
I've primarily just snatched and modified the code from [pgstore](https://github.com/antonlindstrom/pgstore) because it was the one [gin-sessions](https://github.com/gin-contrib/sessions) was using.
## Notes
- Wrapping the time.Time object in a models.CustomDateTime struct because of [this issue](https://github.com/surrealdb/surrealdb.go/issues/181).