forked from gin-gonic/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
39 lines (31 loc) · 754 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func CookieTool() gin.HandlerFunc {
return func(c *gin.Context) {
// Get cookie
if cookie, err := c.Cookie("label"); err == nil {
if cookie == "ok" {
c.Next()
return
}
}
// Cookie verification failed
c.JSON(http.StatusForbidden, gin.H{"error": "Forbidden with no cookie"})
c.Abort()
}
}
func main() {
route := gin.Default()
route.GET("/login", func(c *gin.Context) {
// Set cookie {"label": "ok" }, maxAge 30 seconds.
c.SetCookie("label", "ok", 30, "/", "localhost", false, true)
c.String(200, "Login success!")
})
route.GET("/home", CookieTool(), func(c *gin.Context) {
c.JSON(200, gin.H{"data": "Your home page"})
})
route.Run(":8080")
}