Home

Awesome

Gorilla Session Arangodb

This small library will allow you to use arango db to store user sessions in a document collection.

First create the collection that you want to store the sessions in. You can use arangosh to do this:

db._create( 'sessions', {
    
    //You don't need this. Actually, make this false if you want sessions to persist across database restarts
    "isVolatile": true,

    //AllowUserKeys can be false too. This library lets arangodb to decide session keys right now by using the autogenerated _key
    //attribute. In the future maybe I'll add an option to allow creation from the library side
    "allowUserKeys": true 
}, 'document' )

Now in go code you can save sessions like so:

store, err := NewArangoDbStore(&ArangoDbOptions{
    CollectionName: "sessions", //the name of the collection from above
    Host: "http://localhost:8529", //where to connect to
    DatabaseName: "_system", //the database to use
    User: "root", //user to connect as
    Password: "",
}, []byte("secret-encryption-key-thingy"))

func (f HandlerFunc) ServeHTTP(w ResponseWriter, request *Request) {
    session, err := store.Get( request, "GO_SESS_ID" )
    // Set some session values.
    session.Values["foo"] = "bar"
    session.Values[42] = 43

    session.Save( r, w )
    //OR
    store.Save( r, w, session )
}

The session data will be stored in the collection under the "session-data" key. Sorry, no way for me to switch that key name on the fly right now.

This library also currently relies on another library I'm writing github.com/starJammer/arango. It's a REST API for arangodb written in go. Maybe in the future I will remove this dependency and only use the http package.