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 ¶
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 ¶
Get creates or returns a session from the request registry. It never returns a nil session.
func (*CQLStore) New ¶
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.