package store import ( "database/sql" "time" "github.com/alexedwards/scs/postgresstore" "github.com/alexedwards/scs/v2" ) const sessionsSchemaPostgres = ` CREATE TABLE IF NOT EXISTS sessions ( token TEXT PRIMARY KEY, data BYTEA NOT NULL, expiry TIMESTAMPTZ NOT NULL ); CREATE INDEX IF NOT EXISTS sessions_expiry_idx ON sessions (expiry); ` // applySessionsSchema creates the scs sessions table if missing. func applySessionsSchema(db *sql.DB) error { return applySchema(db, sessionsSchemaPostgres) } type sessionStopper interface { StopCleanup() } // SessionStore wraps scs Postgres session persistence and cleanup. type SessionStore struct { store scs.Store stopper sessionStopper } // NewSessionStore starts a postgresstore with the given cleanup interval. func NewSessionStore(db *sql.DB, cleanupInterval time.Duration) *SessionStore { ps := postgresstore.NewWithCleanupInterval(db, cleanupInterval) return &SessionStore{store: ps, stopper: ps} } // Store returns the scs.Store implementation. func (s *SessionStore) Store() scs.Store { return s.store } // Close stops background session cleanup. func (s *SessionStore) Close() { if s == nil || s.stopper == nil { return } s.stopper.StopCleanup() s.stopper = nil }