-3

How to use redis based-session on Revel?

I found this gist, but didn't know how to use it..

EDIT #1:

my env variable

GOROOT --> /usr/lib/go
GOPATH --> /home/asd/Dropbox/go

what i've done:

mkdir -p $GOPATH/src/myapp/app/libs/session
curl https://gist.githubusercontent.com/xthexder/9026678/raw/9e40fb56d8991de945a2064b6869bb7280b1305a/session.go \
 > $GOPATH/src/myapp/app/libs/session/session.go
go get github.com/garyburd/redigo
go get github.com/robfig/revel

add import "myapp/app/libs/session" on init.go, and this error show up:

Go Compilation Error
The Go code Dropbox/go/src/myapp/app/libs/session/session.go does not compile: undefined: Redis

In Dropbox/go/src/myapp/app/libs/session/session.go (around line 75)
70:         panic("Session values may not have null bytes")
71:     }
72:     sessionValue += "\x00" + key + ":" + value + "\x00"
73: }
74:
75: redisConn := Redis.Get()
76: defer redisConn.Close()
77:
78: params := []interface{}{"session:" + sessionToken, sessionValue}
79: if expireAfterDuration != 0 {    

the error on the console:

WARN  2014/06/11 20:11:15 build.go:132: Cannot determine git repository version: exit status 128
ERROR 2014/06/11 20:11:15 build.go:84: # myapp/app/libs/session
Dropbox/go/src/myapp/app/libs/session/session.go:75: undefined: Redis
Dropbox/go/src/myapp/app/libs/session/session.go:126: undefined: Redis
Dropbox/go/src/myapp/app/libs/session/session.go:129: not enough arguments in call to redis.String
Dropbox/go/src/myapp/app/libs/session/session.go:172: undefined: Redis
Dropbox/go/src/myapp/app/libs/session/session.go:175: not enough arguments in call to redis.Int
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • What have you tried thus far? The Gist is fairly comprehensive: it has an init() function, save and restore (get) session functions that go into your HTTP handlers, a revoke session helper and a middleware. – elithrar Jun 11 '14 at 08:19
  • first of all, where should I put that file (session.go)? – Kokizzu Jun 11 '14 at 12:47
  • 1
    Ultimately it doesn't matter, but to keep it simple: put it in your main package alongside everything else. Go doesn't see files so much as packages though. I suggest reading Effective Go (Google it) and the Revel docs in full, or using a simpler set of tools to learn with (net/http, Gorilla or Goji). Redis based sessions are great but it sounds like you're getting ahead of yourself (encrypted cookies over HTTPS are still good; just don't store sensitive stuff in them). – elithrar Jun 11 '14 at 13:05
  • 1
    You should sit down and read http://tip.golang.org/doc/effective_go.html. – OneOfOne Jun 11 '14 at 14:05
  • yes this is my third day studying Go, 2 days ago I read `Go Programming Language Phrasebook` and `Programming in Go`, yesterday I read Revel tutorial and docs, thanks anyway.. – Kokizzu Jun 12 '14 at 01:48

2 Answers2

1

It looks like the Gist is out of date. The error is right there in your question.
Redis is not defined.

First of all the package name should be lower case, an immediate red flag. Looking at github you can seen the package is (now) lower case as it should be.

deft_code
  • 57,255
  • 29
  • 141
  • 224
0

Nevermind, I solve it myself, modify the session.go code as shown on http://www.diffchecker.com/n0k0fy8w

change init.go from

revel.SessionFilter,           // Restore and write the session cookie.

into

app.SessionFilter,

to check it on the controller/action:

x, _ := strconv.ParseInt(c.Session["test"], 0, 64)
x += 1
c.Session["test"] = fmt.Sprintf("%d", x)
return c.Render(x)

and on the template

{{.x}}

visit the page, and verify it using redis-cli

$ redis-cli get `redis-cli --scan 'session:*'`
"\x00test:15\x00"
Kokizzu
  • 24,974
  • 37
  • 137
  • 233