Skip to content

Sessions

Use Session() to start a session.

lib/handlers/hello.go
package handlers
import frz "github.com/razshare/frizzante"
type State struct{}
func GetHello(c *frz.Connection) {
state, operator := frz.Session(c, State{})
}

This will retrieve the user’s session or create a new one if none is found.

Session state and session operations are handled separately.

When invoking Session(), you’re given back a state and an operator.

Use the operator to save, reload or destroy the state.

lib/handlers/hello.go
package handlers
import frz "github.com/razshare/frizzante"
type State struct{
Name string
}
func GetHello(c *frz.Connection) {
state, operator := frz.Session(c, State{})
defer operator.Save(state)
state.Name = "world"
}

Sessions are managed through the operator’s Archive.

You can overwrite the default Archive of any operator using WithArchive().

lib/handlers/hello.go
package handlers
import frz "github.com/razshare/frizzante"
type State struct{
Name string
}
func GetHello(c *frz.Connection) {
state, operator := frz.Session(c, State{})
operator.WithArchive(MyCustomArchive)
defer operator.Save(state)
state.Name = "world"
}

An Archive is an interface defined as follows

type Archive interface {
Get(domain string, key string) ([]byte, error)
Set(domain string, key string, value []byte) error
Has(domain string, key string) (bool, error)
Remove(domain string, key string) error
HasDomain(domain string) (bool, error)
RemoveDomain(domain string) error
}

Think of it as a repository of data separated in two layers, domains and keys.

An archive contains domains which contains keys.

In the case of session management, a domain is identified by the session id, while the key is a file named session.json.

  • Directory.archive
    • Directorydomain-1
      • key-a.txt
      • key-b.txt
    • Directorydomain-2
      • key-x.txt
      • key-y.txt

The default sessions archive is backed by the file system.

In other words, the default sessions archive organizes sessions in a .sessions directory containing as many subdirectories as there are sessions.

Each subdirectory contains a session.json file which describes the state of each session.

  • Directory.sessions
    • Directory0d0d0c2f-6bb9-405f-6a41-d4fe9acf3dd2
      • session.json
    • Directory8a6d8cfa-2fc7-4dbb-6be9-beb823318303
      • session.json
./sessions/0d0d0c2f-6bb9-405f-6a41-d4fe9acf3dd2/session.json
{
"Name": "world"
}