Skip to content

Instantly share code, notes, and snippets.

@alexedwards
Last active May 23, 2020 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexedwards/29df406a09f9a9e8af98fec959707733 to your computer and use it in GitHub Desktop.
Save alexedwards/29df406a09f9a9e8af98fec959707733 to your computer and use it in GitHub Desktop.
package main
import (
"io"
"net/http"
"log"
"github.com/alexedwards/scs/v2"
"github.com/alexedwards/scs/redisstore"
"github.com/gomodule/redigo/redis"
)
var sessionManager *scs.SessionManager
func main() {
// Establish a redigo connection pool.
pool := &redis.Pool{
MaxIdle: 10,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", "localhost:6379")
},
}
// Initialize a new session manager and configure it to use redisstore as
// the session store.
sessionManager = scs.New()
sessionManager.Store = redisstore.New(pool)
mux := http.NewServeMux()
mux.HandleFunc("/put", putHandler)
mux.HandleFunc("/get", getHandler)
log.Println("starting server...")
http.ListenAndServe(":4000", sessionManager.LoadAndSave(mux))
}
func putHandler(w http.ResponseWriter, r *http.Request) {
sessionManager.Put(r.Context(), "message", "Hello from a session!")
}
func getHandler(w http.ResponseWriter, r *http.Request) {
msg := sessionManager.GetString(r.Context(), "message")
io.WriteString(w, msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment