-
Notifications
You must be signed in to change notification settings - Fork 4
/
page_session.go
84 lines (75 loc) · 2.03 KB
/
page_session.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
package main
import (
"fmt"
"net"
"net/http"
"github.com/gorilla/sessions"
"github.com/pborman/uuid"
)
// This is basically a convenience struct for
// easier session management (hopefully ;)
type pageSession struct {
session *sessions.Session
req *http.Request
w http.ResponseWriter
}
func (p *pageSession) getStringValue(key string) (string, error) {
val := p.session.Values[key]
var retVal string
var ok bool
if retVal, ok = val.(string); !ok {
return "", fmt.Errorf("Unable to create string from %s", key)
}
return retVal, nil
}
func (p *pageSession) setStringValue(key, val string) {
p.session.Values[key] = val
p.session.Save(p.req, p.w)
}
func (p *pageSession) getClientId() string {
var clientId string
var err error
if clientId, err = p.getStringValue("client_id"); err != nil {
// No client id in session, check if we have one for this ip in the db
fmt.Println("Looking for Client Data")
clientIp, _, _ := net.SplitHostPort(p.req.RemoteAddr)
var cli *Client
fmt.Println(" Client IP:" + clientIp)
if clientIp != "127.0.0.1" {
fmt.Println(" Pulling data by IP")
if cli, err = m.GetClientByIp(clientIp); err != nil {
cli = NewClient(clientId)
}
}
if cli != nil {
clientId = cli.UUID
p.setStringValue("client_id", clientId)
} else {
// No client id, generate and save one
fmt.Println(" Generating a new Client ID")
clientId = uuid.New()
p.setStringValue("client_id", clientId)
}
}
return clientId
}
func (p *pageSession) setFlashMessage(msg, status string) {
p.setStringValue("flash_message", msg)
p.setStringValue("flash_status", status)
}
func (p *pageSession) getFlashMessage() (string, string) {
var err error
var msg, status string
if msg, err = p.getStringValue("flash_message"); err != nil {
return "", "hidden"
}
if status, err = p.getStringValue("flash_status"); err != nil {
return "", "hidden"
}
p.setFlashMessage("", "hidden")
return msg, status
}
func (p *pageSession) expireSession() {
p.session.Options.MaxAge = -1
p.session.Save(p.req, p.w)
}