mirror of
https://github.com/alexedwards/scs.git
synced 2025-07-13 01:00:17 +02:00
Standardise documentation for stores
This commit is contained in:
@ -262,6 +262,4 @@ if err != nil {
|
||||
|
||||
### Compatibility
|
||||
|
||||
This package requires Go 1.12 or newer.
|
||||
|
||||
You may have some problems using this package with Go frameworks that do not propagate the request context from standard-library compatible middleware, like [Echo](https://github.com/alexedwards/scs/issues/57) and [Fiber](https://github.com/alexedwards/scs/issues/106). If you are using Echo, please use the [echo-scs-session](https://github.com/spazzymoto/echo-scs-session) fork of this package instead.
|
||||
|
@ -1,11 +1,13 @@
|
||||
# badgerstore
|
||||
|
||||
A [Badger](https://github.com/dgraph-io/badger)-based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
A [Badger](https://github.com/dgraph-io/badger) based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
|
||||
## Example
|
||||
## Setup
|
||||
|
||||
You should follow the instructions to [install and open a database](https://github.com/dgraph-io/badger#installing), and pass the database to `badgerstore.New()` to establish the session store.
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@ -21,15 +23,14 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Create a Badger database.
|
||||
// Open a Badger database.
|
||||
db, err := badger.Open(badger.DefaultOptions("tmp/badger"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Initialize a new session manager and configure it to use badgerstore as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use badgerstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = badgerstore.New(db)
|
||||
|
||||
|
@ -1,11 +1,13 @@
|
||||
# boltstore
|
||||
|
||||
A Bolt-based session store for [SCS](https://github.com/alexedwards/scs) using the [go.etcd.io/bbolt](https://github.com/etcd-io/bbolt) package.
|
||||
A [Bolt](https://github.com/etcd-io/bbolt) based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
|
||||
## Example
|
||||
## Setup
|
||||
|
||||
You should follow the instructions to [open a Bolt database](https://github.com/etcd-io/bbolt#opening-a-database), and pass the database to `boltstore.New()` to establish the session store.
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@ -23,15 +25,14 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Establish a Bolt database.
|
||||
// Open a Bolt database.
|
||||
db, err := bbolt.Open("/tmp/bolt.db", 0600, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Initialize a new session manager and configure it to use boltstore as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use boltstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = boltstore.NewWithCleanupInterval(db, 20*time.Second)
|
||||
sessionManager.Lifetime = time.Minute
|
||||
|
@ -1,11 +1,13 @@
|
||||
# buntdbstore
|
||||
|
||||
A [BuntDB](https://github.com/tidwall/buntdb)-based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
A [BuntDB](https://github.com/tidwall/buntdb) based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
|
||||
## Example
|
||||
## Setup
|
||||
|
||||
You should follow the instructions to [install and open a database](https://github.com/tidwall/buntdb#installing), and pass the database to `buntdbstore.New()` to establish the session store.
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@ -21,15 +23,14 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Create a BuntDB database.
|
||||
// Open a BuntDB database.
|
||||
db, err := buntdb.Open("tmp/buntdb.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Initialize a new session manager and configure it to use buntdbstore as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use buntdbstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = buntdbstore.New(db)
|
||||
|
||||
|
47
firestore/README.md
Normal file
47
firestore/README.md
Normal file
@ -0,0 +1,47 @@
|
||||
# firestore
|
||||
|
||||
A [Google Cloud Firestore](https://pkg.go.dev/cloud.google.com/go/firestore) based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
|
||||
## Setup
|
||||
|
||||
You should follow the instructions to [install and open a database](https://cloud.google.com/firestore/docs), and pass the database to `firestore.New()` to establish the session store.
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/alexedwards/scs/v2"
|
||||
"github.com/alexedwards/scs/firestore"
|
||||
)
|
||||
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Establish connection to Google Cloud Firestore.
|
||||
// ...
|
||||
|
||||
// Initialize a new session manager and configure it to use firestore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = firestore.New(db)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/put", putHandler)
|
||||
mux.HandleFunc("/get", getHandler)
|
||||
|
||||
http.ListenAndServe(":4000", sessionManager.LoadAndSave(mux))
|
||||
}
|
||||
|
||||
func putHandler(w http.ResponseWriter, r *http.Request) {
|
||||
sessionManager.Put(r.Context(), "message", "Hello from a session!")
|
||||
}
|
||||
|
||||
func getHandler(w http.ResponseWriter, r *http.Request) {
|
||||
msg := sessionManager.GetString(r.Context(), "message")
|
||||
io.WriteString(w, msg)
|
||||
}
|
||||
```
|
@ -1,10 +1,10 @@
|
||||
# gormstore
|
||||
|
||||
A GORM based session store using [GORM](https://gorm.io/).
|
||||
A [GORM](https://github.com/go-gorm/gorm) based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
|
||||
## Setup
|
||||
|
||||
You should have a working database containing a `sessions` table with the definition:
|
||||
You should have a working database containing a `sessions` table with the definition (for PostgreSQL):
|
||||
|
||||
```sql
|
||||
CREATE TABLE sessions (
|
||||
@ -15,10 +15,11 @@ CREATE TABLE sessions (
|
||||
|
||||
CREATE INDEX sessions_expiry_idx ON sessions (expiry);
|
||||
```
|
||||
For other stores you can find the setup here: [MSSQL](https://github.com/alexedwards/scs/tree/master/mssqlstore), [MySQL](https://github.com/alexedwards/scs/tree/master/mysqlstore), [SQLite3](https://github.com/alexedwards/scs/tree/master/sqlite3store).
|
||||
|
||||
If no table is pressent new table will be automatically created.
|
||||
If no table is present, a new one will be automatically created.
|
||||
|
||||
The database user for your application must have `CREATE TABLE`, `SELECT`, `INSERT`, `UPDATE` and `DELETE` permissions.
|
||||
The database user for your application must have `CREATE TABLE`, `SELECT`, `INSERT`, `UPDATE` and `DELETE` permissions on this table.
|
||||
|
||||
## Example
|
||||
|
||||
@ -41,14 +42,14 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Establish connection to your store (PostgreSQL here).
|
||||
db, err := gorm.Open("postgres", "postgres://user:pass@localhost/db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Initialize a new session manager and configure it to use GORM as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use gormstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
if sessionManager.Store, err = gormstore.New(db); err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -1,6 +1,6 @@
|
||||
# memstore
|
||||
|
||||
An in-memory session store for [SCS](https://github.com/alexedwards/scs).
|
||||
An in-memory session store, this is the default store for [SCS](https://github.com/alexedwards/scs) if none of the others stores are used.
|
||||
|
||||
|
||||
Because memstore uses in-memory storage only, all session data will be lost when your application is stopped or restarted. Therefore it should only be used in applications where data loss is an acceptable trade off for fast performance, or for prototyping and testing purposes.
|
||||
@ -21,8 +21,7 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Initialize a new session manager and configure it to use memstore as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use memstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = memstore.New()
|
||||
|
||||
|
@ -1,11 +1,13 @@
|
||||
# mongodbstore
|
||||
|
||||
A MongoDB based session store for [SCS](https://github.com/alexedwards/scs) using the official [MongoDB Go Driver](https://github.com/mongodb/mongo-go-driver).
|
||||
A [MongoDB](https://github.com/mongodb/mongo-go-driver) based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
|
||||
## Setup
|
||||
|
||||
You should follow the [usage instructions](https://github.com/mongodb/mongo-go-driver#usage), and pass the database to `mongodbstore.New()` to establish the session store.
|
||||
|
||||
## Example
|
||||
|
||||
You should follow the [usage instructions](https://github.com/mongodb/mongo-go-driver#usage), and pass the database reference to `mongodbstore.New()` to establish the session store.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@ -36,8 +38,7 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
// Initialize a new session manager and configure it to use mongodbstore as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use mongodbstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = mongodbstore.New(client.Database("database"))
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# mssqlstore
|
||||
|
||||
A MSSQL-based session store supporting the [denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb) driver.
|
||||
A [MSSQL](https://github.com/denisenkom/go-mssqldb) based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
|
||||
## Setup
|
||||
|
||||
@ -38,14 +38,14 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Establish connection to MSSQL.
|
||||
db, err := sql.Open("sqlserver", "sqlserver://username:password@host?database=dbname")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Initialize a new session manager and configure it to use MSSQL as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use mssqlstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = mssqlstore.New(db)
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# mysqlstore
|
||||
|
||||
A MySQL-based session store supporting the [go-sql-driver/mysql](https://github.com/go-sql-driver/mysql) driver.
|
||||
A [MySQL](https://github.com/go-sql-driver/mysql) based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
|
||||
## Setup
|
||||
|
||||
@ -38,14 +38,14 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Establish connection to MySQL.
|
||||
db, err := sql.Open("mysql", "user:pass@/db?parseTime=true")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Initialize a new session manager and configure it to use MySQL as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use mysqlstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = mysqlstore.New(db)
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# pgxstore
|
||||
|
||||
A PostgreSQL-based session store supporting the [pgx](https://github.com/jackc/pgx) driver.
|
||||
A PostgreSQL based session store for [SCS](https://github.com/alexedwards/scs) using the [pgx](https://github.com/jackc/pgx) driver.
|
||||
|
||||
## Setup
|
||||
|
||||
@ -37,14 +37,14 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Establish connection pool to PostgreSQL.
|
||||
pool, err := pgxpool.Connect(context.Background(), "postgres://user:pass@localhost/db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
// Initialize a new session manager and configure it to use PostgreSQL with the pgx
|
||||
// driver as the session store.
|
||||
// Initialize a new session manager and configure it to use pgxstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = pgxstore.New(pool)
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# postgresstore
|
||||
|
||||
A PostgreSQL-based session store supporting the [pq](https://github.com/lib/pq) driver.
|
||||
A PostgreSQL based session store for [SCS](https://github.com/alexedwards/scs) using the [pq](https://github.com/lib/pq) driver.
|
||||
|
||||
## Setup
|
||||
|
||||
@ -38,14 +38,14 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Establish connection to PostgreSQL.
|
||||
db, err := sql.Open("postgres", "postgres://user:pass@localhost/db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Initialize a new session manager and configure it to use PostgreSQL as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use postgresstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = postgresstore.New(db)
|
||||
|
||||
|
@ -1,11 +1,13 @@
|
||||
# redisstore
|
||||
|
||||
A Redis-based session store for [SCS](https://github.com/alexedwards/scs) using the popular [redigo](https://github.com/gomodule/redigo) driver.
|
||||
A [Redis](https://github.com/gomodule/redigo) based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
|
||||
## Example
|
||||
## Setup
|
||||
|
||||
You should follow the instructions to [setup a connection pool](https://godoc.org/github.com/gomodule/redigo/redis#Pool), and pass the pool to `redisstore.New()` to establish the session store.
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@ -21,7 +23,7 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Establish a redigo connection pool.
|
||||
// Establish connection pool to Redis.
|
||||
pool := &redis.Pool{
|
||||
MaxIdle: 10,
|
||||
Dial: func() (redis.Conn, error) {
|
||||
@ -29,8 +31,7 @@ func main() {
|
||||
},
|
||||
}
|
||||
|
||||
// Initialize a new session manager and configure it to use redisstore as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use redisstore as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = redisstore.New(pool)
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
# sqlite3store
|
||||
|
||||
A SQLite3-based session store supporting the [mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) driver.
|
||||
A [SQLite3](https://github.com/mattn/go-sqlite3) based session store for [SCS](https://github.com/alexedwards/scs).
|
||||
|
||||
## Setup
|
||||
|
||||
You should have a working SQLite3 database file a `sessions` table with the definition:
|
||||
You should have a working SQLite3 database file containing a `sessions` table with the definition:
|
||||
|
||||
```sql
|
||||
CREATE TABLE sessions (
|
||||
@ -36,14 +36,14 @@ import (
|
||||
var sessionManager *scs.SessionManager
|
||||
|
||||
func main() {
|
||||
// Open a SQLite3 database.
|
||||
db, err := sql.Open("sqlite3", "sqlite3_database.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Initialize a new session manager and configure it to use SQLite3 as
|
||||
// the session store.
|
||||
// Initialize a new session manager and configure it to use sqlite3store as the session store.
|
||||
sessionManager = scs.New()
|
||||
sessionManager.Store = sqlite3store.New(db)
|
||||
|
||||
|
Reference in New Issue
Block a user