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"
|
2019-09-12 22:49:03 +00:00
|
|
|
"strings"
|
2017-05-20 22:06:30 +00:00
|
|
|
"time"
|
|
|
|
|
2019-05-25 19:14:09 +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-09-12 22:49:03 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2019-10-23 17:16:18 +00:00
|
|
|
Date: internal.IOS13Detri(p.Date),
|
2019-05-22 01:47:09 +00:00
|
|
|
SlidesLink: p.SlidesLink,
|
|
|
|
})
|
|
|
|
|
|
|
|
if h == nil {
|
|
|
|
panic("how did we get here?")
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
2019-06-05 13:45:21 +00:00
|
|
|
postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc()
|
2019-05-22 01:47:09 +00:00
|
|
|
}
|
|
|
|
|
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-09-12 22:49:03 +00:00
|
|
|
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
|
|
|
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,
|
2019-10-23 17:16:18 +00:00
|
|
|
Date: internal.IOS13Detri(p.Date),
|
2019-09-12 22:56:43 +00:00
|
|
|
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()
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|