-- import "vimagination.zapto.org/sessions"
Package sessions is used to store session information for a web server.
- Set and get session data in client cookies.
- Session data is signed and dated to prevent tampering.
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"time"
"vimagination.zapto.org/sessions"
)
func main() {
store, err := sessions.NewCookieStore([]byte("!THIS IS MY KEY!"), sessions.Expiry(time.Second))
if err != nil {
fmt.Println(err)
return
}
w := httptest.NewRecorder()
store.Set(w, []byte("MY_DATA"))
r := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
for _, cookie := range w.Result().Cookies() {
r.AddCookie(cookie)
}
fmt.Printf("%q\n", store.Get(r))
time.Sleep(2 * time.Second)
fmt.Printf("%q\n", store.Get(r))
// Output:
// "MY_DATA"
// ""
}Full API docs can be found at: