-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
111 lines (96 loc) · 2.82 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"flag"
"log"
"time"
"github.com/fasthttp/router"
"github.com/fasthttp/session/v2"
"github.com/fasthttp/session/v2/providers/memcache"
"github.com/fasthttp/session/v2/providers/memory"
"github.com/fasthttp/session/v2/providers/mongodb"
"github.com/fasthttp/session/v2/providers/mysql"
"github.com/fasthttp/session/v2/providers/postgre"
"github.com/fasthttp/session/v2/providers/redis"
"github.com/fasthttp/session/v2/providers/sqlite3"
"github.com/valyala/fasthttp"
)
const defaultProvider = "memory"
var serverSession *session.Session
func init() {
providerName := flag.String("provider", defaultProvider, "Name of provider")
flag.Parse()
encoder := session.Base64Encode
decoder := session.Base64Decode
var provider session.Provider
var err error
switch *providerName {
case "memory":
encoder = session.MSGPEncode
decoder = session.MSGPDecode
provider, err = memory.New(memory.Config{})
case "redis":
encoder = session.MSGPEncode
decoder = session.MSGPDecode
provider, err = redis.New(redis.Config{
KeyPrefix: "session",
Addr: "127.0.0.1:6379",
PoolSize: 8,
IdleTimeout: 30 * time.Second,
})
case "memcache":
encoder = session.MSGPEncode
decoder = session.MSGPDecode
provider, err = memcache.New(memcache.Config{
KeyPrefix: "session",
ServerList: []string{
"0.0.0.0:11211",
},
MaxIdleConns: 8,
})
case "mysql":
cfg := mysql.NewConfigWith("127.0.0.1", 3306, "root", "session", "test", "session")
provider, err = mysql.New(cfg)
case "postgre":
cfg := postgre.NewConfigWith("127.0.0.1", 5432, "postgres", "session", "test", "session")
provider, err = postgre.New(cfg)
case "sqlite3":
cfg := sqlite3.NewConfigWith("test.db", "session")
provider, err = sqlite3.New(cfg)
case "mongodb":
cfg := mongodb.NewConfigWith("mongodb://root:password@localhost:27017", "test", "session")
provider, err = mongodb.New(cfg)
default:
panic("Invalid provider")
}
if err != nil {
log.Fatal(err)
}
cfg := session.NewDefaultConfig()
cfg.EncodeFunc = encoder
cfg.DecodeFunc = decoder
serverSession = session.New(cfg)
if err = serverSession.SetProvider(provider); err != nil {
log.Fatal(err)
}
log.Print("Starting example with provider: " + *providerName)
}
func main() {
r := router.New()
r.GET("/", indexHandler)
r.GET("/set", setHandler)
r.GET("/get", getHandler)
r.GET("/delete", deleteHandler)
r.GET("/getAll", getAllHandler)
r.GET("/flush", flushHandler)
r.GET("/destroy", destroyHandler)
r.GET("/sessionid", sessionIDHandler)
r.GET("/regenerate", regenerateHandler)
r.GET("/setexpiration", setExpirationHandler)
r.GET("/getexpiration", getExpirationHandler)
addr := "0.0.0.0:8086"
log.Println("Session example server listen: http://" + addr)
err := fasthttp.ListenAndServe(addr, r.Handler)
if err != nil {
log.Fatal(err)
}
}