# README
Session Manager for Go 📂
This is a simple session manager for a go application. For example, you can use it to manage the user's login status.
Helps to manage user login information in the standard library of go. A lots of frameworks have their own session management, but some times I ue the standard library of go, so I write this package.
Documentation
Visit the GoDoc page for the full documentation.
Usage
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:
Example: Create a new session
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"))
}
Example: Get a session
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"))
}
Example: Delete a session
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)
}
}
Example: Set a default session and get 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()
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"))
}
Example: Activated avoid expired sessions and set a expiration time in a session
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"))
}
Work in progress and completed
- Create a new session
- Get a session
- Delete a session
- Use concurrent map
- Add a session expiration time
- Add a active indicator
License
MIT License
# Functions
NewSession is the constructor for session by default expiration time is 30 minutes and the session is active you can edit this values by setting the ExpirationTime and Active fields.
NewSessionManager is the constructor for session manager.
# Structs
Session is the struct implementation for session ID is the unique id for session Data is the data for session.
SessionManager is the struct implementation for session manager.
# Interfaces
Session is the interface for session.
ISessionManager is the interface for session manager.