Sessions
Use sessions.Start()
to start a session.
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.
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.
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)}
Destroy
Section titled “Destroy”Use Destroy()
to destroy a session.
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()}
Custom Archive
Section titled “Custom Archive”Sessions are managed through an Archive
and
you can overwrite the default Archive
using sessions.WithArchive()
.
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}
Archive Details
Section titled “Archive Details”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
Default Archive
Section titled “Default Archive”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
{ "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.