Cookie/AddCookie
Cookie方法从HTTP请求头中获取cookie,AddCookie则添加cookie至HTTP响应头。
Example
package main
import (
"math/rand"
"net/http"
"strconv"
"github.com/vicanso/elton"
"github.com/vicanso/elton/middleware"
)
func main() {
e := elton.New()
e.Use(middleware.NewDefaultResponder())
e.GET("/", func(c *elton.Context) (err error) {
cookie, _ := c.Cookie("jt")
if cookie == nil {
_ = c.AddCookie(&http.Cookie{
Name: "jt",
Value: strconv.Itoa(rand.Int()),
})
}
c.Body = cookie
return
})
err := e.ListenAndServe(":3000")
if err != nil {
panic(err)
}
}