Last active
May 23, 2020 04:33
-
-
Save alexedwards/29df406a09f9a9e8af98fec959707733 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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