stats added

This commit is contained in:
Christian Müller
2017-09-11 23:03:01 +02:00
parent 852f7785e8
commit 9a2d6f516a
3 changed files with 35 additions and 9 deletions
+3 -3
View File
@@ -13,9 +13,9 @@
</article>
<footer>
<a href="/">&#8962; notehub</a> &middot;
<a href="{{.ID}}/stats">statistics</a> &middot;
<a href="{{.ID}}/edit">edit</a> &middot;
<a href="{{.ID}}/export">export</a> &middot;
<a href="/{{.ID}}/stats">statistics</a> &middot;
<a href="/{{.ID}}/edit">edit</a> &middot;
<a href="/{{.ID}}/export">export</a> &middot;
<a href="/TOS.md">terms of service</a>
</footer>
</body>
+8
View File
@@ -0,0 +1,8 @@
{{define "Stats"}}
<h2>Statistics</h2>
<table>
<tr><td>Published</td><td>{{.Published}}</td></tr>
<tr><td>Edited</td><td>{{.Edited}}</td></tr>
<tr><td>Views</td><td>{{.Views}}</td></tr>
</table>
{{end}}
+21 -3
View File
@@ -1,11 +1,13 @@
package main
import (
"bytes"
"html/template"
"io"
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"
"database/sql"
@@ -22,6 +24,10 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Con
return t.templates.ExecuteTemplate(w, name, data)
}
func (t *Template) Lookup(name string) *template.Template {
return t.templates.Lookup(name)
}
func main() {
e := echo.New()
db, err := sql.Open("sqlite3", "./database.sqlite")
@@ -30,7 +36,8 @@ func main() {
}
defer db.Close()
e.Renderer = &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))}
renderer := &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))}
e.Renderer = renderer
e.File("/favicon.ico", "assets/public/favicon.ico")
e.File("/robots.txt", "assets/public/robots.txt")
@@ -50,6 +57,13 @@ func main() {
n, code := note(c, db)
return c.String(code, n.Text)
})
e.GET("/:id/stats", func(c echo.Context) error {
n, code := note(c, db)
buf := bytes.NewBuffer([]byte{})
renderer.Lookup("Stats").Execute(buf, n)
n.Content = template.HTML(buf.String())
return c.Render(code, "Note", n)
})
e.Logger.Fatal(e.Start(":3000"))
}
@@ -67,7 +81,7 @@ func (n Note) withTitle() Note {
if len(fstLine) < 25 {
maxLength = len(fstLine)
}
n.Title = rexpNonAlphaNum.ReplaceAllString(fstLine[:maxLength], "")
n.Title = strings.TrimSpace(rexpNonAlphaNum.ReplaceAllString(fstLine[:maxLength], ""))
return n
}
@@ -85,7 +99,8 @@ func note(c echo.Context, db *sql.DB) (Note, int) {
}
defer stmt.Close()
row := stmt.QueryRow(c.Param("id"))
var id, text, password, published, edited string
var id, text, password string
var published, edited int64
var views int
if err := row.Scan(&id, &text, &published, &edited, &password, &views); err != nil {
c.Logger().Error(err)
@@ -94,6 +109,9 @@ func note(c echo.Context, db *sql.DB) (Note, int) {
return Note{
ID: id,
Text: text,
Views: views,
Published: time.Unix(published, 0),
Edited: time.Unix(edited, 0),
Content: mdTmplHTML([]byte(text)),
}.withTitle(), http.StatusOK
}