modulepackage
3.0.1+incompatible
Repository: https://github.com/go-session/cookie.git
Documentation: pkg.go.dev
# README
Cookie store for Session
Quick Start
Download and install
$ go get -u -v github.com/go-session/cookie
Create file server.go
package main
import (
"context"
"fmt"
"net/http"
"github.com/go-session/cookie"
"github.com/go-session/session"
)
var (
hashKey = []byte("FF51A553-72FC-478B-9AEF-93D6F506DE91")
)
func main() {
session.InitManager(
session.SetStore(
cookie.NewCookieStore(
cookie.SetCookieName("demo_cookie_store_id"),
cookie.SetHashKey(hashKey),
),
),
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
store, err := session.Start(context.Background(), w, r)
if err != nil {
fmt.Fprint(w, err)
return
}
store.Set("foo", "bar")
err = store.Save()
if err != nil {
fmt.Fprint(w, err)
return
}
http.Redirect(w, r, "/foo", 302)
})
http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
store, err := session.Start(context.Background(), w, r)
if err != nil {
fmt.Fprint(w, err)
return
}
foo, ok := store.Get("foo")
if !ok {
fmt.Fprint(w, "does not exist")
return
}
fmt.Fprintf(w, "foo:%s", foo)
})
http.ListenAndServe(":8080", nil)
}
Build and run
$ go build server.go
$ ./server
Open in your web browser
foo:bar
MIT License
Copyright (c) 2018 Lyric
# Functions
NewCookieStore Create an instance of a cookie store.
SetBlockFunc sets the encryption function used to create a cipher.Block.
SetBlockKey used to encrypt values.
SetCookieName Set the cookie name.
SetHashFunc sets the hash function used to create HMAC.
SetHashKey used to authenticate values using HMAC.
SetMaxAge restricts the maximum age, in seconds, for the cookie value.
SetMaxLength restricts the maximum length, in bytes, for the cookie value.
SetMinAge restricts the minimum age, in seconds, for the cookie value.
SetSecure Set cookie security.
# Type aliases
Option A cookie parameter options.