From 12066bcbeb21a4751d57000237175ff924ab7eea Mon Sep 17 00:00:00 2001 From: Prokopenko Andrey Date: Fri, 2 Jul 2021 16:47:22 +0200 Subject: [PATCH] added sqlite store --- go.mod | 3 ++- go.sum | 12 ++++++++-- sqlite/sqlite.go | 28 ++++++++++++++++++++++ sqlite/sqlite_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 sqlite/sqlite.go create mode 100644 sqlite/sqlite_test.go diff --git a/go.mod b/go.mod index a792c60..ba8fc5d 100644 --- a/go.mod +++ b/go.mod @@ -10,8 +10,9 @@ require ( github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 github.com/gomodule/redigo v2.0.0+incompatible github.com/gorilla/context v1.1.1 - github.com/gorilla/sessions v1.2.0 + github.com/gorilla/sessions v1.2.1 github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b github.com/memcachier/mc v2.0.1+incompatible github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b + github.com/terem42/sqlite3store v1.0.0 ) diff --git a/go.sum b/go.sum index 2f3d565..74fc02f 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +crawshaw.io/iox v0.0.0-20181124134642-c51c3df30797 h1:yDf7ARQc637HoxDho7xjqdvO5ZA2Yb+xzv/fOnnvZzw= +crawshaw.io/iox v0.0.0-20181124134642-c51c3df30797/go.mod h1:sXBiorCo8c46JlQV3oXPKINnZ8mcqnye1EkVkqsectk= +crawshaw.io/sqlite v0.3.2 h1:N6IzTjkiw9FItHAa0jp+ZKC6tuLzXqAYIv+ccIWos1I= +crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff h1:RmdPFa+slIr4SCBg4st/l/vZWVe9QJKMXGO60Bxbe04= github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff/go.mod h1:+RTT1BOk5P97fT2CiHkbFQwkK3mjsFAP6zCYV2aXtjw= github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b h1:L/QXpzIa3pOvUGt1D1lA5KjYhPBAN/3iWdP7xeFS9F0= @@ -32,8 +36,8 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51 github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/sessions v1.1.1/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w= -github.com/gorilla/sessions v1.2.0 h1:S7P+1Hm5V/AT9cjEcUD5uDaQSX0OE577aCXgoaKpYbQ= -github.com/gorilla/sessions v1.2.0/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= +github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI= +github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b h1:TLCm7HR+P9HM2NXaAJaIiHerOUMedtFJeAfaYwZ8YhY= @@ -48,6 +52,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b h1:aUNXCGgukb4gtY99imuIeoh8Vr0GSwAlYxPAhqZrpFc= @@ -57,6 +63,8 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/terem42/sqlite3store v1.0.0 h1:6q+yNxFLFBp0hqAt2THswusdLOkqSj/E/Oaqeipk4BU= +github.com/terem42/sqlite3store v1.0.0/go.mod h1:BULc/GWhyahcGF3WkvFrqNyR0frMhuNhfI+h9P/fXqw= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= diff --git a/sqlite/sqlite.go b/sqlite/sqlite.go new file mode 100644 index 0000000..9765749 --- /dev/null +++ b/sqlite/sqlite.go @@ -0,0 +1,28 @@ +package sqlite + +import ( + "net/http" + + "github.com/gin-contrib/sessions" + "github.com/terem42/sqlite3store" +) + +type Store interface { + sessions.Store +} + +func NewStore(db_file_location string, db_pool_size int, db_table_name string, maxAge int, keyPairs ...[]byte) (Store, error) { + s, err := sqlite3store.NewSqliteStore(db_file_location, db_pool_size, db_table_name, "/", "", false, false, http.SameSiteNoneMode, maxAge, keyPairs...) + if err != nil { + return nil, err + } + return &store{s}, nil +} + +type store struct { + *sqlite3store.SqliteStore +} + +func (c *store) Options(options sessions.Options) { + c.SqliteStore.Options = options.ToGorillaOptions() +} diff --git a/sqlite/sqlite_test.go b/sqlite/sqlite_test.go new file mode 100644 index 0000000..4ce6e4d --- /dev/null +++ b/sqlite/sqlite_test.go @@ -0,0 +1,54 @@ +package sqlite + +import ( + "os" + "testing" + + "github.com/gin-contrib/sessions" + "github.com/gin-contrib/sessions/tester" +) + +var newStore = func(t *testing.T) sessions.Store { + store, err := NewStore("test.db", 10, "sessions", 600, []byte("secret-key")) + if err != nil { + t.Fatal(err.Error()) + } + + return store +} + +func TestSqlite_SessionGetSet(t *testing.T) { + tester.GetSet(t, newStore) + t.Cleanup(cleanup) +} + +func TestSqlite_SessionDeleteKey(t *testing.T) { + tester.DeleteKey(t, newStore) + t.Cleanup(cleanup) +} + +func TestSqlite_SessionFlashes(t *testing.T) { + tester.Flashes(t, newStore) + t.Cleanup(cleanup) +} + +func TestSqlite_SessionClear(t *testing.T) { + tester.Clear(t, newStore) + t.Cleanup(cleanup) +} + +func TestSqlite_SessionOptions(t *testing.T) { + tester.Options(t, newStore) + t.Cleanup(cleanup) +} + +func TestSqlite_SessionMany(t *testing.T) { + tester.Many(t, newStore) + t.Cleanup(cleanup) +} + +func cleanup() { + os.Remove("test.db") + os.Remove("test.db-shm") + os.Remove("test.db-wal") +}