This is a simple session manager for a go application. Helps you to store information of user and manage it, set a expiration time and set a default session of a group of different sessions created.
Visit the GoDoc page for the full documentation.
Install the package using the go get command:
go get github.com/solrac97gr/session-manager
Import the package in your code:
import "github.com/solrac97gr/session-manager"
And use it:
package main
import (
"fmt"
"net/http"
"github.com/solrac97gr/session-manager"
)
func main() {
user := struct{
Name string
Age int
}{
Name: "Solrac",
Age: 20,
}{}
sm := sessionmanager.NewSessionManager()
// Create a new session
s,err := sm.CreateSession()
if err != nil {
panic(err)
}
s.Set("user", user)
fmt.Println(s.Get("user"))
}
package main
import (
"fmt"
"net/http"
"github.com/solrac97gr/session-manager"
)
func main() {
sm := sessionmanager.NewSessionManager()
// Get a session
s,err := sm.GetSession("session-id")
if err != nil {
panic(err)
}
fmt.Println(s.Get("user"))
}
package main
import (
"fmt"
"net/http"
"github.com/solrac97gr/session-manager"
)
func main() {
sm := sessionmanager.NewSessionManager()
// Destroy a session
err := sm.DestroySession("session-id")
if err != nil {
panic(err)
}
}
package main
import (
"fmt"
"net/http"
"github.com/solrac97gr/session-manager"
)
func main() {
user := struct{
Name string
Age int
}{
Name: "Solrac",
Age: 20,
}
sm := sessionmanager.NewSessionManager()
s,_:= sm.CreateSession()
s.Set("user", user)
// Set a default session
sm.SetAsDefaultSession(s.SessionId())
// Get a session
s,err := sm.GetDefaultSession()
if err != nil {
panic(err)
}
fmt.Println(s.Get("user"))
}
By default, the session manager does not avoid expired sessions, so if you want to avoid expired sessions, you must activate it.
The defatul expiration time is 5 minutes, so if you want to change expiration time, you must set it.
package main
import (
"fmt"
"net/http"
"github.com/solrac97gr/session-manager"
)
func main() {
user := struct{
Name string
Age int
}{
Name: "Solrac",
Age: 20,
}
sm := sessionmanager.NewSessionManager()
// Activate avoid expired sessions
sm.SetAvoidExpiredSessions(true)
s,_:= sm.CreateSession()
s.Set("user", user)
s.SetExpirationTime(time.Now().Add(1440 * time.Minute))
// Set a default session
sm.SetAsDefaultSession(s.SessionId())
// Get a session
s,err := sm.GetDefaultSession()
if err != nil {
panic(err)
}
fmt.Println(s.Get("user"))
}
- Create a new session
- Get a session
- Delete a session
- Use concurrent map
- Add a session expiration time
- Add a active indicator
MIT License