cmd/site: per post view counters

This commit is contained in:
Cadey Ratio 2019-03-21 08:05:23 -07:00
parent 368bd244ae
commit 9664dc2847
1 changed files with 9 additions and 0 deletions

View File

@ -5,8 +5,11 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"net/http" "net/http"
"path/filepath"
"time" "time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"within.website/ln" "within.website/ln"
) )
@ -49,6 +52,11 @@ func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.H
}) })
} }
var postView = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "posts_viewed",
Help: "The number of views per post",
}, []string{"base"})
func (s *Site) showPost(w http.ResponseWriter, r *http.Request) { func (s *Site) showPost(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/blog/" { if r.RequestURI == "/blog/" {
http.Redirect(w, r, "/blog", http.StatusSeeOther) http.Redirect(w, r, "/blog", http.StatusSeeOther)
@ -70,4 +78,5 @@ func (s *Site) showPost(w http.ResponseWriter, r *http.Request) {
} }
s.renderTemplatePage("blogpost.html", p).ServeHTTP(w, r) s.renderTemplatePage("blogpost.html", p).ServeHTTP(w, r)
postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc()
} }