Skip to content
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

feat(store): add xorm support #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## Features

- Automatic loading and saving of session data via middleware.
- Choice of 19 different server-side session stores including PostgreSQL, MySQL, MSSQL, SQLite, Redis and many others. Custom session stores are also supported.
- Choice of 20 different server-side session stores including PostgreSQL, MySQL, MSSQL, SQLite, Redis and many others. Custom session stores are also supported.
- Supports multiple sessions per request, 'flash' messages, session token regeneration, idle and absolute session timeouts, and 'remember me' functionality.
- Easy to extend and customize. Communicate session tokens to/from clients in HTTP headers or request/response bodies.
- Efficient design. Smaller, faster and uses less memory than [gorilla/sessions](https://github.com/gorilla/sessions).
Expand Down Expand Up @@ -169,6 +169,7 @@ The session stores currently included are shown in the table below. Please click
| [postgresstore](https://github.com/alexedwards/scs/tree/master/postgresstore) | PostgreSQL based session store (using the [pq](https://github.com/lib/pq) driver) |
| [redisstore](https://github.com/alexedwards/scs/tree/master/redisstore) | Redis based session store |
| [sqlite3store](https://github.com/alexedwards/scs/tree/master/sqlite3store) | SQLite3 based session store |
| [xormstore](https://github.com/alexedwards/scs/tree/master/xormstore) | Xorm based session store |

Custom session stores are also supported. Please [see here](#using-custom-session-stores) for more information.

Expand Down Expand Up @@ -310,4 +311,4 @@ You may have some problems using this package with Go frameworks that do not pro

### Contributing

Bug fixes and documentation improvements are very welcome! For feature additions or behavioral changes, please open an issue to discuss the change before submitting a PR. For new stores, please also open an issue to establish whether there is wider demand for the store before submitting a PR.
Bug fixes and documentation improvements are very welcome! For feature additions or behavioral changes, please open an issue to discuss the change before submitting a PR. For new stores, please also open an issue to establish whether there is wider demand for the store before submitting a PR.
97 changes: 97 additions & 0 deletions xormstore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# xormstore

A [Xorm](https://xorm.io/) based session store for [SCS](https://github.com/alexedwards/scs).

## Setup

`xormstore` will auto migrate `sessions` table
## Example

```go
package main

import (
"io"
"log"
"net/http"

"github.com/alexedwards/scs/xormstore"
"github.com/alexedwards/scs/v2"

"xorm.io/xorm"

_ "github.com/go-sql-driver/mysql"
)

var sessionManager *scs.SessionManager

func main() {
// Establish connection to your store.
db, err := xorm.NewEngine("mysql", "root:123@/test?charset=utf8") // MySQL
// db, err := xorm.NewEngine("sqlite3", "data.db") // SQLite3
if err != nil {
log.Fatal(err)
}

// Initialize a new session manager and configure it to use gormstore as the session store.
sessionManager = scs.New()
if sessionManager.Store, err = xormstore.New(db); err != nil {
log.Fatal(err)
}

mux := http.NewServeMux()
mux.HandleFunc("/put", putHandler)
mux.HandleFunc("/get", getHandler)

log.Println("OK")
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)
}
```

## Expired Session Cleanup

This package provides a background 'cleanup' goroutine to delete expired session data. This stops the database table from holding on to invalid sessions indefinitely and growing unnecessarily large. By default the cleanup runs every 5 minutes. You can change this by using the `NewWithCleanupInterval()` function to initialize your session store. For example:

```go
// Run a cleanup every 30 minutes.
xormstore.NewWithCleanupInterval(db, 30*time.Minute)

// Disable the cleanup goroutine by setting the cleanup interval to zero.
xormstore.NewWithCleanupInterval(db, 0)
```

### Terminating the Cleanup Goroutine

It's rare that the cleanup goroutine needs to be terminated --- it is generally intended to be long-lived and run for the lifetime of your application.

However, there may be occasions when your use of a session store instance is transient. A common example would be using it in a short-lived test function. In this scenario, the cleanup goroutine (which will run forever) will prevent the session store instance from being garbage collected even after the test function has finished. You can prevent this by either disabling the cleanup goroutine altogether (as described above) or by stopping it using the `StopCleanup()` method. For example:

```go
func TestExample(t *testing.T) {
db, err := xorm.NewEngine("mysql", "root:123@/test?charset=utf8")
if err != nil {
t.Fatal(err)
}
defer db.Close()

store, err := xormstore.New(db)
if err != nil {
t.Fatal(err)
}
defer store.StopCleanup()

sessionManager = scs.New()
sessionManager.Store = store

// Run test...
}
```
18 changes: 18 additions & 0 deletions xormstore/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module github.com/alexedwards/scs/xormstore

go 1.21.4

require (
github.com/go-sql-driver/mysql v1.7.0
xorm.io/xorm v1.3.4
)

require (
github.com/goccy/go-json v0.8.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 // indirect
)
Loading