ictgj-voting/page_session.go

85 lines
2.0 KiB
Go
Raw Normal View History

2017-04-03 21:32:13 +00:00
package main
import (
"fmt"
2017-07-28 19:36:14 +00:00
"net"
2017-04-03 21:32:13 +00:00
"net/http"
"github.com/gorilla/sessions"
2017-06-08 17:20:43 +00:00
"github.com/pborman/uuid"
2017-04-03 21:32:13 +00:00
)
// 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)
}
2017-06-30 19:35:36 +00:00
func (p *pageSession) getClientId() string {
2017-06-08 17:20:43 +00:00
var clientId string
var err error
if clientId, err = p.getStringValue("client_id"); err != nil {
2017-07-28 19:36:14 +00:00
// 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")
2017-10-21 22:47:01 +00:00
if cli, err = m.GetClientByIp(clientIp); err != nil {
cli = NewClient(clientId)
}
2017-07-28 19:36:14 +00:00
}
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)
}
2017-06-08 17:20:43 +00:00
}
return clientId
}
2017-04-03 21:32:13 +00:00
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)
}