mirror of
https://github.com/astaxie/beego.git
synced 2024-12-22 21:30:49 +00:00
golint session
This commit is contained in:
parent
ea2039c1dc
commit
172894efe8
@ -39,7 +39,7 @@ var (
|
||||
// BeegoInput operates the http request header, data, cookie and body.
|
||||
// it also contains router params and current session.
|
||||
type BeegoInput struct {
|
||||
CruSession session.SessionStore
|
||||
CruSession session.Store
|
||||
Params map[string]string
|
||||
Data map[interface{}]interface{} // store some values in this context when calling context in filter or controller.
|
||||
Request *http.Request
|
||||
|
@ -67,7 +67,7 @@ type Controller struct {
|
||||
TplExt string
|
||||
_xsrfToken string
|
||||
gotofunc string
|
||||
CruSession session.SessionStore
|
||||
CruSession session.Store
|
||||
XSRFExpire int
|
||||
AppController interface{}
|
||||
EnableRender bool
|
||||
@ -559,7 +559,7 @@ func (c *Controller) SaveToFile(fromfile, tofile string) error {
|
||||
}
|
||||
|
||||
// StartSession starts session and load old session data info this controller.
|
||||
func (c *Controller) StartSession() session.SessionStore {
|
||||
func (c *Controller) StartSession() session.Store {
|
||||
if c.CruSession == nil {
|
||||
c.CruSession = c.Ctx.Input.CruSession
|
||||
}
|
||||
@ -596,7 +596,7 @@ func (c *Controller) SessionRegenerateID() {
|
||||
if c.CruSession != nil {
|
||||
c.CruSession.SessionRelease(c.Ctx.ResponseWriter)
|
||||
}
|
||||
c.CruSession = GlobalSessions.SessionRegenerateId(c.Ctx.ResponseWriter, c.Ctx.Request)
|
||||
c.CruSession = GlobalSessions.SessionRegenerateID(c.Ctx.ResponseWriter, c.Ctx.Request)
|
||||
c.Ctx.Input.CruSession = c.CruSession
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// package couchbase for session provider
|
||||
// Package couchbase for session provider
|
||||
//
|
||||
// depend on github.com/couchbaselabs/go-couchbasee
|
||||
//
|
||||
@ -30,21 +30,22 @@
|
||||
// }
|
||||
//
|
||||
// more docs: http://beego.me/docs/module/session.md
|
||||
package session
|
||||
package couchbase
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/couchbaselabs/go-couchbase"
|
||||
"gh.apple.com/md/go-couchbase"
|
||||
|
||||
"github.com/astaxie/beego/session"
|
||||
)
|
||||
|
||||
var couchbpder = &CouchbaseProvider{}
|
||||
var couchbpder = &Provider{}
|
||||
|
||||
type CouchbaseSessionStore struct {
|
||||
// SessionStore store each session
|
||||
type SessionStore struct {
|
||||
b *couchbase.Bucket
|
||||
sid string
|
||||
lock sync.RWMutex
|
||||
@ -52,7 +53,8 @@ type CouchbaseSessionStore struct {
|
||||
maxlifetime int64
|
||||
}
|
||||
|
||||
type CouchbaseProvider struct {
|
||||
// Provider couchabse provided
|
||||
type Provider struct {
|
||||
maxlifetime int64
|
||||
savePath string
|
||||
pool string
|
||||
@ -60,42 +62,47 @@ type CouchbaseProvider struct {
|
||||
b *couchbase.Bucket
|
||||
}
|
||||
|
||||
func (cs *CouchbaseSessionStore) Set(key, value interface{}) error {
|
||||
// Set value to couchabse session
|
||||
func (cs *SessionStore) Set(key, value interface{}) error {
|
||||
cs.lock.Lock()
|
||||
defer cs.lock.Unlock()
|
||||
cs.values[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *CouchbaseSessionStore) Get(key interface{}) interface{} {
|
||||
// Get value from couchabse session
|
||||
func (cs *SessionStore) Get(key interface{}) interface{} {
|
||||
cs.lock.RLock()
|
||||
defer cs.lock.RUnlock()
|
||||
if v, ok := cs.values[key]; ok {
|
||||
return v
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *CouchbaseSessionStore) Delete(key interface{}) error {
|
||||
// Delete value in couchbase session by given key
|
||||
func (cs *SessionStore) Delete(key interface{}) error {
|
||||
cs.lock.Lock()
|
||||
defer cs.lock.Unlock()
|
||||
delete(cs.values, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *CouchbaseSessionStore) Flush() error {
|
||||
// Flush Clean all values in couchbase session
|
||||
func (cs *SessionStore) Flush() error {
|
||||
cs.lock.Lock()
|
||||
defer cs.lock.Unlock()
|
||||
cs.values = make(map[interface{}]interface{})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *CouchbaseSessionStore) SessionID() string {
|
||||
// SessionID Get couchbase session store id
|
||||
func (cs *SessionStore) SessionID() string {
|
||||
return cs.sid
|
||||
}
|
||||
|
||||
func (cs *CouchbaseSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
// SessionRelease Write couchbase session with Gob string
|
||||
func (cs *SessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
defer cs.b.Close()
|
||||
|
||||
bo, err := session.EncodeGob(cs.values)
|
||||
@ -106,7 +113,7 @@ func (cs *CouchbaseSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
cs.b.Set(cs.sid, int(cs.maxlifetime), bo)
|
||||
}
|
||||
|
||||
func (cp *CouchbaseProvider) getBucket() *couchbase.Bucket {
|
||||
func (cp *Provider) getBucket() *couchbase.Bucket {
|
||||
c, err := couchbase.Connect(cp.savePath)
|
||||
if err != nil {
|
||||
return nil
|
||||
@ -125,10 +132,10 @@ func (cp *CouchbaseProvider) getBucket() *couchbase.Bucket {
|
||||
return bucket
|
||||
}
|
||||
|
||||
// init couchbase session
|
||||
// SessionInit init couchbase session
|
||||
// savepath like couchbase server REST/JSON URL
|
||||
// e.g. http://host:port/, Pool, Bucket
|
||||
func (cp *CouchbaseProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
func (cp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
cp.maxlifetime = maxlifetime
|
||||
configs := strings.Split(savePath, ",")
|
||||
if len(configs) > 0 {
|
||||
@ -144,8 +151,8 @@ func (cp *CouchbaseProvider) SessionInit(maxlifetime int64, savePath string) err
|
||||
return nil
|
||||
}
|
||||
|
||||
// read couchbase session by sid
|
||||
func (cp *CouchbaseProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
// SessionRead read couchbase session by sid
|
||||
func (cp *Provider) SessionRead(sid string) (session.Store, error) {
|
||||
cp.b = cp.getBucket()
|
||||
|
||||
var doc []byte
|
||||
@ -161,11 +168,13 @@ func (cp *CouchbaseProvider) SessionRead(sid string) (session.SessionStore, erro
|
||||
}
|
||||
}
|
||||
|
||||
cs := &CouchbaseSessionStore{b: cp.b, sid: sid, values: kv, maxlifetime: cp.maxlifetime}
|
||||
cs := &SessionStore{b: cp.b, sid: sid, values: kv, maxlifetime: cp.maxlifetime}
|
||||
return cs, nil
|
||||
}
|
||||
|
||||
func (cp *CouchbaseProvider) SessionExist(sid string) bool {
|
||||
// SessionExist Check couchbase session exist.
|
||||
// it checkes sid exist or not.
|
||||
func (cp *Provider) SessionExist(sid string) bool {
|
||||
cp.b = cp.getBucket()
|
||||
defer cp.b.Close()
|
||||
|
||||
@ -173,12 +182,12 @@ func (cp *CouchbaseProvider) SessionExist(sid string) bool {
|
||||
|
||||
if err := cp.b.Get(sid, &doc); err != nil || doc == nil {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (cp *CouchbaseProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
|
||||
// SessionRegenerate remove oldsid and use sid to generate new session
|
||||
func (cp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
||||
cp.b = cp.getBucket()
|
||||
|
||||
var doc []byte
|
||||
@ -206,11 +215,12 @@ func (cp *CouchbaseProvider) SessionRegenerate(oldsid, sid string) (session.Sess
|
||||
}
|
||||
}
|
||||
|
||||
cs := &CouchbaseSessionStore{b: cp.b, sid: sid, values: kv, maxlifetime: cp.maxlifetime}
|
||||
cs := &SessionStore{b: cp.b, sid: sid, values: kv, maxlifetime: cp.maxlifetime}
|
||||
return cs, nil
|
||||
}
|
||||
|
||||
func (cp *CouchbaseProvider) SessionDestroy(sid string) error {
|
||||
// SessionDestroy Remove bucket in this couchbase
|
||||
func (cp *Provider) SessionDestroy(sid string) error {
|
||||
cp.b = cp.getBucket()
|
||||
defer cp.b.Close()
|
||||
|
||||
@ -218,11 +228,13 @@ func (cp *CouchbaseProvider) SessionDestroy(sid string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cp *CouchbaseProvider) SessionGC() {
|
||||
// SessionGC Recycle
|
||||
func (cp *Provider) SessionGC() {
|
||||
return
|
||||
}
|
||||
|
||||
func (cp *CouchbaseProvider) SessionAll() int {
|
||||
// SessionAll return all active session
|
||||
func (cp *Provider) SessionAll() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
package session
|
||||
// Package ledis provide session Provider
|
||||
package ledis
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@ -11,59 +12,58 @@ import (
|
||||
"github.com/siddontang/ledisdb/ledis"
|
||||
)
|
||||
|
||||
var ledispder = &LedisProvider{}
|
||||
var ledispder = &Provider{}
|
||||
var c *ledis.DB
|
||||
|
||||
// ledis session store
|
||||
type LedisSessionStore struct {
|
||||
// SessionStore ledis session store
|
||||
type SessionStore struct {
|
||||
sid string
|
||||
lock sync.RWMutex
|
||||
values map[interface{}]interface{}
|
||||
maxlifetime int64
|
||||
}
|
||||
|
||||
// set value in ledis session
|
||||
func (ls *LedisSessionStore) Set(key, value interface{}) error {
|
||||
// Set value in ledis session
|
||||
func (ls *SessionStore) Set(key, value interface{}) error {
|
||||
ls.lock.Lock()
|
||||
defer ls.lock.Unlock()
|
||||
ls.values[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
// get value in ledis session
|
||||
func (ls *LedisSessionStore) Get(key interface{}) interface{} {
|
||||
// Get value in ledis session
|
||||
func (ls *SessionStore) Get(key interface{}) interface{} {
|
||||
ls.lock.RLock()
|
||||
defer ls.lock.RUnlock()
|
||||
if v, ok := ls.values[key]; ok {
|
||||
return v
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// delete value in ledis session
|
||||
func (ls *LedisSessionStore) Delete(key interface{}) error {
|
||||
// Delete value in ledis session
|
||||
func (ls *SessionStore) Delete(key interface{}) error {
|
||||
ls.lock.Lock()
|
||||
defer ls.lock.Unlock()
|
||||
delete(ls.values, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// clear all values in ledis session
|
||||
func (ls *LedisSessionStore) Flush() error {
|
||||
// Flush clear all values in ledis session
|
||||
func (ls *SessionStore) Flush() error {
|
||||
ls.lock.Lock()
|
||||
defer ls.lock.Unlock()
|
||||
ls.values = make(map[interface{}]interface{})
|
||||
return nil
|
||||
}
|
||||
|
||||
// get ledis session id
|
||||
func (ls *LedisSessionStore) SessionID() string {
|
||||
// SessionID get ledis session id
|
||||
func (ls *SessionStore) SessionID() string {
|
||||
return ls.sid
|
||||
}
|
||||
|
||||
// save session values to ledis
|
||||
func (ls *LedisSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
// SessionRelease save session values to ledis
|
||||
func (ls *SessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
b, err := session.EncodeGob(ls.values)
|
||||
if err != nil {
|
||||
return
|
||||
@ -72,17 +72,17 @@ func (ls *LedisSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
c.Expire([]byte(ls.sid), ls.maxlifetime)
|
||||
}
|
||||
|
||||
// ledis session provider
|
||||
type LedisProvider struct {
|
||||
// Provider ledis session provider
|
||||
type Provider struct {
|
||||
maxlifetime int64
|
||||
savePath string
|
||||
db int
|
||||
}
|
||||
|
||||
// init ledis session
|
||||
// SessionInit init ledis session
|
||||
// savepath like ledis server saveDataPath,pool size
|
||||
// e.g. 127.0.0.1:6379,100,astaxie
|
||||
func (lp *LedisProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
func (lp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
var err error
|
||||
lp.maxlifetime = maxlifetime
|
||||
configs := strings.Split(savePath, ",")
|
||||
@ -106,8 +106,8 @@ func (lp *LedisProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// read ledis session by sid
|
||||
func (lp *LedisProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
// SessionRead read ledis session by sid
|
||||
func (lp *Provider) SessionRead(sid string) (session.Store, error) {
|
||||
kvs, err := c.Get([]byte(sid))
|
||||
var kv map[interface{}]interface{}
|
||||
if len(kvs) == 0 {
|
||||
@ -118,22 +118,21 @@ func (lp *LedisProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
ls := &LedisSessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime}
|
||||
ls := &SessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime}
|
||||
return ls, nil
|
||||
}
|
||||
|
||||
// check ledis session exist by sid
|
||||
func (lp *LedisProvider) SessionExist(sid string) bool {
|
||||
// SessionExist check ledis session exist by sid
|
||||
func (lp *Provider) SessionExist(sid string) bool {
|
||||
count, _ := c.Exists([]byte(sid))
|
||||
if count == 0 {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// generate new sid for ledis session
|
||||
func (lp *LedisProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
|
||||
// SessionRegenerate generate new sid for ledis session
|
||||
func (lp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
||||
count, _ := c.Exists([]byte(sid))
|
||||
if count == 0 {
|
||||
// oldsid doesn't exists, set the new sid directly
|
||||
@ -156,23 +155,23 @@ func (lp *LedisProvider) SessionRegenerate(oldsid, sid string) (session.SessionS
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
ls := &LedisSessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime}
|
||||
ls := &SessionStore{sid: sid, values: kv, maxlifetime: lp.maxlifetime}
|
||||
return ls, nil
|
||||
}
|
||||
|
||||
// delete ledis session by id
|
||||
func (lp *LedisProvider) SessionDestroy(sid string) error {
|
||||
// SessionDestroy delete ledis session by id
|
||||
func (lp *Provider) SessionDestroy(sid string) error {
|
||||
c.Del([]byte(sid))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Impelment method, no used.
|
||||
func (lp *LedisProvider) SessionGC() {
|
||||
// SessionGC Impelment method, no used.
|
||||
func (lp *Provider) SessionGC() {
|
||||
return
|
||||
}
|
||||
|
||||
// @todo
|
||||
func (lp *LedisProvider) SessionAll() int {
|
||||
// SessionAll return all active session
|
||||
func (lp *Provider) SessionAll() int {
|
||||
return 0
|
||||
}
|
||||
func init() {
|
||||
|
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// package memcache for session provider
|
||||
// Package memcache for session provider
|
||||
//
|
||||
// depend on github.com/bradfitz/gomemcache/memcache
|
||||
//
|
||||
@ -30,7 +30,7 @@
|
||||
// }
|
||||
//
|
||||
// more docs: http://beego.me/docs/module/session.md
|
||||
package session
|
||||
package memcache
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@ -45,56 +45,55 @@ import (
|
||||
var mempder = &MemProvider{}
|
||||
var client *memcache.Client
|
||||
|
||||
// memcache session store
|
||||
type MemcacheSessionStore struct {
|
||||
// SessionStore memcache session store
|
||||
type SessionStore struct {
|
||||
sid string
|
||||
lock sync.RWMutex
|
||||
values map[interface{}]interface{}
|
||||
maxlifetime int64
|
||||
}
|
||||
|
||||
// set value in memcache session
|
||||
func (rs *MemcacheSessionStore) Set(key, value interface{}) error {
|
||||
// Set value in memcache session
|
||||
func (rs *SessionStore) Set(key, value interface{}) error {
|
||||
rs.lock.Lock()
|
||||
defer rs.lock.Unlock()
|
||||
rs.values[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
// get value in memcache session
|
||||
func (rs *MemcacheSessionStore) Get(key interface{}) interface{} {
|
||||
// Get value in memcache session
|
||||
func (rs *SessionStore) Get(key interface{}) interface{} {
|
||||
rs.lock.RLock()
|
||||
defer rs.lock.RUnlock()
|
||||
if v, ok := rs.values[key]; ok {
|
||||
return v
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// delete value in memcache session
|
||||
func (rs *MemcacheSessionStore) Delete(key interface{}) error {
|
||||
// Delete value in memcache session
|
||||
func (rs *SessionStore) Delete(key interface{}) error {
|
||||
rs.lock.Lock()
|
||||
defer rs.lock.Unlock()
|
||||
delete(rs.values, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// clear all values in memcache session
|
||||
func (rs *MemcacheSessionStore) Flush() error {
|
||||
// Flush clear all values in memcache session
|
||||
func (rs *SessionStore) Flush() error {
|
||||
rs.lock.Lock()
|
||||
defer rs.lock.Unlock()
|
||||
rs.values = make(map[interface{}]interface{})
|
||||
return nil
|
||||
}
|
||||
|
||||
// get memcache session id
|
||||
func (rs *MemcacheSessionStore) SessionID() string {
|
||||
// SessionID get memcache session id
|
||||
func (rs *SessionStore) SessionID() string {
|
||||
return rs.sid
|
||||
}
|
||||
|
||||
// save session values to memcache
|
||||
func (rs *MemcacheSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
// SessionRelease save session values to memcache
|
||||
func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
b, err := session.EncodeGob(rs.values)
|
||||
if err != nil {
|
||||
return
|
||||
@ -103,7 +102,7 @@ func (rs *MemcacheSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
client.Set(&item)
|
||||
}
|
||||
|
||||
// memcahe session provider
|
||||
// MemProvider memcache session provider
|
||||
type MemProvider struct {
|
||||
maxlifetime int64
|
||||
conninfo []string
|
||||
@ -111,7 +110,7 @@ type MemProvider struct {
|
||||
password string
|
||||
}
|
||||
|
||||
// init memcache session
|
||||
// SessionInit init memcache session
|
||||
// savepath like
|
||||
// e.g. 127.0.0.1:9090
|
||||
func (rp *MemProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
@ -121,8 +120,8 @@ func (rp *MemProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// read memcache session by sid
|
||||
func (rp *MemProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
// SessionRead read memcache session by sid
|
||||
func (rp *MemProvider) SessionRead(sid string) (session.Store, error) {
|
||||
if client == nil {
|
||||
if err := rp.connectInit(); err != nil {
|
||||
return nil, err
|
||||
@ -130,7 +129,7 @@ func (rp *MemProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
}
|
||||
item, err := client.Get(sid)
|
||||
if err != nil && err == memcache.ErrCacheMiss {
|
||||
rs := &MemcacheSessionStore{sid: sid, values: make(map[interface{}]interface{}), maxlifetime: rp.maxlifetime}
|
||||
rs := &SessionStore{sid: sid, values: make(map[interface{}]interface{}), maxlifetime: rp.maxlifetime}
|
||||
return rs, nil
|
||||
}
|
||||
var kv map[interface{}]interface{}
|
||||
@ -142,11 +141,11 @@ func (rp *MemProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rs := &MemcacheSessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
||||
rs := &SessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
// check memcache session exist by sid
|
||||
// SessionExist check memcache session exist by sid
|
||||
func (rp *MemProvider) SessionExist(sid string) bool {
|
||||
if client == nil {
|
||||
if err := rp.connectInit(); err != nil {
|
||||
@ -155,13 +154,12 @@ func (rp *MemProvider) SessionExist(sid string) bool {
|
||||
}
|
||||
if item, err := client.Get(sid); err != nil || len(item.Value) == 0 {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// generate new sid for memcache session
|
||||
func (rp *MemProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
|
||||
// SessionRegenerate generate new sid for memcache session
|
||||
func (rp *MemProvider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
||||
if client == nil {
|
||||
if err := rp.connectInit(); err != nil {
|
||||
return nil, err
|
||||
@ -195,11 +193,11 @@ func (rp *MemProvider) SessionRegenerate(oldsid, sid string) (session.SessionSto
|
||||
}
|
||||
}
|
||||
|
||||
rs := &MemcacheSessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
||||
rs := &SessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
// delete memcache session by id
|
||||
// SessionDestroy delete memcache session by id
|
||||
func (rp *MemProvider) SessionDestroy(sid string) error {
|
||||
if client == nil {
|
||||
if err := rp.connectInit(); err != nil {
|
||||
@ -219,12 +217,12 @@ func (rp *MemProvider) connectInit() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Impelment method, no used.
|
||||
// SessionGC Impelment method, no used.
|
||||
func (rp *MemProvider) SessionGC() {
|
||||
return
|
||||
}
|
||||
|
||||
// @todo
|
||||
// SessionAll return all activeSession
|
||||
func (rp *MemProvider) SessionAll() int {
|
||||
return 0
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// package mysql for session provider
|
||||
// Package mysql for session provider
|
||||
//
|
||||
// depends on github.com/go-sql-driver/mysql:
|
||||
//
|
||||
@ -38,7 +38,7 @@
|
||||
// }
|
||||
//
|
||||
// more docs: http://beego.me/docs/module/session.md
|
||||
package session
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
@ -47,67 +47,67 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/session"
|
||||
|
||||
// import mysql driver
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
var (
|
||||
// TableName store the session in MySQL
|
||||
TableName = "session"
|
||||
mysqlpder = &MysqlProvider{}
|
||||
mysqlpder = &Provider{}
|
||||
)
|
||||
|
||||
// mysql session store
|
||||
type MysqlSessionStore struct {
|
||||
// SessionStore mysql session store
|
||||
type SessionStore struct {
|
||||
c *sql.DB
|
||||
sid string
|
||||
lock sync.RWMutex
|
||||
values map[interface{}]interface{}
|
||||
}
|
||||
|
||||
// set value in mysql session.
|
||||
// Set value in mysql session.
|
||||
// it is temp value in map.
|
||||
func (st *MysqlSessionStore) Set(key, value interface{}) error {
|
||||
func (st *SessionStore) Set(key, value interface{}) error {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
st.values[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
// get value from mysql session
|
||||
func (st *MysqlSessionStore) Get(key interface{}) interface{} {
|
||||
// Get value from mysql session
|
||||
func (st *SessionStore) Get(key interface{}) interface{} {
|
||||
st.lock.RLock()
|
||||
defer st.lock.RUnlock()
|
||||
if v, ok := st.values[key]; ok {
|
||||
return v
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// delete value in mysql session
|
||||
func (st *MysqlSessionStore) Delete(key interface{}) error {
|
||||
// Delete value in mysql session
|
||||
func (st *SessionStore) Delete(key interface{}) error {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
delete(st.values, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// clear all values in mysql session
|
||||
func (st *MysqlSessionStore) Flush() error {
|
||||
// Flush clear all values in mysql session
|
||||
func (st *SessionStore) Flush() error {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
st.values = make(map[interface{}]interface{})
|
||||
return nil
|
||||
}
|
||||
|
||||
// get session id of this mysql session store
|
||||
func (st *MysqlSessionStore) SessionID() string {
|
||||
// SessionID get session id of this mysql session store
|
||||
func (st *SessionStore) SessionID() string {
|
||||
return st.sid
|
||||
}
|
||||
|
||||
// save mysql session values to database.
|
||||
// SessionRelease save mysql session values to database.
|
||||
// must call this method to save values to database.
|
||||
func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
func (st *SessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
defer st.c.Close()
|
||||
b, err := session.EncodeGob(st.values)
|
||||
if err != nil {
|
||||
@ -118,14 +118,14 @@ func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
|
||||
}
|
||||
|
||||
// mysql session provider
|
||||
type MysqlProvider struct {
|
||||
// Provider mysql session provider
|
||||
type Provider struct {
|
||||
maxlifetime int64
|
||||
savePath string
|
||||
}
|
||||
|
||||
// connect to mysql
|
||||
func (mp *MysqlProvider) connectInit() *sql.DB {
|
||||
func (mp *Provider) connectInit() *sql.DB {
|
||||
db, e := sql.Open("mysql", mp.savePath)
|
||||
if e != nil {
|
||||
return nil
|
||||
@ -133,16 +133,16 @@ func (mp *MysqlProvider) connectInit() *sql.DB {
|
||||
return db
|
||||
}
|
||||
|
||||
// init mysql session.
|
||||
// SessionInit init mysql session.
|
||||
// savepath is the connection string of mysql.
|
||||
func (mp *MysqlProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
func (mp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
mp.maxlifetime = maxlifetime
|
||||
mp.savePath = savePath
|
||||
return nil
|
||||
}
|
||||
|
||||
// get mysql session by sid
|
||||
func (mp *MysqlProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
// SessionRead get mysql session by sid
|
||||
func (mp *Provider) SessionRead(sid string) (session.Store, error) {
|
||||
c := mp.connectInit()
|
||||
row := c.QueryRow("select session_data from "+TableName+" where session_key=?", sid)
|
||||
var sessiondata []byte
|
||||
@ -160,12 +160,12 @@ func (mp *MysqlProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rs := &MysqlSessionStore{c: c, sid: sid, values: kv}
|
||||
rs := &SessionStore{c: c, sid: sid, values: kv}
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
// check mysql session exist
|
||||
func (mp *MysqlProvider) SessionExist(sid string) bool {
|
||||
// SessionExist check mysql session exist
|
||||
func (mp *Provider) SessionExist(sid string) bool {
|
||||
c := mp.connectInit()
|
||||
defer c.Close()
|
||||
row := c.QueryRow("select session_data from "+TableName+" where session_key=?", sid)
|
||||
@ -173,13 +173,12 @@ func (mp *MysqlProvider) SessionExist(sid string) bool {
|
||||
err := row.Scan(&sessiondata)
|
||||
if err == sql.ErrNoRows {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// generate new sid for mysql session
|
||||
func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
|
||||
// SessionRegenerate generate new sid for mysql session
|
||||
func (mp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
||||
c := mp.connectInit()
|
||||
row := c.QueryRow("select session_data from "+TableName+" where session_key=?", oldsid)
|
||||
var sessiondata []byte
|
||||
@ -197,28 +196,28 @@ func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (session.SessionS
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rs := &MysqlSessionStore{c: c, sid: sid, values: kv}
|
||||
rs := &SessionStore{c: c, sid: sid, values: kv}
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
// delete mysql session by sid
|
||||
func (mp *MysqlProvider) SessionDestroy(sid string) error {
|
||||
// SessionDestroy delete mysql session by sid
|
||||
func (mp *Provider) SessionDestroy(sid string) error {
|
||||
c := mp.connectInit()
|
||||
c.Exec("DELETE FROM "+TableName+" where session_key=?", sid)
|
||||
c.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
// delete expired values in mysql session
|
||||
func (mp *MysqlProvider) SessionGC() {
|
||||
// SessionGC delete expired values in mysql session
|
||||
func (mp *Provider) SessionGC() {
|
||||
c := mp.connectInit()
|
||||
c.Exec("DELETE from "+TableName+" where session_expiry < ?", time.Now().Unix()-mp.maxlifetime)
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// count values in mysql session
|
||||
func (mp *MysqlProvider) SessionAll() int {
|
||||
// SessionAll count values in mysql session
|
||||
func (mp *Provider) SessionAll() int {
|
||||
c := mp.connectInit()
|
||||
defer c.Close()
|
||||
var total int
|
||||
|
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// package postgresql for session provider
|
||||
// Package postgres for session provider
|
||||
//
|
||||
// depends on github.com/lib/pq:
|
||||
//
|
||||
@ -27,9 +27,9 @@
|
||||
// session_expiry timestamp NOT NULL,
|
||||
// CONSTRAINT session_key PRIMARY KEY(session_key)
|
||||
// );
|
||||
|
||||
//
|
||||
// will be activated with these settings in app.conf:
|
||||
|
||||
//
|
||||
// SessionOn = true
|
||||
// SessionProvider = postgresql
|
||||
// SessionSavePath = "user=a password=b dbname=c sslmode=disable"
|
||||
@ -48,7 +48,7 @@
|
||||
// }
|
||||
//
|
||||
// more docs: http://beego.me/docs/module/session.md
|
||||
package session
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
@ -57,64 +57,63 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/session"
|
||||
|
||||
// import postgresql Driver
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var postgresqlpder = &PostgresqlProvider{}
|
||||
var postgresqlpder = &Provider{}
|
||||
|
||||
// postgresql session store
|
||||
type PostgresqlSessionStore struct {
|
||||
// SessionStore postgresql session store
|
||||
type SessionStore struct {
|
||||
c *sql.DB
|
||||
sid string
|
||||
lock sync.RWMutex
|
||||
values map[interface{}]interface{}
|
||||
}
|
||||
|
||||
// set value in postgresql session.
|
||||
// Set value in postgresql session.
|
||||
// it is temp value in map.
|
||||
func (st *PostgresqlSessionStore) Set(key, value interface{}) error {
|
||||
func (st *SessionStore) Set(key, value interface{}) error {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
st.values[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
// get value from postgresql session
|
||||
func (st *PostgresqlSessionStore) Get(key interface{}) interface{} {
|
||||
// Get value from postgresql session
|
||||
func (st *SessionStore) Get(key interface{}) interface{} {
|
||||
st.lock.RLock()
|
||||
defer st.lock.RUnlock()
|
||||
if v, ok := st.values[key]; ok {
|
||||
return v
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// delete value in postgresql session
|
||||
func (st *PostgresqlSessionStore) Delete(key interface{}) error {
|
||||
// Delete value in postgresql session
|
||||
func (st *SessionStore) Delete(key interface{}) error {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
delete(st.values, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// clear all values in postgresql session
|
||||
func (st *PostgresqlSessionStore) Flush() error {
|
||||
// Flush clear all values in postgresql session
|
||||
func (st *SessionStore) Flush() error {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
st.values = make(map[interface{}]interface{})
|
||||
return nil
|
||||
}
|
||||
|
||||
// get session id of this postgresql session store
|
||||
func (st *PostgresqlSessionStore) SessionID() string {
|
||||
// SessionID get session id of this postgresql session store
|
||||
func (st *SessionStore) SessionID() string {
|
||||
return st.sid
|
||||
}
|
||||
|
||||
// save postgresql session values to database.
|
||||
// SessionRelease save postgresql session values to database.
|
||||
// must call this method to save values to database.
|
||||
func (st *PostgresqlSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
func (st *SessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
defer st.c.Close()
|
||||
b, err := session.EncodeGob(st.values)
|
||||
if err != nil {
|
||||
@ -125,14 +124,14 @@ func (st *PostgresqlSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
|
||||
}
|
||||
|
||||
// postgresql session provider
|
||||
type PostgresqlProvider struct {
|
||||
// Provider postgresql session provider
|
||||
type Provider struct {
|
||||
maxlifetime int64
|
||||
savePath string
|
||||
}
|
||||
|
||||
// connect to postgresql
|
||||
func (mp *PostgresqlProvider) connectInit() *sql.DB {
|
||||
func (mp *Provider) connectInit() *sql.DB {
|
||||
db, e := sql.Open("postgres", mp.savePath)
|
||||
if e != nil {
|
||||
return nil
|
||||
@ -140,16 +139,16 @@ func (mp *PostgresqlProvider) connectInit() *sql.DB {
|
||||
return db
|
||||
}
|
||||
|
||||
// init postgresql session.
|
||||
// SessionInit init postgresql session.
|
||||
// savepath is the connection string of postgresql.
|
||||
func (mp *PostgresqlProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
func (mp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
mp.maxlifetime = maxlifetime
|
||||
mp.savePath = savePath
|
||||
return nil
|
||||
}
|
||||
|
||||
// get postgresql session by sid
|
||||
func (mp *PostgresqlProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
// SessionRead get postgresql session by sid
|
||||
func (mp *Provider) SessionRead(sid string) (session.Store, error) {
|
||||
c := mp.connectInit()
|
||||
row := c.QueryRow("select session_data from session where session_key=$1", sid)
|
||||
var sessiondata []byte
|
||||
@ -174,12 +173,12 @@ func (mp *PostgresqlProvider) SessionRead(sid string) (session.SessionStore, err
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rs := &PostgresqlSessionStore{c: c, sid: sid, values: kv}
|
||||
rs := &SessionStore{c: c, sid: sid, values: kv}
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
// check postgresql session exist
|
||||
func (mp *PostgresqlProvider) SessionExist(sid string) bool {
|
||||
// SessionExist check postgresql session exist
|
||||
func (mp *Provider) SessionExist(sid string) bool {
|
||||
c := mp.connectInit()
|
||||
defer c.Close()
|
||||
row := c.QueryRow("select session_data from session where session_key=$1", sid)
|
||||
@ -188,13 +187,12 @@ func (mp *PostgresqlProvider) SessionExist(sid string) bool {
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// generate new sid for postgresql session
|
||||
func (mp *PostgresqlProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
|
||||
// SessionRegenerate generate new sid for postgresql session
|
||||
func (mp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
||||
c := mp.connectInit()
|
||||
row := c.QueryRow("select session_data from session where session_key=$1", oldsid)
|
||||
var sessiondata []byte
|
||||
@ -213,28 +211,28 @@ func (mp *PostgresqlProvider) SessionRegenerate(oldsid, sid string) (session.Ses
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rs := &PostgresqlSessionStore{c: c, sid: sid, values: kv}
|
||||
rs := &SessionStore{c: c, sid: sid, values: kv}
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
// delete postgresql session by sid
|
||||
func (mp *PostgresqlProvider) SessionDestroy(sid string) error {
|
||||
// SessionDestroy delete postgresql session by sid
|
||||
func (mp *Provider) SessionDestroy(sid string) error {
|
||||
c := mp.connectInit()
|
||||
c.Exec("DELETE FROM session where session_key=$1", sid)
|
||||
c.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
// delete expired values in postgresql session
|
||||
func (mp *PostgresqlProvider) SessionGC() {
|
||||
// SessionGC delete expired values in postgresql session
|
||||
func (mp *Provider) SessionGC() {
|
||||
c := mp.connectInit()
|
||||
c.Exec("DELETE from session where EXTRACT(EPOCH FROM (current_timestamp - session_expiry)) > $1", mp.maxlifetime)
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// count values in postgresql session
|
||||
func (mp *PostgresqlProvider) SessionAll() int {
|
||||
// SessionAll count values in postgresql session
|
||||
func (mp *Provider) SessionAll() int {
|
||||
c := mp.connectInit()
|
||||
defer c.Close()
|
||||
var total int
|
||||
|
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// package redis for session provider
|
||||
// Package redis for session provider
|
||||
//
|
||||
// depend on github.com/garyburd/redigo/redis
|
||||
//
|
||||
@ -30,7 +30,7 @@
|
||||
// }
|
||||
//
|
||||
// more docs: http://beego.me/docs/module/session.md
|
||||
package session
|
||||
package redis
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@ -43,15 +43,15 @@ import (
|
||||
"github.com/garyburd/redigo/redis"
|
||||
)
|
||||
|
||||
var redispder = &RedisProvider{}
|
||||
var redispder = &Provider{}
|
||||
|
||||
// redis max pool size
|
||||
var MAX_POOL_SIZE = 100
|
||||
var MaxPoolSize = 100
|
||||
|
||||
var redisPool chan redis.Conn
|
||||
|
||||
// redis session store
|
||||
type RedisSessionStore struct {
|
||||
// SessionStore redis session store
|
||||
type SessionStore struct {
|
||||
p *redis.Pool
|
||||
sid string
|
||||
lock sync.RWMutex
|
||||
@ -59,48 +59,47 @@ type RedisSessionStore struct {
|
||||
maxlifetime int64
|
||||
}
|
||||
|
||||
// set value in redis session
|
||||
func (rs *RedisSessionStore) Set(key, value interface{}) error {
|
||||
// Set value in redis session
|
||||
func (rs *SessionStore) Set(key, value interface{}) error {
|
||||
rs.lock.Lock()
|
||||
defer rs.lock.Unlock()
|
||||
rs.values[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
// get value in redis session
|
||||
func (rs *RedisSessionStore) Get(key interface{}) interface{} {
|
||||
// Get value in redis session
|
||||
func (rs *SessionStore) Get(key interface{}) interface{} {
|
||||
rs.lock.RLock()
|
||||
defer rs.lock.RUnlock()
|
||||
if v, ok := rs.values[key]; ok {
|
||||
return v
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// delete value in redis session
|
||||
func (rs *RedisSessionStore) Delete(key interface{}) error {
|
||||
// Delete value in redis session
|
||||
func (rs *SessionStore) Delete(key interface{}) error {
|
||||
rs.lock.Lock()
|
||||
defer rs.lock.Unlock()
|
||||
delete(rs.values, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// clear all values in redis session
|
||||
func (rs *RedisSessionStore) Flush() error {
|
||||
// Flush clear all values in redis session
|
||||
func (rs *SessionStore) Flush() error {
|
||||
rs.lock.Lock()
|
||||
defer rs.lock.Unlock()
|
||||
rs.values = make(map[interface{}]interface{})
|
||||
return nil
|
||||
}
|
||||
|
||||
// get redis session id
|
||||
func (rs *RedisSessionStore) SessionID() string {
|
||||
// SessionID get redis session id
|
||||
func (rs *SessionStore) SessionID() string {
|
||||
return rs.sid
|
||||
}
|
||||
|
||||
// save session values to redis
|
||||
func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
// SessionRelease save session values to redis
|
||||
func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
c := rs.p.Get()
|
||||
defer c.Close()
|
||||
|
||||
@ -112,8 +111,8 @@ func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
c.Do("SETEX", rs.sid, rs.maxlifetime, string(b))
|
||||
}
|
||||
|
||||
// redis session provider
|
||||
type RedisProvider struct {
|
||||
// Provider redis session provider
|
||||
type Provider struct {
|
||||
maxlifetime int64
|
||||
savePath string
|
||||
poolsize int
|
||||
@ -122,10 +121,10 @@ type RedisProvider struct {
|
||||
poollist *redis.Pool
|
||||
}
|
||||
|
||||
// init redis session
|
||||
// SessionInit init redis session
|
||||
// savepath like redis server addr,pool size,password,dbnum
|
||||
// e.g. 127.0.0.1:6379,100,astaxie,0
|
||||
func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
rp.maxlifetime = maxlifetime
|
||||
configs := strings.Split(savePath, ",")
|
||||
if len(configs) > 0 {
|
||||
@ -134,12 +133,12 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
if len(configs) > 1 {
|
||||
poolsize, err := strconv.Atoi(configs[1])
|
||||
if err != nil || poolsize <= 0 {
|
||||
rp.poolsize = MAX_POOL_SIZE
|
||||
rp.poolsize = MaxPoolSize
|
||||
} else {
|
||||
rp.poolsize = poolsize
|
||||
}
|
||||
} else {
|
||||
rp.poolsize = MAX_POOL_SIZE
|
||||
rp.poolsize = MaxPoolSize
|
||||
}
|
||||
if len(configs) > 2 {
|
||||
rp.password = configs[2]
|
||||
@ -176,8 +175,8 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
return rp.poollist.Get().Err()
|
||||
}
|
||||
|
||||
// read redis session by sid
|
||||
func (rp *RedisProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
// SessionRead read redis session by sid
|
||||
func (rp *Provider) SessionRead(sid string) (session.Store, error) {
|
||||
c := rp.poollist.Get()
|
||||
defer c.Close()
|
||||
|
||||
@ -192,24 +191,23 @@ func (rp *RedisProvider) SessionRead(sid string) (session.SessionStore, error) {
|
||||
}
|
||||
}
|
||||
|
||||
rs := &RedisSessionStore{p: rp.poollist, sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
||||
rs := &SessionStore{p: rp.poollist, sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
// check redis session exist by sid
|
||||
func (rp *RedisProvider) SessionExist(sid string) bool {
|
||||
// SessionExist check redis session exist by sid
|
||||
func (rp *Provider) SessionExist(sid string) bool {
|
||||
c := rp.poollist.Get()
|
||||
defer c.Close()
|
||||
|
||||
if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// generate new sid for redis session
|
||||
func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
|
||||
// SessionRegenerate generate new sid for redis session
|
||||
func (rp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
||||
c := rp.poollist.Get()
|
||||
defer c.Close()
|
||||
|
||||
@ -234,12 +232,12 @@ func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (session.SessionS
|
||||
}
|
||||
}
|
||||
|
||||
rs := &RedisSessionStore{p: rp.poollist, sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
||||
rs := &SessionStore{p: rp.poollist, sid: sid, values: kv, maxlifetime: rp.maxlifetime}
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
// delete redis session by id
|
||||
func (rp *RedisProvider) SessionDestroy(sid string) error {
|
||||
// SessionDestroy delete redis session by id
|
||||
func (rp *Provider) SessionDestroy(sid string) error {
|
||||
c := rp.poollist.Get()
|
||||
defer c.Close()
|
||||
|
||||
@ -247,13 +245,13 @@ func (rp *RedisProvider) SessionDestroy(sid string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Impelment method, no used.
|
||||
func (rp *RedisProvider) SessionGC() {
|
||||
// SessionGC Impelment method, no used.
|
||||
func (rp *Provider) SessionGC() {
|
||||
return
|
||||
}
|
||||
|
||||
// @todo
|
||||
func (rp *RedisProvider) SessionAll() int {
|
||||
// SessionAll return all activeSession
|
||||
func (rp *Provider) SessionAll() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,7 @@ import (
|
||||
|
||||
var cookiepder = &CookieProvider{}
|
||||
|
||||
// Cookie SessionStore
|
||||
// CookieSessionStore Cookie SessionStore
|
||||
type CookieSessionStore struct {
|
||||
sid string
|
||||
values map[interface{}]interface{} // session data
|
||||
@ -47,9 +47,8 @@ func (st *CookieSessionStore) Get(key interface{}) interface{} {
|
||||
defer st.lock.RUnlock()
|
||||
if v, ok := st.values[key]; ok {
|
||||
return v
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete value in cookie session
|
||||
@ -60,7 +59,7 @@ func (st *CookieSessionStore) Delete(key interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Clean all values in cookie session
|
||||
// Flush Clean all values in cookie session
|
||||
func (st *CookieSessionStore) Flush() error {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
@ -68,12 +67,12 @@ func (st *CookieSessionStore) Flush() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Return id of this cookie session
|
||||
// SessionID Return id of this cookie session
|
||||
func (st *CookieSessionStore) SessionID() string {
|
||||
return st.sid
|
||||
}
|
||||
|
||||
// Write cookie session to http response cookie
|
||||
// SessionRelease Write cookie session to http response cookie
|
||||
func (st *CookieSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
str, err := encodeCookie(cookiepder.block,
|
||||
cookiepder.config.SecurityKey,
|
||||
@ -101,14 +100,14 @@ type cookieConfig struct {
|
||||
Maxage int `json:"maxage"`
|
||||
}
|
||||
|
||||
// Cookie session provider
|
||||
// CookieProvider Cookie session provider
|
||||
type CookieProvider struct {
|
||||
maxlifetime int64
|
||||
config *cookieConfig
|
||||
block cipher.Block
|
||||
}
|
||||
|
||||
// Init cookie session provider with max lifetime and config json.
|
||||
// SessionInit Init cookie session provider with max lifetime and config json.
|
||||
// maxlifetime is ignored.
|
||||
// json config:
|
||||
// securityKey - hash string
|
||||
@ -136,9 +135,9 @@ func (pder *CookieProvider) SessionInit(maxlifetime int64, config string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get SessionStore in cooke.
|
||||
// SessionRead Get SessionStore in cooke.
|
||||
// decode cooke string to map and put into SessionStore with sid.
|
||||
func (pder *CookieProvider) SessionRead(sid string) (SessionStore, error) {
|
||||
func (pder *CookieProvider) SessionRead(sid string) (Store, error) {
|
||||
maps, _ := decodeCookie(pder.block,
|
||||
pder.config.SecurityKey,
|
||||
pder.config.SecurityName,
|
||||
@ -150,32 +149,32 @@ func (pder *CookieProvider) SessionRead(sid string) (SessionStore, error) {
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
// Cookie session is always existed
|
||||
// SessionExist Cookie session is always existed
|
||||
func (pder *CookieProvider) SessionExist(sid string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Implement method, no used.
|
||||
func (pder *CookieProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
|
||||
// SessionRegenerate Implement method, no used.
|
||||
func (pder *CookieProvider) SessionRegenerate(oldsid, sid string) (Store, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Implement method, no used.
|
||||
// SessionDestroy Implement method, no used.
|
||||
func (pder *CookieProvider) SessionDestroy(sid string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Implement method, no used.
|
||||
// SessionGC Implement method, no used.
|
||||
func (pder *CookieProvider) SessionGC() {
|
||||
return
|
||||
}
|
||||
|
||||
// Implement method, return 0.
|
||||
// SessionAll Implement method, return 0.
|
||||
func (pder *CookieProvider) SessionAll() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Implement method, no used.
|
||||
// SessionUpdate Implement method, no used.
|
||||
func (pder *CookieProvider) SessionUpdate(sid string) error {
|
||||
return nil
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ var (
|
||||
gcmaxlifetime int64
|
||||
)
|
||||
|
||||
// File session store
|
||||
// FileSessionStore File session store
|
||||
type FileSessionStore struct {
|
||||
sid string
|
||||
lock sync.RWMutex
|
||||
@ -53,9 +53,8 @@ func (fs *FileSessionStore) Get(key interface{}) interface{} {
|
||||
defer fs.lock.RUnlock()
|
||||
if v, ok := fs.values[key]; ok {
|
||||
return v
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete value in file session by given key
|
||||
@ -66,7 +65,7 @@ func (fs *FileSessionStore) Delete(key interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Clean all values in file session
|
||||
// Flush Clean all values in file session
|
||||
func (fs *FileSessionStore) Flush() error {
|
||||
fs.lock.Lock()
|
||||
defer fs.lock.Unlock()
|
||||
@ -74,12 +73,12 @@ func (fs *FileSessionStore) Flush() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get file session store id
|
||||
// SessionID Get file session store id
|
||||
func (fs *FileSessionStore) SessionID() string {
|
||||
return fs.sid
|
||||
}
|
||||
|
||||
// Write file session to local file with Gob string
|
||||
// SessionRelease Write file session to local file with Gob string
|
||||
func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
b, err := EncodeGob(fs.values)
|
||||
if err != nil {
|
||||
@ -100,14 +99,14 @@ func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
f.Close()
|
||||
}
|
||||
|
||||
// File session provider
|
||||
// FileProvider File session provider
|
||||
type FileProvider struct {
|
||||
lock sync.RWMutex
|
||||
maxlifetime int64
|
||||
savePath string
|
||||
}
|
||||
|
||||
// Init file session provider.
|
||||
// SessionInit Init file session provider.
|
||||
// savePath sets the session files path.
|
||||
func (fp *FileProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
fp.maxlifetime = maxlifetime
|
||||
@ -115,10 +114,10 @@ func (fp *FileProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read file session by sid.
|
||||
// SessionRead Read file session by sid.
|
||||
// if file is not exist, create it.
|
||||
// the file path is generated from sid string.
|
||||
func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
|
||||
func (fp *FileProvider) SessionRead(sid string) (Store, error) {
|
||||
filepder.lock.Lock()
|
||||
defer filepder.lock.Unlock()
|
||||
|
||||
@ -154,7 +153,7 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
// Check file session exist.
|
||||
// SessionExist Check file session exist.
|
||||
// it checkes the file named from sid exist or not.
|
||||
func (fp *FileProvider) SessionExist(sid string) bool {
|
||||
filepder.lock.Lock()
|
||||
@ -163,12 +162,11 @@ func (fp *FileProvider) SessionExist(sid string) bool {
|
||||
_, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
||||
if err == nil {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Remove all files in this save path
|
||||
// SessionDestroy Remove all files in this save path
|
||||
func (fp *FileProvider) SessionDestroy(sid string) error {
|
||||
filepder.lock.Lock()
|
||||
defer filepder.lock.Unlock()
|
||||
@ -176,7 +174,7 @@ func (fp *FileProvider) SessionDestroy(sid string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Recycle files in save path
|
||||
// SessionGC Recycle files in save path
|
||||
func (fp *FileProvider) SessionGC() {
|
||||
filepder.lock.Lock()
|
||||
defer filepder.lock.Unlock()
|
||||
@ -185,7 +183,7 @@ func (fp *FileProvider) SessionGC() {
|
||||
filepath.Walk(fp.savePath, gcpath)
|
||||
}
|
||||
|
||||
// Get active file session number.
|
||||
// SessionAll Get active file session number.
|
||||
// it walks save path to count files.
|
||||
func (fp *FileProvider) SessionAll() int {
|
||||
a := &activeSession{}
|
||||
@ -199,9 +197,9 @@ func (fp *FileProvider) SessionAll() int {
|
||||
return a.total
|
||||
}
|
||||
|
||||
// Generate new sid for file session.
|
||||
// SessionRegenerate Generate new sid for file session.
|
||||
// it delete old file and create new file named from new sid.
|
||||
func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
|
||||
func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (Store, error) {
|
||||
filepder.lock.Lock()
|
||||
defer filepder.lock.Unlock()
|
||||
|
||||
@ -269,14 +267,14 @@ type activeSession struct {
|
||||
total int
|
||||
}
|
||||
|
||||
func (self *activeSession) visit(paths string, f os.FileInfo, err error) error {
|
||||
func (as *activeSession) visit(paths string, f os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if f.IsDir() {
|
||||
return nil
|
||||
}
|
||||
self.total = self.total + 1
|
||||
as.total = as.total + 1
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ import (
|
||||
|
||||
var mempder = &MemProvider{list: list.New(), sessions: make(map[string]*list.Element)}
|
||||
|
||||
// memory session store.
|
||||
// MemSessionStore memory session store.
|
||||
// it saved sessions in a map in memory.
|
||||
type MemSessionStore struct {
|
||||
sid string //session id
|
||||
@ -32,7 +32,7 @@ type MemSessionStore struct {
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
// set value to memory session
|
||||
// Set value to memory session
|
||||
func (st *MemSessionStore) Set(key, value interface{}) error {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
@ -40,18 +40,17 @@ func (st *MemSessionStore) Set(key, value interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// get value from memory session by key
|
||||
// Get value from memory session by key
|
||||
func (st *MemSessionStore) Get(key interface{}) interface{} {
|
||||
st.lock.RLock()
|
||||
defer st.lock.RUnlock()
|
||||
if v, ok := st.value[key]; ok {
|
||||
return v
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// delete in memory session by key
|
||||
// Delete in memory session by key
|
||||
func (st *MemSessionStore) Delete(key interface{}) error {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
@ -59,7 +58,7 @@ func (st *MemSessionStore) Delete(key interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// clear all values in memory session
|
||||
// Flush clear all values in memory session
|
||||
func (st *MemSessionStore) Flush() error {
|
||||
st.lock.Lock()
|
||||
defer st.lock.Unlock()
|
||||
@ -67,15 +66,16 @@ func (st *MemSessionStore) Flush() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// get this id of memory session store
|
||||
// SessionID get this id of memory session store
|
||||
func (st *MemSessionStore) SessionID() string {
|
||||
return st.sid
|
||||
}
|
||||
|
||||
// Implement method, no used.
|
||||
// SessionRelease Implement method, no used.
|
||||
func (st *MemSessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
}
|
||||
|
||||
// MemProvider Implement the provider interface
|
||||
type MemProvider struct {
|
||||
lock sync.RWMutex // locker
|
||||
sessions map[string]*list.Element // map in memory
|
||||
@ -84,44 +84,42 @@ type MemProvider struct {
|
||||
savePath string
|
||||
}
|
||||
|
||||
// init memory session
|
||||
// SessionInit init memory session
|
||||
func (pder *MemProvider) SessionInit(maxlifetime int64, savePath string) error {
|
||||
pder.maxlifetime = maxlifetime
|
||||
pder.savePath = savePath
|
||||
return nil
|
||||
}
|
||||
|
||||
// get memory session store by sid
|
||||
func (pder *MemProvider) SessionRead(sid string) (SessionStore, error) {
|
||||
// SessionRead get memory session store by sid
|
||||
func (pder *MemProvider) SessionRead(sid string) (Store, error) {
|
||||
pder.lock.RLock()
|
||||
if element, ok := pder.sessions[sid]; ok {
|
||||
go pder.SessionUpdate(sid)
|
||||
pder.lock.RUnlock()
|
||||
return element.Value.(*MemSessionStore), nil
|
||||
} else {
|
||||
pder.lock.RUnlock()
|
||||
pder.lock.Lock()
|
||||
newsess := &MemSessionStore{sid: sid, timeAccessed: time.Now(), value: make(map[interface{}]interface{})}
|
||||
element := pder.list.PushBack(newsess)
|
||||
pder.sessions[sid] = element
|
||||
pder.lock.Unlock()
|
||||
return newsess, nil
|
||||
}
|
||||
pder.lock.RUnlock()
|
||||
pder.lock.Lock()
|
||||
newsess := &MemSessionStore{sid: sid, timeAccessed: time.Now(), value: make(map[interface{}]interface{})}
|
||||
element := pder.list.PushBack(newsess)
|
||||
pder.sessions[sid] = element
|
||||
pder.lock.Unlock()
|
||||
return newsess, nil
|
||||
}
|
||||
|
||||
// check session store exist in memory session by sid
|
||||
// SessionExist check session store exist in memory session by sid
|
||||
func (pder *MemProvider) SessionExist(sid string) bool {
|
||||
pder.lock.RLock()
|
||||
defer pder.lock.RUnlock()
|
||||
if _, ok := pder.sessions[sid]; ok {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// generate new sid for session store in memory session
|
||||
func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) {
|
||||
// SessionRegenerate generate new sid for session store in memory session
|
||||
func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (Store, error) {
|
||||
pder.lock.RLock()
|
||||
if element, ok := pder.sessions[oldsid]; ok {
|
||||
go pder.SessionUpdate(oldsid)
|
||||
@ -132,18 +130,17 @@ func (pder *MemProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er
|
||||
delete(pder.sessions, oldsid)
|
||||
pder.lock.Unlock()
|
||||
return element.Value.(*MemSessionStore), nil
|
||||
} else {
|
||||
pder.lock.RUnlock()
|
||||
pder.lock.Lock()
|
||||
newsess := &MemSessionStore{sid: sid, timeAccessed: time.Now(), value: make(map[interface{}]interface{})}
|
||||
element := pder.list.PushBack(newsess)
|
||||
pder.sessions[sid] = element
|
||||
pder.lock.Unlock()
|
||||
return newsess, nil
|
||||
}
|
||||
pder.lock.RUnlock()
|
||||
pder.lock.Lock()
|
||||
newsess := &MemSessionStore{sid: sid, timeAccessed: time.Now(), value: make(map[interface{}]interface{})}
|
||||
element := pder.list.PushBack(newsess)
|
||||
pder.sessions[sid] = element
|
||||
pder.lock.Unlock()
|
||||
return newsess, nil
|
||||
}
|
||||
|
||||
// delete session store in memory session by id
|
||||
// SessionDestroy delete session store in memory session by id
|
||||
func (pder *MemProvider) SessionDestroy(sid string) error {
|
||||
pder.lock.Lock()
|
||||
defer pder.lock.Unlock()
|
||||
@ -155,7 +152,7 @@ func (pder *MemProvider) SessionDestroy(sid string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// clean expired session stores in memory session
|
||||
// SessionGC clean expired session stores in memory session
|
||||
func (pder *MemProvider) SessionGC() {
|
||||
pder.lock.RLock()
|
||||
for {
|
||||
@ -177,12 +174,12 @@ func (pder *MemProvider) SessionGC() {
|
||||
pder.lock.RUnlock()
|
||||
}
|
||||
|
||||
// get count number of memory session
|
||||
// SessionAll get count number of memory session
|
||||
func (pder *MemProvider) SessionAll() int {
|
||||
return pder.list.Len()
|
||||
}
|
||||
|
||||
// expand time of session store by id in memory session
|
||||
// SessionUpdate expand time of session store by id in memory session
|
||||
func (pder *MemProvider) SessionUpdate(sid string) error {
|
||||
pder.lock.Lock()
|
||||
defer pder.lock.Unlock()
|
||||
|
@ -178,11 +178,11 @@ func decodeCookie(block cipher.Block, hashKey, name, value string, gcmaxlifetime
|
||||
return nil, err
|
||||
}
|
||||
// 5. DecodeGob.
|
||||
if dst, err := DecodeGob(b); err != nil {
|
||||
dst, err := DecodeGob(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return dst, nil
|
||||
}
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
// Encoding -------------------------------------------------------------------
|
||||
|
@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// package session provider
|
||||
// Package session provider
|
||||
//
|
||||
// Usage:
|
||||
// import(
|
||||
@ -37,8 +37,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// SessionStore contains all data for one session process with specific id.
|
||||
type SessionStore interface {
|
||||
// Store contains all data for one session process with specific id.
|
||||
type Store interface {
|
||||
Set(key, value interface{}) error //set session value
|
||||
Get(key interface{}) interface{} //get session value
|
||||
Delete(key interface{}) error //delete session value
|
||||
@ -51,9 +51,9 @@ type SessionStore interface {
|
||||
// it can operate a SessionStore by its id.
|
||||
type Provider interface {
|
||||
SessionInit(gclifetime int64, config string) error
|
||||
SessionRead(sid string) (SessionStore, error)
|
||||
SessionRead(sid string) (Store, error)
|
||||
SessionExist(sid string) bool
|
||||
SessionRegenerate(oldsid, sid string) (SessionStore, error)
|
||||
SessionRegenerate(oldsid, sid string) (Store, error)
|
||||
SessionDestroy(sid string) error
|
||||
SessionAll() int //get all active session
|
||||
SessionGC()
|
||||
@ -83,7 +83,7 @@ type managerConfig struct {
|
||||
CookieLifeTime int `json:"cookieLifeTime"`
|
||||
ProviderConfig string `json:"providerConfig"`
|
||||
Domain string `json:"domain"`
|
||||
SessionIdLength int64 `json:"sessionIdLength"`
|
||||
SessionIDLength int64 `json:"sessionIDLength"`
|
||||
}
|
||||
|
||||
// Manager contains Provider and its configuration.
|
||||
@ -92,7 +92,7 @@ type Manager struct {
|
||||
config *managerConfig
|
||||
}
|
||||
|
||||
// Create new Manager with provider name and json config string.
|
||||
// NewManager Create new Manager with provider name and json config string.
|
||||
// provider name:
|
||||
// 1. cookie
|
||||
// 2. file
|
||||
@ -123,8 +123,8 @@ func NewManager(provideName, config string) (*Manager, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cf.SessionIdLength == 0 {
|
||||
cf.SessionIdLength = 16
|
||||
if cf.SessionIDLength == 0 {
|
||||
cf.SessionIDLength = 16
|
||||
}
|
||||
|
||||
return &Manager{
|
||||
@ -133,12 +133,12 @@ func NewManager(provideName, config string) (*Manager, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Start session. generate or read the session id from http request.
|
||||
// SessionStart Start session. generate or read the session id from http request.
|
||||
// if session id exists, return SessionStore with this id.
|
||||
func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (session SessionStore, err error) {
|
||||
func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (session Store, err error) {
|
||||
cookie, errs := r.Cookie(manager.config.CookieName)
|
||||
if errs != nil || cookie.Value == "" {
|
||||
sid, errs := manager.sessionId(r)
|
||||
sid, errs := manager.sessionID(r)
|
||||
if errs != nil {
|
||||
return nil, errs
|
||||
}
|
||||
@ -167,7 +167,7 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
|
||||
if manager.provider.SessionExist(sid) {
|
||||
session, err = manager.provider.SessionRead(sid)
|
||||
} else {
|
||||
sid, err = manager.sessionId(r)
|
||||
sid, err = manager.sessionID(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -193,39 +193,38 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
|
||||
return
|
||||
}
|
||||
|
||||
// Destroy session by its id in http request cookie.
|
||||
// SessionDestroy Destroy session by its id in http request cookie.
|
||||
func (manager *Manager) SessionDestroy(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie(manager.config.CookieName)
|
||||
if err != nil || cookie.Value == "" {
|
||||
return
|
||||
} else {
|
||||
manager.provider.SessionDestroy(cookie.Value)
|
||||
expiration := time.Now()
|
||||
cookie := http.Cookie{Name: manager.config.CookieName,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
Expires: expiration,
|
||||
MaxAge: -1}
|
||||
http.SetCookie(w, &cookie)
|
||||
}
|
||||
manager.provider.SessionDestroy(cookie.Value)
|
||||
expiration := time.Now()
|
||||
cookie = &http.Cookie{Name: manager.config.CookieName,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
Expires: expiration,
|
||||
MaxAge: -1}
|
||||
http.SetCookie(w, cookie)
|
||||
}
|
||||
|
||||
// Get SessionStore by its id.
|
||||
func (manager *Manager) GetSessionStore(sid string) (sessions SessionStore, err error) {
|
||||
// GetSessionStore Get SessionStore by its id.
|
||||
func (manager *Manager) GetSessionStore(sid string) (sessions Store, err error) {
|
||||
sessions, err = manager.provider.SessionRead(sid)
|
||||
return
|
||||
}
|
||||
|
||||
// Start session gc process.
|
||||
// GC Start session gc process.
|
||||
// it can do gc in times after gc lifetime.
|
||||
func (manager *Manager) GC() {
|
||||
manager.provider.SessionGC()
|
||||
time.AfterFunc(time.Duration(manager.config.Gclifetime)*time.Second, func() { manager.GC() })
|
||||
}
|
||||
|
||||
// Regenerate a session id for this SessionStore who's id is saving in http request.
|
||||
func (manager *Manager) SessionRegenerateId(w http.ResponseWriter, r *http.Request) (session SessionStore) {
|
||||
sid, err := manager.sessionId(r)
|
||||
// SessionRegenerateID Regenerate a session id for this SessionStore who's id is saving in http request.
|
||||
func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Request) (session Store) {
|
||||
sid, err := manager.sessionID(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -256,18 +255,18 @@ func (manager *Manager) SessionRegenerateId(w http.ResponseWriter, r *http.Reque
|
||||
return
|
||||
}
|
||||
|
||||
// Get all active sessions count number.
|
||||
// GetActiveSession Get all active sessions count number.
|
||||
func (manager *Manager) GetActiveSession() int {
|
||||
return manager.provider.SessionAll()
|
||||
}
|
||||
|
||||
// Set cookie with https.
|
||||
// SetSecure Set cookie with https.
|
||||
func (manager *Manager) SetSecure(secure bool) {
|
||||
manager.config.Secure = secure
|
||||
}
|
||||
|
||||
func (manager *Manager) sessionId(r *http.Request) (string, error) {
|
||||
b := make([]byte, manager.config.SessionIdLength)
|
||||
func (manager *Manager) sessionID(r *http.Request) (string, error) {
|
||||
b := make([]byte, manager.config.SessionIDLength)
|
||||
n, err := rand.Read(b)
|
||||
if n != len(b) || err != nil {
|
||||
return "", fmt.Errorf("Could not successfully read from the system CSPRNG.")
|
||||
|
Loading…
Reference in New Issue
Block a user