qlstore

package module
Version: v0.0.0-...-646d93e Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2016 License: MIT Imports: 7 Imported by: 2

README

qlstore

GoDoc Build Status Coverage Status Go Report Card

Implements gorilla/sessions store using embedded sql database ( ql)

installation

go get github.com/gernest/qlstore

Usage


package main

import (
	"database/sql"
	"log"
	"net/http"

	"github.com/gernest/qlstore"
	// load ql drier
	_ "github.com/cznic/ql/driver"
)

var keyPair = [][]byte{
	[]byte("ePAPW9vJv7gHoftvQTyNj5VkWB52mlza"),
	[]byte("N8SmpJ00aSpepNrKoyYxmAJhwVuKEWZD"),
}

func main() {

	db, err := sql.Open("ql-mem", "testing.db")
	if err != nil {
		log.Fatal(err)
	}

	// This is a convenient helper. It creates the session table if the table
	// doesnt exist yet.
	err = qlstore.Migrate(db)
	if err != nil {
		log.Fatal(err)
	}

	store := qlstore.NewQLStore(db, "/", 2592000, keyPair...)
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Get a session. We're ignoring the error resulted from decoding an
		// existing session: Get() always returns a session, even if empty.
		session, _ := store.Get(r, "session-name")
		// Set some session values.
		session.Values["foo"] = "bar"
		session.Values[42] = 43
		// Save it before we write to the response/return from the handler.
		session.Save(r, w)
	})
	log.Fatal(http.ListenAndServe(":8090", nil))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(db *sql.DB) error

Migrate creates the session table if the table does not exist yet.

Types

type QLStore

type QLStore struct {
	Codecs  []securecookie.Codec
	Options *sessions.Options
	// contains filtered or unexported fields
}

QLStore is the session storage implementation for gorilla/sessions using embedded SQL database(ql).

func NewQLStore

func NewQLStore(store *sql.DB, path string, maxAge int, keyPairs ...[]byte) *QLStore

NewQLStore initillizes QLStore with the given keyPairs

func (*QLStore) Delete

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

Delete deletes session.

func (*QLStore) Get

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

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

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

New returns a new session

func (*QLStore) Save

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

Save saves the session into a ql database

Source Files

Jump to

Keyboard shortcuts

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