sqlstore

package module
v0.0.0-...-4b7df27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2021 License: MIT Imports: 9 Imported by: 0

README

sqlstore

SQL session store for the Gorilla web toolkit.

Work in progress, API may not be stable. Currently only supports the PostgreSQL dialect for either the stdlib database/sql package or the pgx driver.

GoDoc

Build Status

Usage

See the Gorilla toolkit sessions documentation on how to use the sessions package.

Get the package go get github.com/jstemmer/sqlstore and import github.com/jstemmer/sqlstore. Depending on which database implementation you want to use either import github.com/jstemmer/sqlstore/pgdb or github.com/jstemmer/sqlstore/pgxdb.

Call sqlstore.New() to create a new store and provide it with a database implementation and keyPairs. See the session.NewCookieStore documentation on how to use the keyPairs parameter.

For example:

store := sqlstore.New(pgdb.New(db), []byte("something-very-secret"))

Sessions are stored in a sessions table, which is assumed to exist. It should have the following schema:

CREATE TABLE sessions (
	id varchar(100) PRIMARY KEY,
	data bytea NOT NULL,
	created_at timestamp NOT NULL,
	updated_at timestamp NOT NULL
);

License

MIT, see LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database interface {
	Load(ctx context.Context, id string) (updatedAt time.Time, data []byte, err error)
	Insert(ctx context.Context, id string, data []byte) error
	Update(ctx context.Context, id string, data []byte) error
	Delete(ctx context.Context, id string) error
}

Database describes a session store database interface.

type SQLStore

type SQLStore struct {
	Options *sessions.Options
	// contains filtered or unexported fields
}

SQLStore stores gorilla sessions in a database.

func New

func New(db Database, keyPairs ...[]byte) *SQLStore

New returns a new SQLStore. The keyPairs are used in the same way as the gorilla sessions CookieStore.

func (*SQLStore) Get

func (s *SQLStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get returns a cached session.

func (*SQLStore) New

func (s *SQLStore) New(r *http.Request, name string) (*sessions.Session, error)

New creates a new session for the given request r. If the request contains a valid session ID for an existing, non-expired session, then this session will be loaded from the database.

func (*SQLStore) Save

func (s *SQLStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error

Save stores the session in the database. If session.Options.MaxAge is < 0, the session is deleted from the database.

Directories

Path Synopsis
Package pgdb provides a sqlstore.Database implementation for a PostgreSQL database accessed through the standard library database/sql package.
Package pgdb provides a sqlstore.Database implementation for a PostgreSQL database accessed through the standard library database/sql package.
Package pgxdb provides a sqlstore.Database implementation for a PostgreSQL database accessed through the github.com/jackc/pgx package.
Package pgxdb provides a sqlstore.Database implementation for a PostgreSQL database accessed through the github.com/jackc/pgx package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL