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