1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 20:30:49 +00:00
Beego/session
2013-11-26 17:14:27 +08:00
..
README.md update docs 2013-11-26 17:14:27 +08:00
sess_file.go fix #248 2013-11-05 22:23:48 +08:00
sess_gob.go session 2013-04-05 23:50:53 +08:00
sess_mem.go fix #248 2013-11-05 22:23:48 +08:00
sess_mysql.go session & orm register called twice for driver mysql 2013-11-26 17:12:25 +08:00
sess_redis.go change third repo from other to beego 2013-11-21 22:19:19 +08:00
sess_test.go add session test & delete readme 2013-05-08 22:31:36 +08:00
session.go fix #248 2013-11-05 22:23:48 +08:00

session

session is a Go session manager. It can use many session providers. Just like the database/sql and database/sql/driver.

How to install?

go get github.com/astaxie/beego/session

What providers are supported?

As of now this session manager support memory, file, Redis and MySQL.

How to use it?

First you must import it

import (
	"github.com/astaxie/beego/session"
)

Then in you web app init the global session manager

var globalSessions *session.Manager
  • Use memory as provider:

      func init() {
      	globalSessions, _ = session.NewManager("memory", "gosessionid", 3600,"")
      	go globalSessions.GC()
      }
    
  • Use file as provider, the last param is the path where you want file to be stored:

      func init() {
      	globalSessions, _ = session.NewManager("file", "gosessionid", 3600, "./tmp")
      	go globalSessions.GC()
      }
    
  • Use Redis as provider, the last param is the Redis conn address,poolsize,password:

      func init() {
      	globalSessions, _ = session.NewManager("redis", "gosessionid", 3600, "127.0.0.1:6379,100,astaxie")
      	go globalSessions.GC()
      }
    
  • Use MySQL as provider, the last param is the DSN, learn more from mysql,but first you should import mysql:

      import _ "github.com/go-sql-driver/mysql"
    
      func init() {
      	globalSessions, _ = session.NewManager(
      		"mysql", "gosessionid", 3600, "username:password@protocol(address)/dbname?param=value")
      	go globalSessions.GC()
      }
    

Finally in the handlerfunc you can use it like this

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"])
	}
}

How to write own provider?

When you develop a web app, maybe you want to write own provider because you must meet the requirements.

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.

type SessionStore interface {
	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
	SessionRelease()                  // release the resource
}

type Provider interface {
	SessionInit(maxlifetime int64, savePath string) error
	SessionRead(sid string) (SessionStore, error)
	SessionDestroy(sid string) error
	SessionGC()
}

LICENSE

BSD License http://creativecommons.org/licenses/BSD/