2017-05-20 22:06:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-12-13 18:49:13 +00:00
|
|
|
"context"
|
2017-05-20 22:06:30 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2019-03-21 15:05:23 +00:00
|
|
|
"path/filepath"
|
2017-05-20 22:06:30 +00:00
|
|
|
"time"
|
|
|
|
|
2019-03-27 14:18:52 +00:00
|
|
|
"christine.website/internal"
|
|
|
|
"christine.website/internal/blog"
|
2019-03-21 15:05:23 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2019-01-12 17:05:00 +00:00
|
|
|
"within.website/ln"
|
2019-03-23 01:42:25 +00:00
|
|
|
"within.website/ln/opname"
|
2017-05-20 22:06:30 +00:00
|
|
|
)
|
|
|
|
|
2019-03-23 01:42:25 +00:00
|
|
|
var (
|
|
|
|
templateRenderTime = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
|
|
|
Name: "template_render_time",
|
|
|
|
Help: "Template render time in nanoseconds",
|
|
|
|
}, []string{"name"})
|
|
|
|
)
|
|
|
|
|
|
|
|
func logTemplateTime(ctx context.Context, name string, f ln.F, from time.Time) {
|
|
|
|
dur := time.Since(from)
|
|
|
|
templateRenderTime.With(prometheus.Labels{"name": name}).Observe(float64(dur))
|
|
|
|
ln.Log(ctx, f, ln.F{"dur": dur, "name": name})
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-03-23 01:42:25 +00:00
|
|
|
ctx := opname.With(r.Context(), "renderTemplatePage")
|
2019-03-27 14:18:52 +00:00
|
|
|
fetag := "W/" + internal.Hash(templateFname, etag) + "-1"
|
2019-01-26 14:38:22 +00:00
|
|
|
|
2019-03-21 14:55:32 +00:00
|
|
|
f := ln.F{"etag": fetag, "if_none_match": r.Header.Get("If-None-Match")}
|
2019-01-26 14:38:22 +00:00
|
|
|
|
|
|
|
if r.Header.Get("If-None-Match") == fetag {
|
|
|
|
http.Error(w, "Cached data OK", http.StatusNotModified)
|
2019-03-23 01:42:25 +00:00
|
|
|
ln.Log(ctx, f, ln.Info("Cache hit"))
|
2019-01-26 14:38:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:25 +00:00
|
|
|
defer logTemplateTime(ctx, templateFname, f, time.Now())
|
2017-05-20 22:06:30 +00:00
|
|
|
|
|
|
|
var t *template.Template
|
|
|
|
var err error
|
|
|
|
|
2019-03-21 14:55:32 +00:00
|
|
|
t, err = template.ParseFiles("templates/base.html", "templates/"+templateFname)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2019-03-23 01:42:25 +00:00
|
|
|
ln.Error(ctx, err, ln.F{"action": "renderTemplatePage", "page": templateFname})
|
2019-03-21 14:55:32 +00:00
|
|
|
fmt.Fprintf(w, "error: %v", err)
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 14:38:22 +00:00
|
|
|
w.Header().Set("ETag", fetag)
|
|
|
|
w.Header().Set("Cache-Control", "max-age=432000")
|
|
|
|
|
2017-05-20 22:06:30 +00:00
|
|
|
err = t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-21 15:05:23 +00:00
|
|
|
var postView = promauto.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
Name: "posts_viewed",
|
2019-05-22 01:47:09 +00:00
|
|
|
Help: "The number of views per post or talk",
|
2019-03-21 15:05:23 +00:00
|
|
|
}, []string{"base"})
|
|
|
|
|
2019-05-22 01:47:09 +00:00
|
|
|
func (s *Site) showTalk(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.RequestURI == "/talks/" {
|
|
|
|
http.Redirect(w, r, "/talks", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmp := r.URL.Path[1:]
|
|
|
|
var p blog.Post
|
|
|
|
var found bool
|
|
|
|
for _, pst := range s.Talks {
|
|
|
|
if pst.Link == cmp {
|
|
|
|
p = pst
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
s.renderTemplatePage("error.html", "no such post found: "+r.RequestURI).ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const dateFormat = `2006-01-02`
|
|
|
|
h := s.renderTemplatePage("talkpost.html", struct {
|
|
|
|
Title string
|
|
|
|
Link string
|
|
|
|
BodyHTML template.HTML
|
|
|
|
Date string
|
|
|
|
SlidesLink string
|
|
|
|
}{
|
|
|
|
Title: p.Title,
|
|
|
|
Link: p.Link,
|
|
|
|
BodyHTML: p.BodyHTML,
|
|
|
|
Date: p.Date.Format(dateFormat),
|
|
|
|
SlidesLink: p.SlidesLink,
|
|
|
|
})
|
|
|
|
|
|
|
|
if h == nil {
|
|
|
|
panic("how did we get here?")
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2017-05-20 22:06:30 +00:00
|
|
|
func (s *Site) showPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.RequestURI == "/blog/" {
|
|
|
|
http.Redirect(w, r, "/blog", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:14:00 +00:00
|
|
|
cmp := r.URL.Path[1:]
|
2019-03-27 14:18:52 +00:00
|
|
|
var p blog.Post
|
|
|
|
var found bool
|
2017-05-20 22:06:30 +00:00
|
|
|
for _, pst := range s.Posts {
|
2018-12-10 16:14:00 +00:00
|
|
|
if pst.Link == cmp {
|
2017-05-20 22:06:30 +00:00
|
|
|
p = pst
|
2019-03-27 14:18:52 +00:00
|
|
|
found = true
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 14:18:52 +00:00
|
|
|
if !found {
|
2017-05-20 22:06:30 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
s.renderTemplatePage("error.html", "no such post found: "+r.RequestURI).ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-27 14:31:57 +00:00
|
|
|
const dateFormat = `2006-01-02`
|
|
|
|
s.renderTemplatePage("blogpost.html", struct {
|
|
|
|
Title string
|
|
|
|
Link string
|
|
|
|
BodyHTML template.HTML
|
|
|
|
Date string
|
|
|
|
}{
|
|
|
|
Title: p.Title,
|
|
|
|
Link: p.Link,
|
|
|
|
BodyHTML: p.BodyHTML,
|
|
|
|
Date: p.Date.Format(dateFormat),
|
|
|
|
}).ServeHTTP(w, r)
|
2019-03-21 15:05:23 +00:00
|
|
|
postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc()
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|