cqlstore

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

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

Go to latest
Published: May 12, 2015 License: MIT Imports: 7 Imported by: 0

README

cqlstore

GoDocTravis

A Cassandra implementation for github.com/gorilla/sessions.

Example and API references on GoDoc

Stability Note

This package has not yet been tested in a production environment. I do not expect any breaking changes or significant performance issues but until it has been tested in the wild this notice will remain.

Testing

Tests require an active Cassandra DB. You must use environment variables to specify the Cassandra URL and the name of a test keyspace. This keyspace will be created and dropped as part of the test suite.

CQLSTORE_KEYSPACE=foobar CQLSTORE_URL=dockerhost go test

Documentation

Overview

Package cqlstore provides an Apache Cassandra implementation of HTTP session storage for github.com/gorilla/sessions.

Example (Basic)

This example shows connecting to a Cassandra cluster, initalizing the CQLStore, then running a simple HTTP server that keeps track of the number of times the user has hit the page.

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gocql/gocql"
	"github.com/jcbwlkr/cqlstore"
)

func main() {
	// Connect to your Cassandra cluster
	cluster := gocql.NewCluster("192.168.59.103")
	cluster.Keyspace = "demo"
	dbSess, err := cluster.CreateSession()
	if err != nil {
		log.Fatalln(err)
	}
	defer dbSess.Close()

	// Create the CQLStore
	store, err := cqlstore.New(dbSess, "sessions", []byte("something-secret"))
	if err != nil {
		log.Fatalln(err)
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// Don't bump counter for things like /favicon.ico
		if r.URL.String() != "/" {
			return
		}

		// Get session named "demo-session" identified by the request's cookies
		session, err := store.Get(r, "demo-session")
		if err != nil {
			// Error loading existing session. Session might have expired,
			// their cookie was malformed, or there was a database issue.
			log.Println(err)
		}

		counter, ok := session.Values["counter"].(int)
		if !ok {
			counter = 0
		}
		session.Values["counter"] = counter + 1

		// Save session values to DB and add header to response to set cookie
		err = session.Save(r, w)
		if err != nil {
			// Error saving session. Probably pretty important.
			log.Println(err)
		}

		fmt.Fprintf(w, "I have seen you %d time(s)\n", session.Values["counter"])
	})

	http.ListenAndServe(":8080", nil)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CQLStore

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

CQLStore provides a Cassandra backed implementation of the interface Store from github.com/gorilla/sessions

func New

func New(cs *gocql.Session, table string, keypairs ...[]byte) (*CQLStore, error)

New creates a new CQLStore. It requires an active gocql.Session and the name of the table where it should store session data. It will create this table with the appropriate schema if it does not exist. Additionally pass one or more byte slices to serve as authentication and/or encryption keys for both the cookie's session ID value and the values stored in the database.

func (*CQLStore) Get

func (st *CQLStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get creates or returns a session from the request registry. It never returns a nil session.

func (*CQLStore) New

func (st *CQLStore) New(r *http.Request, name string) (*sessions.Session, error)

New creates and returns a new session without adding it to the registry. If the request has the named cookie then it will decode the session ID and load session values from the database. If the request might already have had the session loaded then calling Get instead will be faster. It never returns a nil session.

func (*CQLStore) Save

Save persists session values to the database and adds the session ID cookie to the request. Save must be called before writing the response or the cookie will not be sent.

Jump to

Keyboard shortcuts

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