2013-06-20 12:22:06 +00:00
|
|
|
session
|
2013-04-08 05:02:45 +00:00
|
|
|
==============
|
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
session is a Go session manager. It can use many session providers. Just like the `database/sql` and `database/sql/driver`.
|
2013-04-08 05:02:45 +00:00
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
## How to install?
|
2013-04-08 05:02:45 +00:00
|
|
|
|
|
|
|
go get github.com/astaxie/beego/session
|
|
|
|
|
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
## What providers are supported?
|
2013-04-08 05:02:45 +00:00
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
As of now this session manager support memory, file, Redis and MySQL.
|
2013-04-08 05:02:45 +00:00
|
|
|
|
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
## How to use it?
|
2013-04-08 05:02:45 +00:00
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
First you must import it
|
2013-04-08 05:02:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/astaxie/beego/session"
|
|
|
|
)
|
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
Then in you web app init the global session manager
|
2013-04-08 05:02:45 +00:00
|
|
|
|
|
|
|
var globalSessions *session.Manager
|
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
* Use **memory** as provider:
|
2013-04-08 05:02:45 +00:00
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
func init() {
|
|
|
|
globalSessions, _ = session.NewManager("memory", "gosessionid", 3600,"")
|
|
|
|
go globalSessions.GC()
|
|
|
|
}
|
2013-04-08 05:02:45 +00:00
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
* Use **file** as provider, the last param is the path where you want file to be stored:
|
2013-04-08 05:02:45 +00:00
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
func init() {
|
|
|
|
globalSessions, _ = session.NewManager("file", "gosessionid", 3600, "./tmp")
|
|
|
|
go globalSessions.GC()
|
|
|
|
}
|
2013-04-08 05:02:45 +00:00
|
|
|
|
2013-10-28 14:19:37 +00:00
|
|
|
* Use **Redis** as provider, the last param is the Redis conn address,poolsize,password:
|
2013-04-08 05:02:45 +00:00
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
func init() {
|
2013-10-28 14:19:37 +00:00
|
|
|
globalSessions, _ = session.NewManager("redis", "gosessionid", 3600, "127.0.0.1:6379,100,astaxie")
|
2013-06-20 12:22:06 +00:00
|
|
|
go globalSessions.GC()
|
|
|
|
}
|
2013-06-20 12:25:39 +00:00
|
|
|
|
2013-11-26 09:16:35 +00:00
|
|
|
* Use **MySQL** as provider, the last param is the DSN, learn more from [mysql](https://github.com/go-sql-driver/mysql#dsn-data-source-name):
|
2013-06-20 12:25:39 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
globalSessions, _ = session.NewManager(
|
|
|
|
"mysql", "gosessionid", 3600, "username:password@protocol(address)/dbname?param=value")
|
|
|
|
go globalSessions.GC()
|
|
|
|
}
|
2013-04-08 05:02:45 +00:00
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
Finally in the handlerfunc you can use it like this
|
2013-04-08 05:02:45 +00:00
|
|
|
|
|
|
|
func login(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sess := globalSessions.SessionStart(w, r)
|
|
|
|
defer sess.SessionRelease()
|
|
|
|
username := sess.Get("username")
|
|
|
|
fmt.Println(username)
|
|
|
|
if r.Method == "GET" {
|
|
|
|
t, _ := template.ParseFiles("login.gtpl")
|
|
|
|
t.Execute(w, nil)
|
|
|
|
} else {
|
|
|
|
fmt.Println("username:", r.Form["username"])
|
|
|
|
sess.Set("username", r.Form["username"])
|
|
|
|
fmt.Println("password:", r.Form["password"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
## How to write own provider?
|
|
|
|
|
|
|
|
When you develop a web app, maybe you want to write own provider because you must meet the requirements.
|
2013-04-08 05:02:45 +00:00
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
Writing a provider is easy. You only need to define two struct types
|
|
|
|
(Session and Provider), which satisfy the interface definition.
|
|
|
|
Maybe you will find the **memory** provider as good example.
|
2013-04-08 05:02:45 +00:00
|
|
|
|
|
|
|
type SessionStore interface {
|
2013-12-07 05:26:22 +00:00
|
|
|
Set(key, value interface{}) error //set session value
|
|
|
|
Get(key interface{}) interface{} //get session value
|
|
|
|
Delete(key interface{}) error //delete session value
|
|
|
|
SessionID() string //back current sessionID
|
|
|
|
SessionRelease() // release the resource & save data to provider
|
|
|
|
Flush() error //delete all data
|
2013-04-08 05:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provider interface {
|
|
|
|
SessionInit(maxlifetime int64, savePath string) error
|
|
|
|
SessionRead(sid string) (SessionStore, error)
|
2013-12-07 05:26:22 +00:00
|
|
|
SessionExist(sid string) bool
|
|
|
|
SessionRegenerate(oldsid, sid string) (SessionStore, error)
|
2013-04-08 05:02:45 +00:00
|
|
|
SessionDestroy(sid string) error
|
2013-12-07 05:26:22 +00:00
|
|
|
SessionAll() int //get all active session
|
2013-04-08 05:02:45 +00:00
|
|
|
SessionGC()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 12:22:06 +00:00
|
|
|
## LICENSE
|
|
|
|
|
|
|
|
BSD License http://creativecommons.org/licenses/BSD/
|