Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set int value into session (using redis db), but can not get it out! #1020

Closed
jaggerwang opened this issue Jun 2, 2018 · 2 comments
Closed

Comments

@jaggerwang
Copy link

jaggerwang commented Jun 2, 2018

Problem

Very strange problem! I can set string value into session (using redis db) and can get it out, but not working with int value.

Set("id", "10005") => GetString("id") OK
Set("id", "10005") => GetInt("id") OK
Set("id", 10005) => GetString("id") Fail
Set("id", 10005) => GetInt("id") Fail

func newSession(db sessions.Database) *sessions.Sessions {
	sess := sessions.New(sessions.Config{
		Cookie:  viper.GetString("session.cookieName"),
		Expires: time.Duration(viper.GetInt64("session.expireHours")) * time.Hour,
	})

	sess.UseDatabase(db)

	return sess
}

func newSessionDb() *redis.Database {
	return redis.New(service.Config{
		Addr:     viper.GetString("session.db.addr"),
		Password: viper.GetString("session.db.password"),
		Database: viper.GetString("session.db.database"),
	})
}

func RunApp() {
	app := iris.New()
	app.Use(recover.New())
	app.Use(logger.New())

	sessdb := newSessionDb()
	defer sessdb.Close()
	iris.RegisterOnInterrupt(func() {
		sessdb.Close()
	})
	sess := newSession(sessdb)

	mvc.New(app.Party("/")).Handle(controller.NewIndex())
	mvc.New(app.Party("/oauth/")).Register(sess.Start).Handle(controller.NewOAuth())
	mvc.New(app.Party("/account/")).Register(sess.Start).Handle(controller.NewAccount())

	fmt.Println("load views from", viper.GetString("web.viewDir"))
	app.RegisterView(iris.HTML(viper.GetString("web.viewDir"), ".gohtml"))

	fmt.Println("web service listen on", viper.GetString("web.listenAddr"))
	app.Run(iris.Addr(viper.GetString("web.listenAddr")))
}

func (c *Account) GetLogin(ctx iris.Context) {
	c.Session.Set("currentUserID", "10005")
}

func (c *Account) GetInfo(ctx iris.Context) {
	id, err := c.Session.GetInt("currentUserID")
	...
}

Env

Go: go version go1.10.2 darwin/amd64
Iris: 10.6.5:https://github.com/kataras/iris/blob/master/HISTORY.md#mo-21-may-2018--v1065

@kataras
Copy link
Owner

kataras commented Jun 2, 2018

I I know where the problem is, you'll have a fix for that in 24hours from now :) Thank you @jaggerwang for the bug report.

@kataras
Copy link
Owner

kataras commented Jun 2, 2018

Fixed, although GetFloat64 would work because redis stores the go's int as float64, it was an easy fix, on the GetInt we added a check for float64 as well (alongside with int64 and string we already have there).

Please complete the user experience form whenever you feel free to: https://goo.gl/forms/lnRbVgA6ICTkPyk02 and star the iris project if you love the fast fixes:)

Upgrade with: go get -u github.com/kataras/iris, automatic updater will not work because I'm waiting some days more for pushing the new release. Thank you @jaggerwang , have a nice day!

@kataras kataras closed this as completed in 3d30cce Jun 2, 2018
github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
…hat type assertion on GetInt as well

Former-commit-id: d29abdfe3a39fa1e046acbc5d118421a153d9c04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants