1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-16 22:40:54 +00:00
Beego/session/README.md

102 lines
2.8 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() {
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-06-20 12:22:06 +00:00
* Use **Redis** as provider, the last param is the Redis conn address:
2013-04-08 05:02:45 +00:00
2013-06-20 12:22:06 +00:00
func init() {
globalSessions, _ = session.NewManager("redis", "gosessionid", 3600, "127.0.0.1:6379")
go globalSessions.GC()
}
2013-06-20 12:25:39 +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):
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-06-20 12:22:06 +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 // return current sessionID
2013-04-08 05:02:45 +00:00
SessionRelease() // release the resource
}
type Provider interface {
SessionInit(maxlifetime int64, savePath string) error
SessionRead(sid string) (SessionStore, error)
SessionDestroy(sid string) error
SessionGC()
}
2013-06-20 12:22:06 +00:00
## LICENSE
BSD License http://creativecommons.org/licenses/BSD/