xesite/cmd/site/html.go

195 lines
4.5 KiB
Go
Raw Normal View History

package main
import (
2017-12-13 18:49:13 +00:00
"context"
"fmt"
"html/template"
"net/http"
2019-03-21 15:05:23 +00:00
"path/filepath"
"strings"
"time"
"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"
)
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})
}
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())
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)
}
2019-01-26 14:38:22 +00:00
w.Header().Set("ETag", fetag)
w.Header().Set("Cache-Control", "max-age=432000")
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",
Help: "The number of views per post or talk",
2019-03-21 15:05:23 +00:00
}, []string{"base"})
func (s *Site) listSeries(w http.ResponseWriter, r *http.Request) {
s.renderTemplatePage("series.html", s.Series).ServeHTTP(w, r)
}
func (s *Site) showSeries(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/blog/series/" {
http.Redirect(w, r, "/blog/series", http.StatusSeeOther)
return
}
series := filepath.Base(r.URL.Path)
var posts []blog.Post
for _, p := range s.Posts {
if p.Series == series {
posts = append(posts, p)
}
}
s.renderTemplatePage("serieslist.html", struct {
Name string
Posts []blog.Post
}{
Name: series,
Posts: posts,
}).ServeHTTP(w, r)
}
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)
postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc()
}
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
for _, pst := range s.Posts {
2018-12-10 16:14:00 +00:00
if pst.Link == cmp {
p = pst
2019-03-27 14:18:52 +00:00
found = true
}
}
2019-03-27 14:18:52 +00:00
if !found {
w.WriteHeader(http.StatusNotFound)
s.renderTemplatePage("error.html", "no such post found: "+r.RequestURI).ServeHTTP(w, r)
return
}
var tags string
if len(p.Tags) != 0 {
for _, t := range p.Tags {
tags = tags + " #" + strings.ReplaceAll(t, "-", "")
}
}
2019-03-27 14:31:57 +00:00
const dateFormat = `2006-01-02`
s.renderTemplatePage("blogpost.html", struct {
2019-09-12 22:56:43 +00:00
Title string
Link string
BodyHTML template.HTML
Date string
Series, SeriesTag string
Tags string
2019-03-27 14:31:57 +00:00
}{
2019-09-12 22:56:43 +00:00
Title: p.Title,
Link: p.Link,
BodyHTML: p.BodyHTML,
Date: p.Date.Format(dateFormat),
Series: p.Series,
SeriesTag: strings.ReplaceAll(p.Series, "-", ""),
Tags: tags,
2019-03-27 14:31:57 +00:00
}).ServeHTTP(w, r)
2019-03-21 15:05:23 +00:00
postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc()
}