-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.go
More file actions
81 lines (65 loc) · 1.88 KB
/
session.go
File metadata and controls
81 lines (65 loc) · 1.88 KB
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
// Copyright 2016 HeadwindFly. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/headwindfly/sessions"
"github.com/luisjakon/lightning"
"log"
"math/rand"
"time"
)
var router *lightning.Router
func getSession(ctx *lightning.Context) {
// Get session.
ctx.GetSession()
defer ctx.SaveSession()
if number, ok := ctx.Session.Values["randomNumber"]; ok {
fmt.Fprint(ctx, fmt.Sprintf("The random number is: %d.\n", number))
return
}
fmt.Fprint(ctx, "No random number.\n")
fmt.Fprintf(ctx, "If it does not work, make sure that your redis-server is started.")
}
func setSession(ctx *lightning.Context) {
// Get session.
ctx.GetSession()
defer ctx.SaveSession()
// Set random number.
randomNumber := rand.Intn(100)
ctx.Session.Values["randomNumber"] = randomNumber
fmt.Fprint(ctx, fmt.Sprintf("The random number has been set as: %d.\n", randomNumber))
}
func main() {
// Create a router instance.
router = lightning.NewRouter()
// Create a redis pool.
redisPool := &redis.Pool{
MaxIdle: 100,
IdleTimeout: time.Duration(100) * time.Second,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", ":6379")
},
}
defer redisPool.Close()
// Create a redis session store.
store := sessions.NewRedisStore(redisPool, sessions.Options{
MaxAge: 3600 * 24 * 7, // 10 seconds.
// Domain:".lightning.dev",
// HttpOnly:true,
// Secure:true,
})
// Set session store.
router.SetSessionStore(store)
// Register route handler.
router.GET("/", lightning.HandlerFunc(getSession))
router.GET("/random", lightning.HandlerFunc(setSession))
// Start server.
log.Fatal(lightning.ListenAndServe(":8080", router.Handler))
}