...
1 package main
2
3 import (
4 "database/sql"
5 "github.com/gin-contrib/sessions"
6 "github.com/gin-contrib/sessions/postgres"
7 "github.com/gin-gonic/gin"
8 )
9
10 func main() {
11 r := gin.Default()
12 db, err := sql.Open("postgres", "postgresql://username:password@localhost:5432/database")
13 if err != nil {
14
15 }
16
17 store, err := postgres.NewStore(db, []byte("secret"))
18 if err != nil {
19
20 }
21
22 r.Use(sessions.Sessions("mysession", store))
23
24 r.GET("/incr", func(c *gin.Context) {
25 session := sessions.Default(c)
26 var count int
27 v := session.Get("count")
28 if v == nil {
29 count = 0
30 } else {
31 count = v.(int)
32 count++
33 }
34 session.Set("count", count)
35 session.Save()
36 c.JSON(200, gin.H{"count": count})
37 })
38 r.Run(":8000")
39 }
40
View as plain text