crdbstore

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

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

Go to latest
Published: Jul 13, 2020 License: MIT Imports: 10 Imported by: 0

README

crdbstore

A session store backend for gorilla/sessions - src.

Installation

make get-deps

Documentation

Available on godoc.org.

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

Example
package main

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

	"github.com/stephenafamo/crdbstore"
)

var store *crdbstore.CrDBStore

func main() {
	var err error
	// Fetch new store.
	store, err = crdbstore.NewCrDBStore("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))

	http.HandleFunc("/", ExampleHandler)
	http.ListenAndServe(":8080", nil)
}

// ExampleHandler is an example that displays the usage of PGStore.
func ExampleHandler(w http.ResponseWriter, r *http.Request) {

	// 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)
	}
}

Thanks

This driver is mostly copied from the postgres driver which no longer works for cockroachDB.

What makes this backend different is that it's for CockroachDB.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CrDBSession

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

CrDBSession type

type CrDBStore

type CrDBStore struct {
	Codecs  []securecookie.Codec
	Options *sessions.Options
	Path    string
	DbPool  *sql.DB
}

CrDBStore represents the currently configured session store.

func NewCrDBStore

func NewCrDBStore(dbURL string, keyPairs ...[]byte) (*CrDBStore, error)

NewCrDBStore creates a new CrDBStore instance and a new database/sql pool. This will also create in the database the schema needed by CrDBStore.

func NewCrDBStoreFromPool

func NewCrDBStoreFromPool(db *sql.DB, keyPairs ...[]byte) (*CrDBStore, error)

NewCrDBStoreFromPool creates a new CrDBStore instance from an existing database/sql pool. This will also create the database schema needed by CrDBStore.

func (*CrDBStore) Cleanup

func (db *CrDBStore) 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 (*CrDBStore) Close

func (db *CrDBStore) Close()

Close closes the database connection.

func (*CrDBStore) Get

func (db *CrDBStore) 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 (*CrDBStore) MaxAge

func (db *CrDBStore) 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 (*CrDBStore) MaxLength

func (db *CrDBStore) 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 CrDBStore is 4096. PostgreSQL allows for max value sizes of up to 1GB (http://www.postgresql.org/docs/current/interactive/datatype-character.html)

func (*CrDBStore) New

func (db *CrDBStore) 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 (*CrDBStore) Save

func (db *CrDBStore) 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 (*CrDBStore) StopCleanup

func (db *CrDBStore) 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