From cfef97175ef4e7db99f3fe69dcf6f12321be5448 Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Sat, 12 Mar 2016 12:34:54 +0800 Subject: [PATCH 01/16] change import sort --- cache/ssdb/ssdb_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cache/ssdb/ssdb_test.go b/cache/ssdb/ssdb_test.go index e03ba343..dd474960 100644 --- a/cache/ssdb/ssdb_test.go +++ b/cache/ssdb/ssdb_test.go @@ -1,10 +1,11 @@ package ssdb import ( - "github.com/astaxie/beego/cache" "strconv" "testing" "time" + + "github.com/astaxie/beego/cache" ) func TestSsdbcacheCache(t *testing.T) { From fa8d94fa699511e47e20431fc9d2510e155c9d45 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 6 May 2016 12:09:00 +0800 Subject: [PATCH 02/16] file rotate by day test --- logs/file.go | 19 +++++++++++++++++++ logs/file_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/logs/file.go b/logs/file.go index 8cf20372..42a1cb46 100644 --- a/logs/file.go +++ b/logs/file.go @@ -174,9 +174,28 @@ func (w *fileLogWriter) initFd() error { } w.maxLinesCurLines = count } + if w.Daily { + go w.dailyRotate(w.dailyOpenTime) + } return nil } +func (w *fileLogWriter) dailyRotate(openTime time.Time) { + y, m, d := openTime.Add(24 * time.Hour).Date() + nextDay := time.Date(y, m, d, 0, 0, 0, 0, openTime.Location()) + tm := time.NewTimer(time.Duration(nextDay.UnixNano() - openTime.UnixNano() + 100)) + select { + case <-tm.C: + w.Lock() + if w.needRotate(0, time.Now().Day()) { + if err := w.doRotate(time.Now()); err != nil { + fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err) + } + } + w.Unlock() + } +} + func (w *fileLogWriter) lines() (int, error) { fd, err := os.Open(w.Filename) if err != nil { diff --git a/logs/file_test.go b/logs/file_test.go index 176f4c1e..d468606c 100644 --- a/logs/file_test.go +++ b/logs/file_test.go @@ -17,6 +17,7 @@ package logs import ( "bufio" "fmt" + "io/ioutil" "os" "strconv" "testing" @@ -125,6 +126,21 @@ func TestFileRotate_03(t *testing.T) { os.Remove(fn) } +func TestFileRotate_04(t *testing.T) { + fn1 := "rotate_day.log" + fn2 := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".log" + testFileDailyRotate(t, fn1, fn2) +} + +func TestFileRotate_05(t *testing.T) { + fn1 := "rotate_day.log" + fn := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".log" + os.Create(fn) + fn2 := "rotate_day." + time.Now().Add(-24*time.Hour).Format("2006-01-02") + ".001.log" + testFileDailyRotate(t, fn1, fn2) + os.Remove(fn) +} + func testFileRotate(t *testing.T, fn1, fn2 string) { fw := &fileLogWriter{ Daily: true, @@ -145,6 +161,38 @@ func testFileRotate(t *testing.T, fn1, fn2 string) { } os.Remove(file) } + fw.Destroy() +} + +func testFileDailyRotate(t *testing.T, fn1, fn2 string) { + fw := &fileLogWriter{ + Daily: true, + MaxDays: 7, + Rotate: true, + Level: LevelTrace, + Perm: 0660, + } + fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) + fw.dailyOpenTime = time.Now().Add(-24 * time.Hour) + fw.dailyOpenDate = fw.dailyOpenTime.Day() + today, _ := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), fw.dailyOpenTime.Location()) + today = today.Add(-1 * time.Second) + fw.dailyRotate(today) + for _, file := range []string{fn1, fn2} { + _, err := os.Stat(file) + if err != nil { + t.FailNow() + } + content, err := ioutil.ReadFile(file) + if err != nil { + t.FailNow() + } + if len(content) > 0 { + t.FailNow() + } + os.Remove(file) + } + fw.Destroy() } func exists(path string) (bool, error) { From b28581a463dbefccd8bea73461a2958874afbebe Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 6 May 2016 12:11:14 +0800 Subject: [PATCH 03/16] make daily rotate --- logs/file.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/logs/file.go b/logs/file.go index 42a1cb46..b87664bd 100644 --- a/logs/file.go +++ b/logs/file.go @@ -167,6 +167,9 @@ func (w *fileLogWriter) initFd() error { w.dailyOpenTime = time.Now() w.dailyOpenDate = w.dailyOpenTime.Day() w.maxLinesCurLines = 0 + if w.Daily { + go w.dailyRotate(w.dailyOpenTime) + } if fInfo.Size() > 0 { count, err := w.lines() if err != nil { @@ -174,9 +177,6 @@ func (w *fileLogWriter) initFd() error { } w.maxLinesCurLines = count } - if w.Daily { - go w.dailyRotate(w.dailyOpenTime) - } return nil } From a4d4b8de779fdc79f841f431f5be4ef0b16a1eff Mon Sep 17 00:00:00 2001 From: wincss Date: Tue, 10 May 2016 17:45:06 +0800 Subject: [PATCH 04/16] fix #1936 --- orm/db.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/orm/db.go b/orm/db.go index 7796686a..78c72e87 100644 --- a/orm/db.go +++ b/orm/db.go @@ -1475,7 +1475,11 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond sels := strings.Join(cols, ", ") - query := fmt.Sprintf("SELECT %s FROM %s%s%s T0 %s%s%s%s%s", sels, Q, mi.table, Q, join, where, groupBy, orderBy, limit) + sqlSelect := "SELECT" + if qs.distinct { + sqlSelect += " DISTINCT" + } + query := fmt.Sprintf("%s %s FROM %s%s%s T0 %s%s%s%s%s", sqlSelect, sels, Q, mi.table, Q, join, where, groupBy, orderBy, limit) d.ins.ReplaceMarks(&query) From 27be1e7ca3ce2e52efc38b4757623be3041817c2 Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Tue, 10 May 2016 19:56:45 +0800 Subject: [PATCH 05/16] add ssdb-session interface --- session/ssdb/sess_ssdb.go | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 session/ssdb/sess_ssdb.go diff --git a/session/ssdb/sess_ssdb.go b/session/ssdb/sess_ssdb.go new file mode 100644 index 00000000..109f3416 --- /dev/null +++ b/session/ssdb/sess_ssdb.go @@ -0,0 +1,55 @@ +package ssdb + +import ( + "net/http" + + "github.com/astaxie/beego/session" + "github.com/ssdb/gossdb/ssdb" +) + +var ssdbSsdbProvider = &SsdbSsdbProvider{} + +type SsdbProvider struct { +} + +func (r *SsdbProvider) SessionInit(maxlifetime int64, savePath string) error { +} + +func (r *SsdbProvider) SessionRead(sid string) (session.Store, error) { +} + +func (r *SsdbProvider) SessionExist(sid string) bool { +} +func (r *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, error) { +} + +func (r *SsdbProvider) SessionDestroy(sid string) error { +} + +func (r *SsdbProvider) SessionGC() { + return +} + +func (r *SsdbProvider) SessionAll() int { + return 0 +} + +type SessionStore struct { +} + +func (s *SessionStore) Set(key, value interface{}) error { +} +func (s *SessionStore) Get(key interface{}) interface{} { +} +func (s *SessionStore) Delete(key interface{}) error { +} +func (s *SessionStore) Flush() error { +} +func (s *SessionStore) SessionID() string { +} + +func (s *SessionStore) SessionRelease(w http.ResponseWriter) { +} +func init() { + session.Register("redis", redispder) +} From 0e4b9563ae42173f49182168b2d4bc77aed3add9 Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Tue, 10 May 2016 20:08:15 +0800 Subject: [PATCH 06/16] add test file --- session/ssdb/sess_ssdb_test.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 session/ssdb/sess_ssdb_test.go diff --git a/session/ssdb/sess_ssdb_test.go b/session/ssdb/sess_ssdb_test.go new file mode 100644 index 00000000..e69de29b From 153d76e20071f4e963088c9f815aa003e15c4eb9 Mon Sep 17 00:00:00 2001 From: Mark Mindenhall Date: Tue, 10 May 2016 22:54:33 -0600 Subject: [PATCH 07/16] More flexible support for template engines --- template.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/template.go b/template.go index 63eae741..3d07ec55 100644 --- a/template.go +++ b/template.go @@ -33,13 +33,12 @@ import ( var ( beegoTplFuncMap = make(template.FuncMap) // beeTemplates caching map and supported template file extensions. - beeTemplates = make(map[string]TemplateRenderer) + beeTemplates = make(map[string]*template.Template) templatesLock sync.RWMutex // beeTemplateExt stores the template extension which will build beeTemplateExt = []string{"tpl", "html"} // beeTemplatePreprocessors stores associations of extension -> preprocessor handler - beeTemplateEngines = map[string]templateHandler{} -) + beeTemplateEngines = map[string]templatePreProcessor{}) // ExecuteTemplate applies the template with name to the specified data object, // writing the output to wr. @@ -50,11 +49,22 @@ func ExecuteTemplate(wr io.Writer, name string, data interface{}) error { defer templatesLock.RUnlock() } if t, ok := beeTemplates[name]; ok { - err := t.ExecuteTemplate(wr, name, data) - if err != nil { - logs.Trace("template Execute err:", err) + if t.Lookup(name) != nil { + err := t.ExecuteTemplate(wr, name, data) + if err != nil { + logs.Trace("template Execute err:", err) + } + return err + } else { + err := t.Execute(wr, data) + if err != nil { + if err != nil { + logs.Trace("template Execute err:", err) + } + return err + } } - return err + return nil } panic("can't find templatefile in the path:" + name) } @@ -94,10 +104,8 @@ func AddFuncMap(key string, fn interface{}) error { return nil } -type templateHandler func(root, path string, funcs template.FuncMap) (TemplateRenderer, error) -type TemplateRenderer interface { - ExecuteTemplate(wr io.Writer, name string, data interface{}) error -} +type templatePreProcessor func(root, path string, funcs template.FuncMap) (*template.Template, error) + type templateFile struct { root string files map[string][]string @@ -172,7 +180,7 @@ func BuildTemplate(dir string, files ...string) error { if buildAllFiles || utils.InSlice(file, files) { templatesLock.Lock() ext := filepath.Ext(file) - var t TemplateRenderer + var t *template.Template if len(ext) == 0 { t, err = getTemplate(self.root, file, v...) } else if fn, ok := beeTemplateEngines[ext[1:]]; ok { @@ -325,7 +333,7 @@ func DelStaticPath(url string) *App { return BeeApp } -func AddTemplateEngine(extension string, fn templateHandler) *App { +func AddTemplateEngine(extension string, fn templatePreProcessor) *App { AddTemplateExt(extension) beeTemplateEngines[extension] = fn return BeeApp From d679a4b865f24a806ec6e4a3d3b9a977eded9f5f Mon Sep 17 00:00:00 2001 From: nullne Date: Sat, 14 May 2016 10:54:09 +0800 Subject: [PATCH 08/16] fix bug with file permission in log module --- logs/file.go | 14 +++++++++++--- logs/file_test.go | 25 +++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/logs/file.go b/logs/file.go index b87664bd..f75e5bae 100644 --- a/logs/file.go +++ b/logs/file.go @@ -22,6 +22,7 @@ import ( "io" "os" "path/filepath" + "strconv" "strings" "sync" "time" @@ -53,7 +54,8 @@ type fileLogWriter struct { Level int `json:"level"` - Perm os.FileMode `json:"perm"` + Perm string `json:"perm"` + perm os.FileMode fileNameOnly, suffix string // like "project.log", project is fileNameOnly and .log is suffix } @@ -65,7 +67,8 @@ func newFileWriter() Logger { MaxDays: 7, Rotate: true, Level: LevelTrace, - Perm: 0660, + Perm: "0660", + perm: 0660, } return w } @@ -89,6 +92,11 @@ func (w *fileLogWriter) Init(jsonConfig string) error { if len(w.Filename) == 0 { return errors.New("jsonconfig must have filename") } + perm, err := strconv.ParseInt(w.Perm, 8, 64) + if err != nil { + return err + } + w.perm = os.FileMode(perm) w.suffix = filepath.Ext(w.Filename) w.fileNameOnly = strings.TrimSuffix(w.Filename, w.suffix) if w.suffix == "" { @@ -153,7 +161,7 @@ func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error { func (w *fileLogWriter) createLogFile() (*os.File, error) { // Open the log file - fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, w.Perm) + fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, w.perm) return fd, err } diff --git a/logs/file_test.go b/logs/file_test.go index d468606c..23370947 100644 --- a/logs/file_test.go +++ b/logs/file_test.go @@ -24,6 +24,27 @@ import ( "time" ) +func TestFilePerm(t *testing.T) { + log := NewLogger(10000) + log.SetLogger("file", `{"filename":"test.log", "perm": "0600"}`) + log.Debug("debug") + log.Informational("info") + log.Notice("notice") + log.Warning("warning") + log.Error("error") + log.Alert("alert") + log.Critical("critical") + log.Emergency("emergency") + file, err := os.Stat("test.log") + if err != nil { + t.Fatal(err) + } + if file.Mode() != 0600 { + t.Fatal("unexpected log file permission") + } + os.Remove("test.log") +} + func TestFile1(t *testing.T) { log := NewLogger(10000) log.SetLogger("file", `{"filename":"test.log"}`) @@ -147,7 +168,7 @@ func testFileRotate(t *testing.T, fn1, fn2 string) { MaxDays: 7, Rotate: true, Level: LevelTrace, - Perm: 0660, + Perm: "0660", } fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) fw.dailyOpenTime = time.Now().Add(-24 * time.Hour) @@ -170,7 +191,7 @@ func testFileDailyRotate(t *testing.T, fn1, fn2 string) { MaxDays: 7, Rotate: true, Level: LevelTrace, - Perm: 0660, + Perm: "0660", } fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) fw.dailyOpenTime = time.Now().Add(-24 * time.Hour) From e50f4f5631957a741589398f0d14a9e6df241d53 Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Sun, 15 May 2016 12:15:40 +0800 Subject: [PATCH 09/16] complete sess_ssdb.go --- session/ssdb/sess_ssdb.go | 148 +++++++++++++++++++++++++++++++++++--- 1 file changed, 138 insertions(+), 10 deletions(-) diff --git a/session/ssdb/sess_ssdb.go b/session/ssdb/sess_ssdb.go index 109f3416..26dd3e8a 100644 --- a/session/ssdb/sess_ssdb.go +++ b/session/ssdb/sess_ssdb.go @@ -2,54 +2,182 @@ package ssdb import ( "net/http" + "strconv" + "strings" + "sync" "github.com/astaxie/beego/session" "github.com/ssdb/gossdb/ssdb" ) -var ssdbSsdbProvider = &SsdbSsdbProvider{} +var ssdbProvider = &SsdbProvider{} type SsdbProvider struct { + client *ssdb.Client + host string + port int32 + maxLifetime int64 } -func (r *SsdbProvider) SessionInit(maxlifetime int64, savePath string) error { +func (p *SsdbProvider) connectInit() error { + var err error + p.client, err = ssdb.Connect(p.host, p.port) + if err != nil { + return err + } + return nil } -func (r *SsdbProvider) SessionRead(sid string) (session.Store, error) { +func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error { + p.maxLifetime = maxLifetime + address := strings.Split(savePath, ":") + p.host = address[0] + port, e := strconv.Atoi(address[1]) + if e != nil { + return e + } + p.port = address[1] + err := p.connectinit() + if err != nil { + return err + } + return nil } -func (r *SsdbProvider) SessionExist(sid string) bool { -} -func (r *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, error) { +func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) { + if p.client == nil { + if err := p.connectInit(); err != nil { + return nil, err + } + } + var kv map[interface{}]interface{} + value, err := p.client.Get(sid) + if err != nil { + return nil, err + } + if value == nil || len(value) == 0 { + kv = make(map[interface{}]interface{}) + } else { + kv, err = session.DecodeGob([]byte(value)) + if err != nil { + return nil, err + } + } + rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client} + return rs, nil } -func (r *SsdbProvider) SessionDestroy(sid string) error { +func (p *SsdbProvider) SessionExist(sid string) bool { + if p.client == nil { + if err := p.connectInit(); err != nil { + return nil, err + } + } + if value == nil || len(value) == 0 { + return false + } + return true + +} +func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, error) { + //conn.Do("setx", key, v, ttl) + if p.client == nil { + if err := p.connectInit(); err != nil { + return nil, err + } + } + value, err := p.client.Get(oldsid) + if err != nil || len(value) == 0 { + value = "" + } else { + err = p.client.Del(sid) + } + _, e := p.client.Do("setx", sid, value, p.maxLifetime) + if e != nil { + return nil, e + } + var kv map[interface{}]interface{} + if value == nil || len(value) == 0 { + kv = make(map[interface{}]interface{}) + } else { + var err error + kv, err = session.DecodeGob(value) + if err != nil { + return nil, err + } + } + rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client} + return rs, nil } -func (r *SsdbProvider) SessionGC() { +func (p *SsdbProvider) SessionDestroy(sid string) error { + if p.client == nil { + if err := p.connectInit(); err != nil { + return nil, err + } + } + flag, err := p.client.Del(sid) + if err != nil { + return err + } + return nil +} + +func (p *SsdbProvider) SessionGC() { return } -func (r *SsdbProvider) SessionAll() int { +func (p *SsdbProvider) SessionAll() int { return 0 } type SessionStore struct { + sid string + lock sync.RWMutex + values map[interface{}]interface{} + maxLifetime int64 + client *ssdb.Client } func (s *SessionStore) Set(key, value interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + s.values[key] = value + return nil } func (s *SessionStore) Get(key interface{}) interface{} { + s.lock.Lock() + defer s.lock.Unlock() + if value, ok := rs.values[key]; ok { + return value + } + return nil } + func (s *SessionStore) Delete(key interface{}) error { + s.lock.Lock() + defer s.lock.Unlock() + delete(s.values, key) + return nil } func (s *SessionStore) Flush() error { + s.lock.Lock() + defer s.lock.Unlock() + s.values = make(map[interface{}]interface{}) + return nil } func (s *SessionStore) SessionID() string { + return s.sid } func (s *SessionStore) SessionRelease(w http.ResponseWriter) { + b, err := session.EncodeGob(s.values) + if err != nil { + return + } + s.client.Do("setx", s.sid, s.values, s.maxLifetime) + } func init() { - session.Register("redis", redispder) + session.Register("ssdb", ssdbProvider) } From 2c12383263fdd1247dbca150eb32abd1176115e7 Mon Sep 17 00:00:00 2001 From: nullne Date: Tue, 17 May 2016 10:29:05 +0800 Subject: [PATCH 10/16] remove attribute perm to make it more brief --- logs/file.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/logs/file.go b/logs/file.go index f75e5bae..7798a221 100644 --- a/logs/file.go +++ b/logs/file.go @@ -55,7 +55,6 @@ type fileLogWriter struct { Level int `json:"level"` Perm string `json:"perm"` - perm os.FileMode fileNameOnly, suffix string // like "project.log", project is fileNameOnly and .log is suffix } @@ -68,7 +67,6 @@ func newFileWriter() Logger { Rotate: true, Level: LevelTrace, Perm: "0660", - perm: 0660, } return w } @@ -82,7 +80,7 @@ func newFileWriter() Logger { // "daily":true, // "maxDays":15, // "rotate":true, -// "perm":0600 +// "perm":"0600" // } func (w *fileLogWriter) Init(jsonConfig string) error { err := json.Unmarshal([]byte(jsonConfig), w) @@ -92,11 +90,6 @@ func (w *fileLogWriter) Init(jsonConfig string) error { if len(w.Filename) == 0 { return errors.New("jsonconfig must have filename") } - perm, err := strconv.ParseInt(w.Perm, 8, 64) - if err != nil { - return err - } - w.perm = os.FileMode(perm) w.suffix = filepath.Ext(w.Filename) w.fileNameOnly = strings.TrimSuffix(w.Filename, w.suffix) if w.suffix == "" { @@ -161,7 +154,11 @@ func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error { func (w *fileLogWriter) createLogFile() (*os.File, error) { // Open the log file - fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, w.perm) + perm, err := strconv.ParseInt(w.Perm, 8, 64) + if err != nil { + return nil, err + } + fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(perm)) return fd, err } From 6234b50111b867a34ad8fa9b1bde4450bc6eed80 Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Thu, 19 May 2016 21:00:04 +0800 Subject: [PATCH 11/16] test done --- session/ssdb/sess_ssdb.go | 50 +++++++++++++++------------- session/ssdb/sess_ssdb_test.go | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 22 deletions(-) diff --git a/session/ssdb/sess_ssdb.go b/session/ssdb/sess_ssdb.go index 26dd3e8a..cbb94840 100644 --- a/session/ssdb/sess_ssdb.go +++ b/session/ssdb/sess_ssdb.go @@ -15,7 +15,7 @@ var ssdbProvider = &SsdbProvider{} type SsdbProvider struct { client *ssdb.Client host string - port int32 + port int maxLifetime int64 } @@ -29,15 +29,15 @@ func (p *SsdbProvider) connectInit() error { } func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error { + var e error = nil p.maxLifetime = maxLifetime address := strings.Split(savePath, ":") p.host = address[0] - port, e := strconv.Atoi(address[1]) + p.port, e = strconv.Atoi(address[1]) if e != nil { return e } - p.port = address[1] - err := p.connectinit() + err := p.connectInit() if err != nil { return err } @@ -55,10 +55,10 @@ func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) { if err != nil { return nil, err } - if value == nil || len(value) == 0 { + if value == nil || len(value.(string)) == 0 { kv = make(map[interface{}]interface{}) } else { - kv, err = session.DecodeGob([]byte(value)) + kv, err = session.DecodeGob([]byte(value.(string))) if err != nil { return nil, err } @@ -70,10 +70,14 @@ func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) { func (p *SsdbProvider) SessionExist(sid string) bool { if p.client == nil { if err := p.connectInit(); err != nil { - return nil, err + panic(err) } } - if value == nil || len(value) == 0 { + value, err := p.client.Get(sid) + if err != nil { + panic(err) + } + if value == nil || len(value.(string)) == 0 { return false } return true @@ -87,24 +91,26 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err } } value, err := p.client.Get(oldsid) - if err != nil || len(value) == 0 { - value = "" - } else { - err = p.client.Del(sid) - } - _, e := p.client.Do("setx", sid, value, p.maxLifetime) - if e != nil { - return nil, e + if err != nil { + return nil, err } var kv map[interface{}]interface{} - if value == nil || len(value) == 0 { + if value == nil || len(value.(string)) == 0 { kv = make(map[interface{}]interface{}) } else { var err error - kv, err = session.DecodeGob(value) + kv, err = session.DecodeGob([]byte(value.(string))) if err != nil { return nil, err } + _, err = p.client.Del(oldsid) + if err != nil { + return nil, err + } + } + _, e := p.client.Do("setx", sid, value.(string), p.maxLifetime) + if e != nil { + return nil, e } rs := &SessionStore{sid: sid, values: kv, maxLifetime: p.maxLifetime, client: p.client} return rs, nil @@ -113,10 +119,10 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err func (p *SsdbProvider) SessionDestroy(sid string) error { if p.client == nil { if err := p.connectInit(); err != nil { - return nil, err + return err } } - flag, err := p.client.Del(sid) + _, err := p.client.Del(sid) if err != nil { return err } @@ -148,7 +154,7 @@ func (s *SessionStore) Set(key, value interface{}) error { func (s *SessionStore) Get(key interface{}) interface{} { s.lock.Lock() defer s.lock.Unlock() - if value, ok := rs.values[key]; ok { + if value, ok := s.values[key]; ok { return value } return nil @@ -175,7 +181,7 @@ func (s *SessionStore) SessionRelease(w http.ResponseWriter) { if err != nil { return } - s.client.Do("setx", s.sid, s.values, s.maxLifetime) + s.client.Do("setx", s.sid, string(b), s.maxLifetime) } func init() { diff --git a/session/ssdb/sess_ssdb_test.go b/session/ssdb/sess_ssdb_test.go index e69de29b..dfeba4a3 100644 --- a/session/ssdb/sess_ssdb_test.go +++ b/session/ssdb/sess_ssdb_test.go @@ -0,0 +1,60 @@ +package ssdb + +import ( + "fmt" + "net/http" + "testing" +) + +func Test(t *testing.T) { + p := &SsdbProvider{} + p.SessionInit(300, "127.0.0.1:8888") + if p.host != "127.0.0.1" || p.port != 8888 { + t.Error("host:port err") + } + if p.client == nil { + t.Error("client err") + } + ss, err := p.SessionRead("1") + if err != nil { + t.Error(err) + } + err = ss.Set("key", "value") + if err != nil { + t.Error(err) + } + if ss.Get("key") != "value" { + t.Error("Get err") + } + err = ss.Delete("key") + //err = ss.Flush() + if err != nil { + t.Error(err) + } + if ss.Get("key") == "value" { + t.Error("Delete/Flush err") + } + if ss.SessionID() != "1" { + t.Error("id err") + } + + ss.Set("key1", "value1") + var w http.ResponseWriter + ss.SessionRelease(w) + new, e := p.SessionRead("1") + if new == nil || e != nil { + t.Error(e) + } + if !p.SessionExist("1") { + t.Error("SessionExist err") + } + newS, er := p.SessionRegenerate("1", "3") + if er != nil || newS == nil { + t.Error("SessionRegenerate err") + } + if p.SessionExist("1") { + t.Error("SessionExist err") + } + fmt.Println(newS) + +} From 4890dd708c9b9245ed969339e12d2c70a5c4303d Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Fri, 20 May 2016 10:27:18 +0800 Subject: [PATCH 12/16] clear code --- session/ssdb/sess_ssdb.go | 7 +++++-- session/ssdb/sess_ssdb_test.go | 3 --- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/session/ssdb/sess_ssdb.go b/session/ssdb/sess_ssdb.go index cbb94840..4dcf160a 100644 --- a/session/ssdb/sess_ssdb.go +++ b/session/ssdb/sess_ssdb.go @@ -1,6 +1,7 @@ package ssdb import ( + "errors" "net/http" "strconv" "strings" @@ -21,6 +22,9 @@ type SsdbProvider struct { func (p *SsdbProvider) connectInit() error { var err error + if p.host == "" || p.port == 0 { + return errors.New("SessionInit First") + } p.client, err = ssdb.Connect(p.host, p.port) if err != nil { return err @@ -98,7 +102,6 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err if value == nil || len(value.(string)) == 0 { kv = make(map[interface{}]interface{}) } else { - var err error kv, err = session.DecodeGob([]byte(value.(string))) if err != nil { return nil, err @@ -108,7 +111,7 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err return nil, err } } - _, e := p.client.Do("setx", sid, value.(string), p.maxLifetime) + _, e := p.client.Do("setx", sid, value, p.maxLifetime) if e != nil { return nil, e } diff --git a/session/ssdb/sess_ssdb_test.go b/session/ssdb/sess_ssdb_test.go index dfeba4a3..4a5933f2 100644 --- a/session/ssdb/sess_ssdb_test.go +++ b/session/ssdb/sess_ssdb_test.go @@ -1,7 +1,6 @@ package ssdb import ( - "fmt" "net/http" "testing" ) @@ -55,6 +54,4 @@ func Test(t *testing.T) { if p.SessionExist("1") { t.Error("SessionExist err") } - fmt.Println(newS) - } From e77a591a6c28d90cc0c9daf23f2cb5030878362e Mon Sep 17 00:00:00 2001 From: lcbluestorm Date: Fri, 20 May 2016 17:25:22 +0800 Subject: [PATCH 13/16] delete test file --- session/ssdb/sess_ssdb_test.go | 57 ---------------------------------- 1 file changed, 57 deletions(-) delete mode 100644 session/ssdb/sess_ssdb_test.go diff --git a/session/ssdb/sess_ssdb_test.go b/session/ssdb/sess_ssdb_test.go deleted file mode 100644 index 4a5933f2..00000000 --- a/session/ssdb/sess_ssdb_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package ssdb - -import ( - "net/http" - "testing" -) - -func Test(t *testing.T) { - p := &SsdbProvider{} - p.SessionInit(300, "127.0.0.1:8888") - if p.host != "127.0.0.1" || p.port != 8888 { - t.Error("host:port err") - } - if p.client == nil { - t.Error("client err") - } - ss, err := p.SessionRead("1") - if err != nil { - t.Error(err) - } - err = ss.Set("key", "value") - if err != nil { - t.Error(err) - } - if ss.Get("key") != "value" { - t.Error("Get err") - } - err = ss.Delete("key") - //err = ss.Flush() - if err != nil { - t.Error(err) - } - if ss.Get("key") == "value" { - t.Error("Delete/Flush err") - } - if ss.SessionID() != "1" { - t.Error("id err") - } - - ss.Set("key1", "value1") - var w http.ResponseWriter - ss.SessionRelease(w) - new, e := p.SessionRead("1") - if new == nil || e != nil { - t.Error(e) - } - if !p.SessionExist("1") { - t.Error("SessionExist err") - } - newS, er := p.SessionRegenerate("1", "3") - if er != nil || newS == nil { - t.Error("SessionRegenerate err") - } - if p.SessionExist("1") { - t.Error("SessionExist err") - } -} From f32392e956316f001e4d7e2e47890d22fc9b2c7a Mon Sep 17 00:00:00 2001 From: tinyproxy Date: Sat, 21 May 2016 15:19:21 +0800 Subject: [PATCH 14/16] net/http will do it better --- context/context.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/context/context.go b/context/context.go index 63a1313d..d5226ec0 100644 --- a/context/context.go +++ b/context/context.go @@ -70,10 +70,8 @@ func (ctx *Context) Reset(rw http.ResponseWriter, r *http.Request) { } // Redirect does redirection to localurl with http header status code. -// It sends http response header directly. func (ctx *Context) Redirect(status int, localurl string) { - ctx.Output.Header("Location", localurl) - ctx.ResponseWriter.WriteHeader(status) + http.Redirect(ctx.ResponseWriter, ctx.Request, localurl, status) } // Abort stops this request. From 24b887063728f4f93d8d6d9a91bf612b3032d303 Mon Sep 17 00:00:00 2001 From: Mark Mindenhall Date: Mon, 23 May 2016 21:43:18 -0600 Subject: [PATCH 15/16] move paren to new line --- template.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/template.go b/template.go index 3d07ec55..494acc4f 100644 --- a/template.go +++ b/template.go @@ -38,7 +38,8 @@ var ( // beeTemplateExt stores the template extension which will build beeTemplateExt = []string{"tpl", "html"} // beeTemplatePreprocessors stores associations of extension -> preprocessor handler - beeTemplateEngines = map[string]templatePreProcessor{}) + beeTemplateEngines = map[string]templatePreProcessor{} +) // ExecuteTemplate applies the template with name to the specified data object, // writing the output to wr. From d4d7621942907818d40e51f61576f20bfaa51d91 Mon Sep 17 00:00:00 2001 From: wy65701436 Date: Fri, 27 May 2016 02:03:58 -0700 Subject: [PATCH 16/16] modify the error log for registerModel to tell user the default hard code PK is 'id'. --- orm/models_boot.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orm/models_boot.go b/orm/models_boot.go index 3690557b..c9905330 100644 --- a/orm/models_boot.go +++ b/orm/models_boot.go @@ -66,7 +66,7 @@ func registerModel(prefix string, model interface{}) { } if info.fields.pk == nil { - fmt.Printf(" `%s` need a primary key field\n", name) + fmt.Printf(" `%s` need a primary key field, default use 'id' if not set\n", name) os.Exit(2) }