1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-28 08:35:47 +00:00
Beego/session/README.md

115 lines
3.5 KiB
Markdown
Raw Normal View History

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() {
2014-01-05 06:48:36 +00:00
globalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid","gclifetime":3600}`)
2013-06-20 12:22:06 +00:00
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",`{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"./tmp"}`)
2013-06-20 12:22:06 +00:00
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() {
globalSessions, _ = session.NewManager("redis", `{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"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", `{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"username:password@protocol(address)/dbname?param=value"}`)
2013-06-20 12:25:39 +00:00
go globalSessions.GC()
}
2013-04-08 05:02:45 +00:00
2014-01-05 06:48:36 +00:00
* Use **Cookie** as provider:
func init() {
globalSessions, _ = session.NewManager(
"cookie", `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`)
2014-01-05 06:48:36 +00:00
go globalSessions.GC()
}
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)
2014-01-05 06:48:36 +00:00
defer sess.SessionRelease(w)
2013-04-08 05:02:45 +00:00
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.
2014-01-05 06:48:36 +00:00
Maybe you will find the **memory** provider is a good example.
2013-04-08 05:02:45 +00:00
type SessionStore interface {
2014-01-05 06:48:36 +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(w http.ResponseWriter) // release the resource & save data to provider & return the data
Flush() error //delete all data
2013-04-08 05:02:45 +00:00
}
type Provider interface {
2014-01-05 06:48:36 +00:00
SessionInit(gclifetime int64, config string) error
2013-04-08 05:02:45 +00:00
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/