Sessions
Server-side session management with 5 storage drivers.
RapidGo provides a session manager with multiple storage backends, flash messages, and form input preservation.
Configuration
SESSION_DRIVER=cookie # cookie, db, file, memory, redis
SESSION_LIFETIME=120 # Session lifetime in minutes
SESSION_SECRET=your-secret-key
SESSION_COOKIE=rapidgo_session # Cookie name
SESSION_SECURE=false # HTTPS-only cookies
SESSION_HTTPONLY=true # Prevent JavaScript access
SESSION_SAMESITE=lax # lax, strict, none
Available Drivers
| Driver | Description | Best For |
|---|---|---|
cookie |
Encrypted client-side cookies | Simple apps, no server storage needed |
db |
Database-backed (GORM) | Production with existing database |
file |
File system storage | Single-server deployments |
memory |
In-process memory | Development and testing |
redis |
Redis-backed | Multi-server production deployments |
Session Manager API
import "github.com/RAiWorks/RapidGo/v2/core/session"
// Create a store from SESSION_DRIVER env
store, err := session.NewStore(db)
// Create the manager
manager := session.NewManager(store)
Start a Session
id, data, err := manager.Start(r) // r = *http.Request
// id — session ID (from cookie or newly generated)
// data — session data map
Save a Session
data["user_id"] = user.ID
err := manager.Save(w, id, data) // w = http.ResponseWriter
Destroy a Session
err := manager.Destroy(w, id) // Clears data and cookie
Flash Messages
Flash messages are available for one request only — useful for success/error notifications after redirects.
// Set a flash message
manager.Flash(data, "success", "Your profile has been updated.")
// Read and remove a flash message
msg, ok := manager.GetFlash(data, "success")
Form Input Preservation
Re-populate form fields after validation failure:
// Store old input
manager.FlashOldInput(data, map[string]string{
"name": c.PostForm("name"),
"email": c.PostForm("email"),
})
// Store validation errors
manager.FlashErrors(data, validator.Errors())
Session Middleware
The session middleware automatically starts and saves sessions:
r.Use(middleware.Session())