pgxstore

package module
v0.0.0-...-b8c8a74 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2021 License: MIT Imports: 12 Imported by: 0

README

pgxstore

A session store backend for gorilla/sessions - src.

Forked from antonlindstrom/pgstore and modified to use jackc/pgx instead of database/sql.

From the pgx docs:

It is recommended to use the pgx interface if:

  1. The application only targets PostgreSQL.
  2. No other libraries that require database/sql are in use.

Installation

go get github.com/yi-jiayu/pgxstore

Documentation

Available on godoc.org.

See http://www.gorillatoolkit.org/pkg/sessions for full documentation on underlying interface.

Example
package examples

import (
	"log"
	"net/http"
	"time"

	"github.com/yi-jiayu/pgxstore"
)

// ExampleHandler is an example that displays the usage of PGStore.
func ExampleHandler(w http.ResponseWriter, r *http.Request) {
	// Fetch new store.
	store, err := pgstore.NewPGStore("postgres://user:password@127.0.0.1:5432/database?sslmode=verify-full", []byte("secret-key"))
	if err != nil {
		log.Fatalf(err.Error())
	}
	defer store.Close()

	// Run a background goroutine to clean up expired sessions from the database.
	defer store.StopCleanup(store.Cleanup(time.Minute * 5))

	// Get a session.
	session, err := store.Get(r, "session-key")
	if err != nil {
		log.Fatalf(err.Error())
	}

	// Add a value.
	session.Values["foo"] = "bar"

	// Save.
	if err = session.Save(r, w); err != nil {
		log.Fatalf("Error saving session: %v", err)
	}

	// Delete session.
	session.Options.MaxAge = -1
	if err = session.Save(r, w); err != nil {
		log.Fatalf("Error saving session: %v", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PGSession

type PGSession struct {
	ID         int64
	Key        string
	Data       string
	CreatedOn  time.Time
	ModifiedOn time.Time
	ExpiresOn  time.Time
}

PGSession type

type PGStore

type PGStore struct {
	Codecs  []securecookie.Codec
	Options *sessions.Options
	Path    string
	Conn    pgxtype.Querier
}

PGStore represents the currently configured session store.

func NewPGStore

func NewPGStore(dbURL string, keyPairs ...[]byte) (*PGStore, error)

NewPGStore creates a new PGStore instance and a new pgxpool.Pool. This will also create in the database the schema needed by pgstore.

func NewPGStoreFromConn

func NewPGStoreFromConn(conn pgxtype.Querier, keyPairs ...[]byte) (*PGStore, error)

NewPGStoreFromConn creates a new PGStore instance from an existing database connection. This will also create the database schema needed by pgstore.

func (*PGStore) Cleanup

func (db *PGStore) Cleanup(interval time.Duration) (chan<- struct{}, <-chan struct{})

Cleanup runs a background goroutine every interval that deletes expired sessions from the database.

The design is based on https://github.com/yosssi/boltstore

func (*PGStore) Close

func (db *PGStore) Close()

Close closes the underlying database connection if it is an instance of *pgx.Conn or *pgxpool.Pool. It is not necessary to call Close if the store was created with an existing connection that will be closed separately.

func (*PGStore) Get

func (db *PGStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get Fetches a session for a given name after it has been added to the registry.

func (*PGStore) MaxAge

func (db *PGStore) MaxAge(age int)

MaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting Options.MaxAge = -1 for that session.

func (*PGStore) MaxLength

func (db *PGStore) MaxLength(l int)

MaxLength restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default for a new PGStore is 4096. PostgreSQL allows for max value sizes of up to 1GB (http://www.postgresql.org/docs/current/interactive/datatype-character.html)

func (*PGStore) New

func (db *PGStore) New(r *http.Request, name string) (*sessions.Session, error)

New returns a new session for the given name without adding it to the registry.

func (*PGStore) Save

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

Save saves the given session into the database and deletes cookies if needed

func (*PGStore) StopCleanup

func (db *PGStore) StopCleanup(quit chan<- struct{}, done <-chan struct{})

StopCleanup stops the background cleanup from running.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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