2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2013-04-05 15:50:53 +00:00
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// Package mysql for session provider
|
2014-08-18 08:41:43 +00:00
|
|
|
//
|
|
|
|
// depends on github.com/go-sql-driver/mysql:
|
|
|
|
//
|
|
|
|
// go install github.com/go-sql-driver/mysql
|
|
|
|
//
|
2014-01-29 10:15:09 +00:00
|
|
|
// mysql session support need create table as sql:
|
|
|
|
// CREATE TABLE `session` (
|
|
|
|
// `session_key` char(64) NOT NULL,
|
2015-06-04 13:40:10 +00:00
|
|
|
// `session_data` blob,
|
2014-01-29 10:15:09 +00:00
|
|
|
// `session_expiry` int(11) unsigned NOT NULL,
|
|
|
|
// PRIMARY KEY (`session_key`)
|
|
|
|
// ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
|
2014-08-18 08:41:43 +00:00
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
// import(
|
|
|
|
// _ "github.com/astaxie/beego/session/mysql"
|
|
|
|
// "github.com/astaxie/beego/session"
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// func init() {
|
|
|
|
// globalSessions, _ = session.NewManager("mysql", ``{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]"}``)
|
|
|
|
// go globalSessions.GC()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// more docs: http://beego.me/docs/module/session.md
|
2015-09-12 14:53:55 +00:00
|
|
|
package mysql
|
2013-04-05 15:50:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2014-01-05 06:48:36 +00:00
|
|
|
"net/http"
|
2013-04-05 15:50:53 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
2014-04-04 00:31:22 +00:00
|
|
|
"github.com/astaxie/beego/session"
|
2015-09-12 14:53:55 +00:00
|
|
|
// import mysql driver
|
2013-12-03 13:37:39 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2013-04-05 15:50:53 +00:00
|
|
|
)
|
|
|
|
|
2015-09-05 02:31:31 +00:00
|
|
|
var (
|
2015-09-12 14:53:55 +00:00
|
|
|
// TableName store the session in MySQL
|
2015-09-05 02:31:31 +00:00
|
|
|
TableName = "session"
|
2015-09-12 14:53:55 +00:00
|
|
|
mysqlpder = &Provider{}
|
2015-09-05 02:31:31 +00:00
|
|
|
)
|
2013-04-05 15:50:53 +00:00
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionStore mysql session store
|
|
|
|
type SessionStore struct {
|
2013-04-05 15:50:53 +00:00
|
|
|
c *sql.DB
|
|
|
|
sid string
|
|
|
|
lock sync.RWMutex
|
|
|
|
values map[interface{}]interface{}
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// Set value in mysql session.
|
2014-01-29 10:15:09 +00:00
|
|
|
// it is temp value in map.
|
2015-09-12 14:53:55 +00:00
|
|
|
func (st *SessionStore) Set(key, value interface{}) error {
|
2013-04-05 15:50:53 +00:00
|
|
|
st.lock.Lock()
|
|
|
|
defer st.lock.Unlock()
|
|
|
|
st.values[key] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// Get value from mysql session
|
|
|
|
func (st *SessionStore) Get(key interface{}) interface{} {
|
2013-04-05 15:50:53 +00:00
|
|
|
st.lock.RLock()
|
|
|
|
defer st.lock.RUnlock()
|
|
|
|
if v, ok := st.values[key]; ok {
|
|
|
|
return v
|
|
|
|
}
|
2015-09-12 14:53:55 +00:00
|
|
|
return nil
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// Delete value in mysql session
|
|
|
|
func (st *SessionStore) Delete(key interface{}) error {
|
2013-04-05 15:50:53 +00:00
|
|
|
st.lock.Lock()
|
|
|
|
defer st.lock.Unlock()
|
|
|
|
delete(st.values, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// Flush clear all values in mysql session
|
|
|
|
func (st *SessionStore) Flush() error {
|
2013-09-26 10:07:00 +00:00
|
|
|
st.lock.Lock()
|
|
|
|
defer st.lock.Unlock()
|
|
|
|
st.values = make(map[interface{}]interface{})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionID get session id of this mysql session store
|
|
|
|
func (st *SessionStore) SessionID() string {
|
2013-04-05 15:50:53 +00:00
|
|
|
return st.sid
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionRelease save mysql session values to database.
|
2014-01-29 10:15:09 +00:00
|
|
|
// must call this method to save values to database.
|
2015-09-12 14:53:55 +00:00
|
|
|
func (st *SessionStore) SessionRelease(w http.ResponseWriter) {
|
2013-11-05 13:59:35 +00:00
|
|
|
defer st.c.Close()
|
2014-04-04 00:31:22 +00:00
|
|
|
b, err := session.EncodeGob(st.values)
|
2014-01-08 12:54:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
2016-03-25 02:48:59 +00:00
|
|
|
st.c.Exec("UPDATE "+TableName+" set `session_data`=?, `session_expiry`=? where session_key=?",
|
2014-01-08 12:54:20 +00:00
|
|
|
b, time.Now().Unix(), st.sid)
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// Provider mysql session provider
|
|
|
|
type Provider struct {
|
2013-04-05 15:50:53 +00:00
|
|
|
maxlifetime int64
|
|
|
|
savePath string
|
|
|
|
}
|
|
|
|
|
2014-01-29 10:15:09 +00:00
|
|
|
// connect to mysql
|
2015-09-12 14:53:55 +00:00
|
|
|
func (mp *Provider) connectInit() *sql.DB {
|
2013-04-05 15:50:53 +00:00
|
|
|
db, e := sql.Open("mysql", mp.savePath)
|
|
|
|
if e != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionInit init mysql session.
|
2014-01-29 10:15:09 +00:00
|
|
|
// savepath is the connection string of mysql.
|
2015-09-12 14:53:55 +00:00
|
|
|
func (mp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
2013-04-05 15:50:53 +00:00
|
|
|
mp.maxlifetime = maxlifetime
|
|
|
|
mp.savePath = savePath
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionRead get mysql session by sid
|
|
|
|
func (mp *Provider) SessionRead(sid string) (session.Store, error) {
|
2013-04-05 15:50:53 +00:00
|
|
|
c := mp.connectInit()
|
2015-09-05 02:31:31 +00:00
|
|
|
row := c.QueryRow("select session_data from "+TableName+" where session_key=?", sid)
|
2013-04-05 15:50:53 +00:00
|
|
|
var sessiondata []byte
|
|
|
|
err := row.Scan(&sessiondata)
|
|
|
|
if err == sql.ErrNoRows {
|
2015-09-05 02:31:31 +00:00
|
|
|
c.Exec("insert into "+TableName+"(`session_key`,`session_data`,`session_expiry`) values(?,?,?)",
|
2014-01-08 12:54:20 +00:00
|
|
|
sid, "", time.Now().Unix())
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
var kv map[interface{}]interface{}
|
|
|
|
if len(sessiondata) == 0 {
|
|
|
|
kv = make(map[interface{}]interface{})
|
|
|
|
} else {
|
2014-04-04 00:31:22 +00:00
|
|
|
kv, err = session.DecodeGob(sessiondata)
|
2013-04-05 15:50:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 14:53:55 +00:00
|
|
|
rs := &SessionStore{c: c, sid: sid, values: kv}
|
2013-04-05 15:50:53 +00:00
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionExist check mysql session exist
|
|
|
|
func (mp *Provider) SessionExist(sid string) bool {
|
2013-11-05 14:23:48 +00:00
|
|
|
c := mp.connectInit()
|
2014-01-14 11:54:32 +00:00
|
|
|
defer c.Close()
|
2015-09-05 02:31:31 +00:00
|
|
|
row := c.QueryRow("select session_data from "+TableName+" where session_key=?", sid)
|
2013-11-05 14:23:48 +00:00
|
|
|
var sessiondata []byte
|
|
|
|
err := row.Scan(&sessiondata)
|
2017-03-17 17:24:45 +00:00
|
|
|
return !(err == sql.ErrNoRows)
|
2013-11-05 14:23:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionRegenerate generate new sid for mysql session
|
|
|
|
func (mp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
2013-09-26 10:07:00 +00:00
|
|
|
c := mp.connectInit()
|
2015-09-05 02:31:31 +00:00
|
|
|
row := c.QueryRow("select session_data from "+TableName+" where session_key=?", oldsid)
|
2013-09-26 10:07:00 +00:00
|
|
|
var sessiondata []byte
|
|
|
|
err := row.Scan(&sessiondata)
|
|
|
|
if err == sql.ErrNoRows {
|
2015-09-05 02:31:31 +00:00
|
|
|
c.Exec("insert into "+TableName+"(`session_key`,`session_data`,`session_expiry`) values(?,?,?)", oldsid, "", time.Now().Unix())
|
2013-09-26 10:07:00 +00:00
|
|
|
}
|
2015-09-05 02:31:31 +00:00
|
|
|
c.Exec("update "+TableName+" set `session_key`=? where session_key=?", sid, oldsid)
|
2013-09-26 10:07:00 +00:00
|
|
|
var kv map[interface{}]interface{}
|
|
|
|
if len(sessiondata) == 0 {
|
|
|
|
kv = make(map[interface{}]interface{})
|
|
|
|
} else {
|
2014-04-04 00:31:22 +00:00
|
|
|
kv, err = session.DecodeGob(sessiondata)
|
2013-09-26 10:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 14:53:55 +00:00
|
|
|
rs := &SessionStore{c: c, sid: sid, values: kv}
|
2013-09-26 10:07:00 +00:00
|
|
|
return rs, nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionDestroy delete mysql session by sid
|
|
|
|
func (mp *Provider) SessionDestroy(sid string) error {
|
2013-04-05 15:50:53 +00:00
|
|
|
c := mp.connectInit()
|
2015-09-05 02:31:31 +00:00
|
|
|
c.Exec("DELETE FROM "+TableName+" where session_key=?", sid)
|
2013-04-05 15:50:53 +00:00
|
|
|
c.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionGC delete expired values in mysql session
|
|
|
|
func (mp *Provider) SessionGC() {
|
2013-04-05 15:50:53 +00:00
|
|
|
c := mp.connectInit()
|
2015-09-05 02:31:31 +00:00
|
|
|
c.Exec("DELETE from "+TableName+" where session_expiry < ?", time.Now().Unix()-mp.maxlifetime)
|
2013-04-05 15:50:53 +00:00
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionAll count values in mysql session
|
|
|
|
func (mp *Provider) SessionAll() int {
|
2013-11-01 16:16:10 +00:00
|
|
|
c := mp.connectInit()
|
|
|
|
defer c.Close()
|
|
|
|
var total int
|
2015-09-05 02:31:31 +00:00
|
|
|
err := c.QueryRow("SELECT count(*) as num from " + TableName).Scan(&total)
|
2013-11-01 16:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
func init() {
|
2014-04-04 00:31:22 +00:00
|
|
|
session.Register("mysql", mysqlpder)
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|