Skip to content

Sessions

Use sessions.Start() to start a session.

lib/handlers/welcome.go
package handlers
import (
"github.com/razshare/frizzante/connections"
"github.com/razshare/frizzante/sessions"
)
type State struct{
Name string
}
func Welcome(con *connections.Connection) {
session := sessions.Start(con, State{})
}

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

Use Save() to save the session state.

lib/handlers/welcome.go
package handlers
import (
"github.com/razshare/frizzante/connections"
"github.com/razshare/frizzante/sessions"
)
type State struct{
Name string
}
func Welcome(con *connections.Connection) {
session := sessions.Start(con, State{})
defer session.Save()
session.Name = "World"
}

Use Load() to load the session state.

lib/handlers/welcome.go
package handlers
import (
"github.com/razshare/frizzante/connections"
"github.com/razshare/frizzante/sessions"
)
type State struct{
Name string
}
func Welcome(con *connections.Connection) {
session := sessions.Start(con, State{})
session.Load()
con.SendMessage("Hello " + session.State.Name)
}

Use Destroy() to destroy a session.

lib/handlers/welcome.go
package handlers
import (
"github.com/razshare/frizzante/connections"
"github.com/razshare/frizzante/sessions"
)
type State struct{
Name string
}
func Welcome(con *connections.Connection) {
session := sessions.Start(con, State{})
session.Destroy()
}

Sessions are managed through an Archive and you can overwrite the default Archive using sessions.WithArchive().

main.go
import "github.com/razshare/frizzante/sessions"
func main(){
sessions.WithArchive(&CustomArchive{})
}

Where CustomArchive implements Archive.

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 an Archive as a repository of data separated in two layers; domains and keys.

An archive contains domains which contain keys.

  • Directoryarchive
    • Directorydomain-1
      • key-a
      • key-b
    • Directorydomain-2
      • key-x
      • key-y

The default operator archive is backed by the file system, more specifically, by a .gen/sessions directory.

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

Each subdirectory (domain) takes the id of a session and it contains the state in a session.json file (key), thus, there are as many subdirectories as there are sessions.