cqlstore: github.com/jcbwlkr/cqlstore Index | Examples | Files

package cqlstore

import "github.com/jcbwlkr/cqlstore"

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

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.

Code:

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

Index

Examples

Package Files

cqlstore.go

type CQLStore Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

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.

Package cqlstore imports 7 packages (graph). Updated 2019-01-23. Refresh now. Tools for package owners.