Skip to content

Commit

Permalink
Merge pull request #235 from vsimakhin/feature/db-sessions-store
Browse files Browse the repository at this point in the history
store sessions in the database
  • Loading branch information
vsimakhin authored Jun 2, 2024
2 parents 1baafb3 + 2bd67cb commit 3fff58f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [Unreleased]

- Update: Session manager now stores tokens in the database instead of memory. In this case it's possible to run the application in the Cloud platforms (AWS ECS, Google Cloud Run, Kubernetes) without always keeping it live and active.

## [2.36.0] - 01.06.2024

- Update: Slightly updated the build script to support v6 and v7 platforms for ARM32 binaries.
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ You also can easily export all flight records into EASA style pdf format, print

# Changelog

## [Unreleased]

- Update: Session manager now stores tokens in the database instead of memory. In this case it's possible to run the application in the Cloud platforms (AWS ECS, Google Cloud Run, Kubernetes) without always keeping it live and active.

## [2.36.0] - 01.06.2024

- Update: Slightly updated the build script to support v6 and v7 platforms for ARM32 binaries.
Expand Down
16 changes: 14 additions & 2 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"os"
"time"

"github.com/alexedwards/scs/mysqlstore"
"github.com/alexedwards/scs/sqlite3store"
"github.com/alexedwards/scs/v2"
"github.com/vsimakhin/web-logbook/internal/driver"
"github.com/vsimakhin/web-logbook/internal/models"
Expand Down Expand Up @@ -123,6 +125,17 @@ func createDBConnection(engine string, dsn string) (*sql.DB, error) {
return conn, nil
}

func createSessionManager(conn *sql.DB, engine string) *scs.SessionManager {
session := scs.New()
session.Lifetime = 12 * time.Hour
if engine == "sqlite" {
session.Store = sqlite3store.New(conn)
} else {
session.Store = mysqlstore.New(conn)
}
return session
}

func main() {
var err error

Expand Down Expand Up @@ -151,8 +164,7 @@ func main() {
defer conn.Close()

// set up session
session = scs.New()
session.Lifetime = 12 * time.Hour
session = createSessionManager(conn, cfg.db.engine)

app := &application{
config: cfg,
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ require (

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/alexedwards/scs/mysqlstore v0.0.0-20240316134038-7e11d57e8885
github.com/alexedwards/scs/sqlite3store v0.0.0-20240316134038-7e11d57e8885
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/alexedwards/scs/mysqlstore v0.0.0-20240316134038-7e11d57e8885 h1:C7QAamNjR5yz6di4KJWAKcnxueKBgq4L/JGXhlnu35w=
github.com/alexedwards/scs/mysqlstore v0.0.0-20240316134038-7e11d57e8885/go.mod h1:p8jK3D80sw1PFrCSdlcJF1O75bp55HqbgDyyCLM0FrE=
github.com/alexedwards/scs/sqlite3store v0.0.0-20240316134038-7e11d57e8885 h1:+DCxWg/ojncqS+TGAuRUoV7OfG/S4doh0pcpAwEcow0=
github.com/alexedwards/scs/sqlite3store v0.0.0-20240316134038-7e11d57e8885/go.mod h1:Iyk7S76cxGaiEX/mSYmTZzYehp4KfyylcLaV3OnToss=
github.com/alexedwards/scs/v2 v2.8.0 h1:h31yUYoycPuL0zt14c0gd+oqxfRwIj6SOjHdKRZxhEw=
github.com/alexedwards/scs/v2 v2.8.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand All @@ -13,6 +17,7 @@ github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-pdf/fpdf v0.9.0 h1:PPvSaUuo1iMi9KkaAn90NuKi+P4gwMedWPHhj8YlJQw=
github.com/go-pdf/fpdf v0.9.0/go.mod h1:oO8N111TkmKb9D7VvWGLvLJlaZUQVPM+6V42pp3iV4Y=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo=
Expand All @@ -31,6 +36,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/nathan-osman/go-sunrise v1.1.0 h1:ZqZmtmtzs8Os/DGQYi0YMHpuUqR/iRoJK+wDO0wTCw8=
Expand Down
6 changes: 6 additions & 0 deletions internal/driver/db_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ var attachmentsTable = NewTable("attachments", "uuid", UUID,
{Name: "document", Type: Blob},
})

var sessionsTable = NewTable("sessions", "token", ColumnType{SQLite: "TEXT", MySQL: "CHAR(43)"},
[]Column{
{Name: "data", Type: ColumnType{SQLite: "BLOB", MySQL: "BLOB"}, Properties: "NOT NULL"},
{Name: "expiry", Type: ColumnType{SQLite: "REAL", MySQL: "TIMESTAMP(6)"}, Properties: "NOT NULL"},
})

var logbookView = NewView("logbook_view",
SQLQuery{
SQLite: `
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func OpenDB(engine string, dsn string) (*sql.DB, error) {
func validateDB(db *sql.DB, engine string) error {
// check tables
tables := []*Table{logbookTable, airportsTable, customAirportsTable,
settingsTable, licensingTable, attachmentsTable,
settingsTable, licensingTable, attachmentsTable, sessionsTable,
}

for _, table := range tables {
Expand Down

0 comments on commit 3fff58f

Please sign in to comment.