From 182389dbb19821649ff3517e5fc908f7f6f05b97 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Mon, 8 Feb 2016 08:37:22 -0500 Subject: [PATCH] Loads current text statically. Faster loading --- db.go | 31 +++++++++++++++++++++++++++++++ routes.go | 5 +++-- static/js/cowyo.js | 2 +- templates/index.tmpl | 2 +- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/db.go b/db.go index 5729e8a..a1e5c41 100644 --- a/db.go +++ b/db.go @@ -77,6 +77,37 @@ func hasPassword(title string) (bool, error) { return hasPassword, nil } +func getCurrentText(title string) string { + title = strings.ToLower(title) + currentText := "" + if !open { + return currentText + } + err := db.View(func(tx *bolt.Tx) error { + var err error + b := tx.Bucket([]byte("datas")) + if b == nil { + return fmt.Errorf("db must be opened before loading!") + } + k := []byte(title) + val := b.Get(k) + if val == nil { + return nil + } + var p CowyoData + err = p.decode(val) + if err != nil { + return err + } + currentText = p.CurrentText + return nil + }) + if err != nil { + fmt.Printf("Could not get CowyoData: %s", err) + } + return currentText +} + func (p *CowyoData) load(title string) error { title = strings.ToLower(title) if !open { diff --git a/routes.go b/routes.go index 9414fe0..d9118c4 100644 --- a/routes.go +++ b/routes.go @@ -30,8 +30,9 @@ func editNote(c *gin.Context) { c.Redirect(302, "/"+title+"/view") } else { c.HTML(http.StatusOK, "index.tmpl", gin.H{ - "Title": title, - "ExternalIP": RuntimeArgs.ExternalIP, + "Title": title, + "ExternalIP": RuntimeArgs.ExternalIP, + "CurrentText": getCurrentText(title), }) } } diff --git a/static/js/cowyo.js b/static/js/cowyo.js index b8aaa7d..7024f80 100644 --- a/static/js/cowyo.js +++ b/static/js/cowyo.js @@ -54,7 +54,7 @@ $(document).ready(function() { } c.onopen = function(){ - updateText(); + // updateText(); updateInterval = setInterval(updateText, pollToGetNewestCopyInterval); } }); diff --git a/templates/index.tmpl b/templates/index.tmpl index 21925c2..b3c0563 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -76,7 +76,7 @@ +Anyone with the URL can access this note.">{{ .CurrentText }}