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

Add support for mysql backend #263

Open
wants to merge 6 commits 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
101 changes: 73 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# sessions

[![Run CI Lint](https://github.com/gin-contrib/sessions/actions/workflows/lint.yml/badge.svg?branch=master)](https://github.com/gin-contrib/sessions/actions/workflows/lint.yml)
[![Run Testing](https://github.com/gin-contrib/sessions/actions/workflows/testing.yml/badge.svg?branch=master)](https://github.com/gin-contrib/sessions/actions/workflows/testing.yml)
[![Run CI Lint](https://github.com/weisskopfjens/sessions/actions/workflows/lint.yml/badge.svg?branch=master)](https://github.com/weisskopfjens/sessions/actions/workflows/lint.yml)
[![Run Testing](https://github.com/weisskopfjens/sessions/actions/workflows/testing.yml/badge.svg?branch=master)](https://github.com/weisskopfjens/sessions/actions/workflows/testing.yml)
[![codecov](https://codecov.io/gh/gin-contrib/sessions/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/sessions)
[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/sessions)](https://goreportcard.com/report/github.com/gin-contrib/sessions)
[![GoDoc](https://godoc.org/github.com/gin-contrib/sessions?status.svg)](https://godoc.org/github.com/gin-contrib/sessions)
[![Go Report Card](https://goreportcard.com/badge/github.com/weisskopfjens/sessions)](https://goreportcard.com/report/github.com/weisskopfjens/sessions)
[![GoDoc](https://godoc.org/github.com/weisskopfjens/sessions?status.svg)](https://godoc.org/github.com/weisskopfjens/sessions)

Gin middleware for session management with multi-backend support:

Expand All @@ -15,6 +15,7 @@ Gin middleware for session management with multi-backend support:
- [GORM](#gorm)
- [memstore](#memstore)
- [PostgreSQL](#postgresql)
- [MySQL](#mysql)

## Usage

Expand All @@ -23,13 +24,13 @@ Gin middleware for session management with multi-backend support:
Download and install it:

```bash
go get github.com/gin-contrib/sessions
go get github.com/weisskopfjens/sessions
```

Import it in your code:

```go
import "github.com/gin-contrib/sessions"
import "github.com/weisskopfjens/sessions"
```

## Basic Examples
Expand All @@ -40,8 +41,8 @@ import "github.com/gin-contrib/sessions"
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/cookie"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -70,8 +71,8 @@ func main() {
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/cookie"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -112,8 +113,8 @@ func main() {
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/cookie"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -146,8 +147,8 @@ func main() {
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/redis"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/redis"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -183,8 +184,8 @@ package main

import (
"github.com/bradfitz/gomemcache/memcache"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memcached"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/memcached"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -217,8 +218,8 @@ func main() {
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memcached"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/memcached"
"github.com/gin-gonic/gin"
"github.com/memcachier/mc"
)
Expand Down Expand Up @@ -255,8 +256,8 @@ func main() {
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/mongo/mongomgo"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/mongo/mongomgo"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo"
)
Expand Down Expand Up @@ -297,8 +298,8 @@ package main

import (
"context"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/mongo/mongodriver"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/mongo/mongodriver"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
Expand Down Expand Up @@ -344,8 +345,8 @@ func main() {
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memstore"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/memstore"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -378,8 +379,8 @@ func main() {
package main

import (
"github.com/gin-contrib/sessions"
gormsessions "github.com/gin-contrib/sessions/gorm"
"github.com/weisskopfjens/sessions"
gormsessions "github.com/weisskopfjens/sessions/gorm"
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
Expand Down Expand Up @@ -420,8 +421,8 @@ package main

import (
"database/sql"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/postgres"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/postgres"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -456,3 +457,47 @@ func main() {
r.Run(":8000")
}
```

### MySQL

```go
package main

import (
"database/sql"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/mysql"
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
db, err := sql.Open("mysql", "testuser:testpass@tcp(localhost:3306)/testdb?parseTime=true&loc=Local")
if err != nil {
// handle err
}

store, err := mysql.NewStore(db, []byte("secret"))
if err != nil {
// handle err
}

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")
}
```
4 changes: 2 additions & 2 deletions _example/cookie/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/cookie"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions _example/gorm/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/gin-contrib/sessions"
gormsessions "github.com/gin-contrib/sessions/gorm"
"github.com/gin-gonic/gin"
"github.com/weisskopfjens/sessions"
gormsessions "github.com/weisskopfjens/sessions/gorm"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
Expand Down
4 changes: 2 additions & 2 deletions _example/memcached/ascii/ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"github.com/bradfitz/gomemcache/memcache"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memcached"
"github.com/gin-gonic/gin"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/memcached"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions _example/memcached/binary/binary.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memcached"
"github.com/gin-gonic/gin"
"github.com/memcachier/mc"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/memcached"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions _example/memstore/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memstore"
"github.com/gin-gonic/gin"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/memstore"
)

func main() {
Expand Down
5 changes: 3 additions & 2 deletions _example/mongo/mongodriver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"context"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/mongo/mongodriver"

"github.com/gin-gonic/gin"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/mongo/mongodriver"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
Expand Down
4 changes: 2 additions & 2 deletions _example/mongo/mongomgo/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/mongo/mongomgo"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/mongo/mongomgo"
)

func main() {
Expand Down
5 changes: 3 additions & 2 deletions _example/postgres/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"database/sql"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/postgres"

"github.com/gin-gonic/gin"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/postgres"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions _example/redis/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/redis"
"github.com/gin-gonic/gin"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/redis"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cookie/cookie.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cookie

import (
"github.com/gin-contrib/sessions"
gsessions "github.com/gorilla/sessions"
"github.com/weisskopfjens/sessions"
)

type Store interface {
Expand Down
4 changes: 2 additions & 2 deletions cookie/cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cookie
import (
"testing"

"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/tester"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/tester"
)

var newStore = func(_ *testing.T) sessions.Store {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/gin-contrib/sessions
module github.com/weisskopfjens/sessions

go 1.18

Expand All @@ -17,6 +17,7 @@ require (
github.com/memcachier/mc v2.0.1+incompatible
github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b
github.com/wader/gormstore/v2 v2.0.3
github.com/weisskopfjens/mysqlstore v0.0.0-20231123182912-ffbca72c0a70
go.mongodb.org/mongo-driver v1.14.0
gorm.io/driver/sqlite v1.4.4
gorm.io/gorm v1.25.8
Expand All @@ -31,6 +32,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.19.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65E
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/wader/gormstore/v2 v2.0.3 h1:/29GWPauY8xZkpLnB8hsp+dZfP3ivA9fiDw1YVNTp6U=
github.com/wader/gormstore/v2 v2.0.3/go.mod h1:sr3N3a8F1+PBc3fHoKaphFqDXLRJ9Oe6Yow0HxKFbbg=
github.com/weisskopfjens/mysqlstore v0.0.0-20231123182912-ffbca72c0a70 h1:tbGFByXJxvWcVaacC7/MYBmjq1hHiPTV5sOyZveZ8Rg=
github.com/weisskopfjens/mysqlstore v0.0.0-20231123182912-ffbca72c0a70/go.mod h1:/n6W/7x3SCW1V2iXh+ns0EROh/KwKjReppbXGQ37hIs=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
Expand Down
2 changes: 1 addition & 1 deletion gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package gorm
import (
"time"

"github.com/gin-contrib/sessions"
"github.com/wader/gormstore/v2"
"github.com/weisskopfjens/sessions"
"gorm.io/gorm"
)

Expand Down
4 changes: 2 additions & 2 deletions gorm/gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package gorm
import (
"testing"

"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/tester"
"github.com/weisskopfjens/sessions"
"github.com/weisskopfjens/sessions/tester"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
Expand Down
Loading