mysql - Golang sessions with MysqlStore not working -
i having issues setting sessions on 1 request , getting them on page request.
i using mysqlstore package recommended on gorilla sessions github page under "store implementations" section in there readme file.
https://github.com/srinathgs/mysqlstore
i setup simple request test out , wasn't working. here code:
homecontroller
func (this *homecontroller) setcookie(ctx types.appcontext) web.handlertype { return func(c web.c, w http.responsewriter, r *http.request) { w.header().set("content-type", "text/plain") fmt.printf("%v\n\n", r) ctx.setcookies(r, w) } } func (this *homecontroller) getcookie(ctx types.appcontext) web.handlertype { return func(c web.c, w http.responsewriter, r *http.request) { w.header().set("content-type", "text/plain") fmt.printf("%v\n\n", r) ctx.getcookies(r) } }
types.appcontext
func (this *appcontext) setcookies(r *http.request, w http.responsewriter) { fmt.println("setting cookies") session, _ := this.store.get(r, "session-name") // set session values. session.values["foo"] = "bar" session.values[42] = 43 // save before write response/return handler. err := session.save(r, w) if err != nil { fmt.println("problem saving session data") } } func (this *appcontext) getcookies(r *http.request) { session, _ := this.store.get(r, "session-name") fmt.println("getting cookies") // session values k, v := range session.values { fmt.println("key:", k) fmt.println("value:", v) } }
main.go
ctx.setstore("sessions", "/", 3600, []byte("somesecret")) .... goji.get("/set-cookie", homectrl.setcookie(ctx)) goji.get("/get-cookie", homectrl.getcookie(ctx))
when visit both pages no errors , can see in database session saved. however, when try retrieve saved cookie, session.values map empty.
when print out request following (formatted easy viewing):
&{ /get-cookie http/1.1 1 1 map[ connection:[keep-alive] user-agent:[mozilla/5.0 (macintosh; intel mac os x 10_9_5) applewebkit/537.36 (khtml, gecko) chrome/44.0.2403.107 safari/537.36] cookie:[session-name=mtq0mdq4mju0m3xcuxdbqwpjm3x3xersttxboojergohzp2pvemqikise1xjs76zi365pa==] accept-language:[en,en-ca;q=0.8,en-us;q=0.6,ja;q=0.4] cache-control:[max-age=0] accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8] upgrade-insecure-requests:[1] accept-encoding:[gzip, deflate, sdch] ] 0x66a9a0 0 [] false localhost:8000 map[] map[] <nil> map[] [::1]:62375 /get-cookie <nil> }
as can see sending cookie , "session-name" one. in chrome when @ cookies can see locally well.
name: session-name value: mtq0mdq4mju0m3xcuxdbqwpjm3x3xersttxboojergohzp2pvemqikise1xjs76zi365pa domain: localhost path: / expires/max-age: 2015-08-25t07:02:23.347z (it 10:50pm - 11:00pm when tested out)
i have been looking @ , can't figure out why doesn't work. simple can make , according examples should work doesn't. there missing in code make work?
so figured out digging around. when logging errors within mysqlstore package found little error led me solution:
panic: sql: scan error on column index 2: unsupported driver -> scan pair: []uint8 -> *time.time
i googled around bit , found discussion on issue here:
https://github.com/go-sql-driver/mysql/issues/9
basically didn't read examples well. in packages readme file whole time. forgot add query string ?parsetime=true&loc=local
@ end of mysql connection credentials.
so solution when setting database:
db, err := sql.open("mysql", "username:password@/dbname?parsetime=true&loc=local")
Comments
Post a Comment