From d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a Mon Sep 17 00:00:00 2001
From: Christine Dodrill
Date: Wed, 27 Mar 2019 07:18:52 -0700
Subject: [PATCH 1/3] reorg: phase 1
---
...-18.md => atom-as-vim-2014-11-18.markdown} | 0
...014-11-28.md => beego-2014-11-28.markdown} | 0
...d-men-and-an-elephant-2018-11-29.markdown} | 0
...8-19-2018.md => death-08-19-2018.markdown} | 0
...md => dependency-hell-2014-11-20.markdown} | 0
...notice-elemental-ircd-2019-02-11.markdown} | 0
...-2014-10-24.md => dev-2014-10-24.markdown} | 0
...07-24-2018.md => fear-07-24-2018.markdown} | 0
...-2018.md => gratitude-07-20-2018.markdown} | 0
...0-20.md => mpd-docker-2014-10-20.markdown} | 0
...dates-titles-salaries-2019-03-14.markdown} | 0
...17.md => old-articles-2019-01-17.markdown} | 0
...ive-webapp-conversion-2019-01-26.markdown} | 0
...voiding-the-interview-2017-04-16.markdown} | 0
...0.md => when-then-zen-2019-01-20.markdown} | 0
cmd/site/html.go | 10 +-
cmd/site/main.go | 171 +++---------------
cmd/site/rss.go | 47 ++++-
go.mod | 2 -
go.sum | 4 -
internal/blog/blog.go | 98 ++++++++++
internal/blog/blog_test.go | 12 ++
{cmd/site => internal}/hash.go | 2 +-
internal/middleware/metrics.go | 43 +++++
internal/middleware/requestid.go | 31 ++++
25 files changed, 250 insertions(+), 170 deletions(-)
rename blog/{atom-as-vim-2014-11-18.md => atom-as-vim-2014-11-18.markdown} (100%)
rename blog/{beego-2014-11-28.md => beego-2014-11-28.markdown} (100%)
rename blog/{blind-men-and-an-elephant-2018-11-29.md => blind-men-and-an-elephant-2018-11-29.markdown} (100%)
rename blog/{death-08-19-2018.md => death-08-19-2018.markdown} (100%)
rename blog/{dependency-hell-2014-11-20.md => dependency-hell-2014-11-20.markdown} (100%)
rename blog/{deprecation-notice-elemental-ircd-2019-02-11.md => deprecation-notice-elemental-ircd-2019-02-11.markdown} (100%)
rename blog/{dev-2014-10-24.md => dev-2014-10-24.markdown} (100%)
rename blog/{fear-07-24-2018.md => fear-07-24-2018.markdown} (100%)
rename blog/{gratitude-07-20-2018.md => gratitude-07-20-2018.markdown} (100%)
rename blog/{mpd-docker-2014-10-20.md => mpd-docker-2014-10-20.markdown} (100%)
rename blog/{my-career-in-dates-titles-salaries-2019-03-14.md => my-career-in-dates-titles-salaries-2019-03-14.markdown} (100%)
rename blog/{old-articles-2019-01-17.md => old-articles-2019-01-17.markdown} (100%)
rename blog/{progressive-webapp-conversion-2019-01-26.md => progressive-webapp-conversion-2019-01-26.markdown} (100%)
rename blog/{voiding-the-interview-2017-04-16.md => voiding-the-interview-2017-04-16.markdown} (100%)
rename blog/{when-then-zen-2019-01-20.md => when-then-zen-2019-01-20.markdown} (100%)
create mode 100644 internal/blog/blog.go
create mode 100644 internal/blog/blog_test.go
rename {cmd/site => internal}/hash.go (95%)
create mode 100644 internal/middleware/metrics.go
create mode 100644 internal/middleware/requestid.go
diff --git a/blog/atom-as-vim-2014-11-18.md b/blog/atom-as-vim-2014-11-18.markdown
similarity index 100%
rename from blog/atom-as-vim-2014-11-18.md
rename to blog/atom-as-vim-2014-11-18.markdown
diff --git a/blog/beego-2014-11-28.md b/blog/beego-2014-11-28.markdown
similarity index 100%
rename from blog/beego-2014-11-28.md
rename to blog/beego-2014-11-28.markdown
diff --git a/blog/blind-men-and-an-elephant-2018-11-29.md b/blog/blind-men-and-an-elephant-2018-11-29.markdown
similarity index 100%
rename from blog/blind-men-and-an-elephant-2018-11-29.md
rename to blog/blind-men-and-an-elephant-2018-11-29.markdown
diff --git a/blog/death-08-19-2018.md b/blog/death-08-19-2018.markdown
similarity index 100%
rename from blog/death-08-19-2018.md
rename to blog/death-08-19-2018.markdown
diff --git a/blog/dependency-hell-2014-11-20.md b/blog/dependency-hell-2014-11-20.markdown
similarity index 100%
rename from blog/dependency-hell-2014-11-20.md
rename to blog/dependency-hell-2014-11-20.markdown
diff --git a/blog/deprecation-notice-elemental-ircd-2019-02-11.md b/blog/deprecation-notice-elemental-ircd-2019-02-11.markdown
similarity index 100%
rename from blog/deprecation-notice-elemental-ircd-2019-02-11.md
rename to blog/deprecation-notice-elemental-ircd-2019-02-11.markdown
diff --git a/blog/dev-2014-10-24.md b/blog/dev-2014-10-24.markdown
similarity index 100%
rename from blog/dev-2014-10-24.md
rename to blog/dev-2014-10-24.markdown
diff --git a/blog/fear-07-24-2018.md b/blog/fear-07-24-2018.markdown
similarity index 100%
rename from blog/fear-07-24-2018.md
rename to blog/fear-07-24-2018.markdown
diff --git a/blog/gratitude-07-20-2018.md b/blog/gratitude-07-20-2018.markdown
similarity index 100%
rename from blog/gratitude-07-20-2018.md
rename to blog/gratitude-07-20-2018.markdown
diff --git a/blog/mpd-docker-2014-10-20.md b/blog/mpd-docker-2014-10-20.markdown
similarity index 100%
rename from blog/mpd-docker-2014-10-20.md
rename to blog/mpd-docker-2014-10-20.markdown
diff --git a/blog/my-career-in-dates-titles-salaries-2019-03-14.md b/blog/my-career-in-dates-titles-salaries-2019-03-14.markdown
similarity index 100%
rename from blog/my-career-in-dates-titles-salaries-2019-03-14.md
rename to blog/my-career-in-dates-titles-salaries-2019-03-14.markdown
diff --git a/blog/old-articles-2019-01-17.md b/blog/old-articles-2019-01-17.markdown
similarity index 100%
rename from blog/old-articles-2019-01-17.md
rename to blog/old-articles-2019-01-17.markdown
diff --git a/blog/progressive-webapp-conversion-2019-01-26.md b/blog/progressive-webapp-conversion-2019-01-26.markdown
similarity index 100%
rename from blog/progressive-webapp-conversion-2019-01-26.md
rename to blog/progressive-webapp-conversion-2019-01-26.markdown
diff --git a/blog/voiding-the-interview-2017-04-16.md b/blog/voiding-the-interview-2017-04-16.markdown
similarity index 100%
rename from blog/voiding-the-interview-2017-04-16.md
rename to blog/voiding-the-interview-2017-04-16.markdown
diff --git a/blog/when-then-zen-2019-01-20.md b/blog/when-then-zen-2019-01-20.markdown
similarity index 100%
rename from blog/when-then-zen-2019-01-20.md
rename to blog/when-then-zen-2019-01-20.markdown
diff --git a/cmd/site/html.go b/cmd/site/html.go
index a645551..ba2b4fa 100644
--- a/cmd/site/html.go
+++ b/cmd/site/html.go
@@ -8,6 +8,8 @@ import (
"path/filepath"
"time"
+ "christine.website/internal"
+ "christine.website/internal/blog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"within.website/ln"
@@ -30,7 +32,7 @@ func logTemplateTime(ctx context.Context, name string, f ln.F, from time.Time) {
func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := opname.With(r.Context(), "renderTemplatePage")
- fetag := "W/" + Hash(templateFname, etag) + "-1"
+ fetag := "W/" + internal.Hash(templateFname, etag) + "-1"
f := ln.F{"etag": fetag, "if_none_match": r.Header.Get("If-None-Match")}
@@ -74,14 +76,16 @@ func (s *Site) showPost(w http.ResponseWriter, r *http.Request) {
}
cmp := r.URL.Path[1:]
- var p *Post
+ var p blog.Post
+ var found bool
for _, pst := range s.Posts {
if pst.Link == cmp {
p = pst
+ found = true
}
}
- if p == nil {
+ if !found {
w.WriteHeader(http.StatusNotFound)
s.renderTemplatePage("error.html", "no such post found: "+r.RequestURI).ServeHTTP(w, r)
return
diff --git a/cmd/site/main.go b/cmd/site/main.go
index ec9648b..b6a809f 100644
--- a/cmd/site/main.go
+++ b/cmd/site/main.go
@@ -6,15 +6,12 @@ import (
"io/ioutil"
"net/http"
"os"
- "path/filepath"
- "sort"
- "strings"
"sync"
"time"
- "christine.website/internal/front"
+ "christine.website/internal/blog"
"christine.website/internal/jsonfeed"
- "github.com/celrenheit/sandflake"
+ "christine.website/internal/middleware"
"github.com/gorilla/feeds"
"github.com/povilasv/prommod"
"github.com/prometheus/client_golang/prometheus"
@@ -29,39 +26,6 @@ import (
var port = os.Getenv("PORT")
-var (
- requestCounter = prometheus.NewCounterVec(
- prometheus.CounterOpts{
- Name: "handler_requests_total",
- Help: "Total number of request/responses by HTTP status code.",
- }, []string{"handler", "code"})
-
- requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
- Name: "handler_request_duration",
- Help: "Handler request duration.",
- }, []string{"handler", "method"})
-
- requestInFlight = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: "handler_requests_in_flight",
- Help: "Current number of requests being served.",
- }, []string{"handler"})
-)
-
-func init() {
- prometheus.Register(requestCounter)
- prometheus.Register(requestDuration)
- prometheus.Register(requestInFlight)
-}
-
-func middlewareMetrics(family string, next http.Handler) http.Handler {
- return promhttp.InstrumentHandlerDuration(
- requestDuration.MustCurryWith(prometheus.Labels{"handler": family}),
- promhttp.InstrumentHandlerCounter(requestCounter.MustCurryWith(prometheus.Labels{"handler": family}),
- promhttp.InstrumentHandlerInFlight(requestInFlight.With(prometheus.Labels{"handler": family}), next),
- ),
- )
-}
-
func main() {
if port == "" {
port = "29384"
@@ -78,30 +42,9 @@ func main() {
http.ListenAndServe(":"+port, s)
}
-func requestIDMiddleware(next http.Handler) http.Handler {
- var g sandflake.Generator
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- id := g.Next().String()
-
- if rid := r.Header.Get("X-Request-Id"); rid != "" {
- id = rid + "," + id
- }
-
- ctx := ln.WithF(r.Context(), ln.F{
- "request_id": id,
- })
- r = r.WithContext(ctx)
-
- w.Header().Set("X-Request-Id", id)
- r.Header.Set("X-Request-Id", id)
-
- next.ServeHTTP(w, r)
- })
-}
-
// Site is the parent object for https://christine.website's backend.
type Site struct {
- Posts Posts
+ Posts blog.Posts
Resume template.HTML
rssFeed *feeds.Feed
@@ -109,7 +52,7 @@ type Site struct {
mux *http.ServeMux
sitemap []byte
- xffmw *xff.XFF
+ xffmw *xff.XFF
templates map[string]*template.Template
tlock sync.RWMutex
@@ -117,20 +60,18 @@ type Site struct {
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := opname.With(r.Context(), "site.ServeHTTP")
+ ctx = ln.WithF(ctx, ln.F{
+ "user_agent": r.Header.Get("User-Agent"),
+ })
r = r.WithContext(ctx)
- requestIDMiddleware(s.xffmw.Handler(ex.HTTPLog(s.mux))).ServeHTTP(w, r)
+ middleware.RequestID(s.xffmw.Handler(ex.HTTPLog(s.mux))).ServeHTTP(w, r)
}
var arbDate = time.Date(2019, time.March, 21, 18, 0, 0, 0, time.UTC)
// Build creates a new Site instance or fails.
func Build() (*Site, error) {
- type postFM struct {
- Title string
- Date string
- }
-
smi := sitemap.New()
smi.Add(&sitemap.URL{
Loc: "https://christine.website/resume",
@@ -156,7 +97,6 @@ func Build() (*Site, error) {
ChangeFreq: sitemap.Weekly,
})
-
xffmw, err := xff.Default()
if err != nil {
return nil, err
@@ -185,61 +125,15 @@ func Build() (*Site, error) {
Avatar: icon,
},
},
- mux: http.NewServeMux(),
+ mux: http.NewServeMux(),
xffmw: xffmw,
}
- err = filepath.Walk("./blog/", func(path string, info os.FileInfo, err error) error {
- if err != nil {
- return err
- }
-
- if info.IsDir() {
- return nil
- }
-
- fin, err := os.Open(path)
- if err != nil {
- return err
- }
- defer fin.Close()
-
- content, err := ioutil.ReadAll(fin)
- if err != nil {
- return err
- }
-
- var fm postFM
- remaining, err := front.Unmarshal(content, &fm)
- if err != nil {
- return err
- }
-
- output := blackfriday.Run(remaining)
-
- p := &Post{
- Title: fm.Title,
- Date: fm.Date,
- Link: strings.Split(path, ".")[0],
- Body: string(remaining),
- BodyHTML: template.HTML(output),
- }
-
- s.Posts = append(s.Posts, p)
- itime, _ := time.Parse("2006-01-02", p.Date)
- smi.Add(&sitemap.URL{
- Loc: "https://christine.website/" + p.Link,
- LastMod: &itime,
- ChangeFreq: sitemap.Monthly,
- })
-
- return nil
- })
+ posts, err := blog.LoadPosts("./blog/")
if err != nil {
return nil, err
}
-
- sort.Sort(sort.Reverse(s.Posts))
+ s.Posts = posts
resumeData, err := ioutil.ReadFile("./static/resume/resume.md")
if err != nil {
@@ -249,19 +143,18 @@ func Build() (*Site, error) {
s.Resume = template.HTML(blackfriday.Run(resumeData))
for _, item := range s.Posts {
- itime, _ := time.Parse("2006-01-02", item.Date)
s.rssFeed.Items = append(s.rssFeed.Items, &feeds.Item{
Title: item.Title,
Link: &feeds.Link{Href: "https://christine.website/" + item.Link},
Description: item.Summary,
- Created: itime,
+ Created: item.Date,
})
s.jsonFeed.Items = append(s.jsonFeed.Items, jsonfeed.Item{
ID: "https://christine.website/" + item.Link,
URL: "https://christine.website/" + item.Link,
Title: item.Title,
- DatePublished: itime,
+ DatePublished: item.Date,
ContentHTML: string(item.BodyHTML),
})
}
@@ -280,13 +173,13 @@ func Build() (*Site, error) {
http.Error(w, "OK", http.StatusOK)
})
s.mux.Handle("/metrics", promhttp.Handler())
- s.mux.Handle("/resume", middlewareMetrics("resume", s.renderTemplatePage("resume.html", s.Resume)))
- s.mux.Handle("/blog", middlewareMetrics("blog", s.renderTemplatePage("blogindex.html", s.Posts)))
- s.mux.Handle("/contact", middlewareMetrics("contact", s.renderTemplatePage("contact.html", nil)))
- s.mux.Handle("/blog.rss", middlewareMetrics("blog.rss", http.HandlerFunc(s.createFeed)))
- s.mux.Handle("/blog.atom", middlewareMetrics("blog.atom", http.HandlerFunc(s.createAtom)))
- s.mux.Handle("/blog.json", middlewareMetrics("blog.json", http.HandlerFunc(s.createJsonFeed)))
- s.mux.Handle("/blog/", middlewareMetrics("blogpost", http.HandlerFunc(s.showPost)))
+ s.mux.Handle("/resume", middleware.Metrics("resume", s.renderTemplatePage("resume.html", s.Resume)))
+ s.mux.Handle("/blog", middleware.Metrics("blog", s.renderTemplatePage("blogindex.html", s.Posts)))
+ s.mux.Handle("/contact", middleware.Metrics("contact", s.renderTemplatePage("contact.html", nil)))
+ s.mux.Handle("/blog.rss", middleware.Metrics("blog.rss", http.HandlerFunc(s.createFeed)))
+ s.mux.Handle("/blog.atom", middleware.Metrics("blog.atom", http.HandlerFunc(s.createAtom)))
+ s.mux.Handle("/blog.json", middleware.Metrics("blog.json", http.HandlerFunc(s.createJSONFeed)))
+ s.mux.Handle("/blog/", middleware.Metrics("blogpost", http.HandlerFunc(s.showPost)))
s.mux.Handle("/css/", http.FileServer(http.Dir(".")))
s.mux.Handle("/static/", http.FileServer(http.Dir(".")))
s.mux.HandleFunc("/sw.js", func(w http.ResponseWriter, r *http.Request) {
@@ -295,7 +188,7 @@ func Build() (*Site, error) {
s.mux.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/robots.txt")
})
- s.mux.Handle("/sitemap.xml", middlewareMetrics("sitemap", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ s.mux.Handle("/sitemap.xml", middleware.Metrics("sitemap", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
smi.WriteTo(w)
})))
@@ -304,25 +197,3 @@ func Build() (*Site, error) {
}
const icon = "https://christine.website/static/img/avatar.png"
-
-// Post is a single blogpost.
-type Post struct {
- Title string `json:"title"`
- Link string `json:"link"`
- Summary string `json:"summary,omitifempty"`
- Body string `json:"-"`
- BodyHTML template.HTML `json:"body"`
- Date string `json:"date"`
-}
-
-// Posts implements sort.Interface for a slice of Post objects.
-type Posts []*Post
-
-func (p Posts) Len() int { return len(p) }
-func (p Posts) Less(i, j int) bool {
- iDate, _ := time.Parse("2006-01-02", p[i].Date)
- jDate, _ := time.Parse("2006-01-02", p[j].Date)
-
- return iDate.Unix() < jDate.Unix()
-}
-func (p Posts) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
diff --git a/cmd/site/rss.go b/cmd/site/rss.go
index 2f319b2..5b2c73a 100644
--- a/cmd/site/rss.go
+++ b/cmd/site/rss.go
@@ -5,19 +5,29 @@ import (
"net/http"
"time"
+ "christine.website/internal"
"within.website/ln"
+ "within.website/ln/opname"
)
var bootTime = time.Now()
-var etag = Hash(bootTime.String(), IncrediblySecureSalt)
+var etag = internal.Hash(bootTime.String(), IncrediblySecureSalt)
// IncrediblySecureSalt *******
const IncrediblySecureSalt = "hunter2"
func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "application/rss+xml")
- w.Header().Set("ETag", "W/"+Hash(bootTime.String(), IncrediblySecureSalt))
+ ctx := opname.With(r.Context(), "rss-feed")
+ fetag := "W/" + internal.Hash(bootTime.String(), IncrediblySecureSalt)
+ w.Header().Set("ETag", fetag)
+ if r.Header.Get("If-None-Match") == fetag {
+ http.Error(w, "Cached data OK", http.StatusNotModified)
+ ln.Log(ctx, ln.Info("cache hit"))
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/rss+xml")
err := s.rssFeed.WriteRss(w)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
@@ -31,13 +41,21 @@ func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) {
}
func (s *Site) createAtom(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "application/atom+xml")
- w.Header().Set("ETag", "W/"+Hash(bootTime.String(), IncrediblySecureSalt))
+ ctx := opname.With(r.Context(), "atom-feed")
+ fetag := "W/" + internal.Hash(bootTime.String(), IncrediblySecureSalt)
+ w.Header().Set("ETag", fetag)
+ if r.Header.Get("If-None-Match") == fetag {
+ http.Error(w, "Cached data OK", http.StatusNotModified)
+ ln.Log(ctx, ln.Info("cache hit"))
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/atom+xml")
err := s.rssFeed.WriteAtom(w)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
- ln.Error(r.Context(), err, ln.F{
+ ln.Error(ctx, err, ln.F{
"remote_addr": r.RemoteAddr,
"action": "generating_atom",
"uri": r.RequestURI,
@@ -46,16 +64,25 @@ func (s *Site) createAtom(w http.ResponseWriter, r *http.Request) {
}
}
-func (s *Site) createJsonFeed(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "application/json")
- w.Header().Set("ETag", Hash(bootTime.String(), IncrediblySecureSalt))
+func (s *Site) createJSONFeed(w http.ResponseWriter, r *http.Request) {
+ ctx := opname.With(r.Context(), "atom-feed")
+ fetag := "W/" + internal.Hash(bootTime.String(), IncrediblySecureSalt)
+ w.Header().Set("ETag", fetag)
+ if r.Header.Get("If-None-Match") == fetag {
+ http.Error(w, "Cached data OK", http.StatusNotModified)
+ ln.Log(ctx, ln.Info("cache hit"))
+ return
+ }
+
+
+ w.Header().Set("Content-Type", "application/json")
e := json.NewEncoder(w)
e.SetIndent("", "\t")
err := e.Encode(s.jsonFeed)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
- ln.Error(r.Context(), err, ln.F{
+ ln.Error(ctx, err, ln.F{
"remote_addr": r.RemoteAddr,
"action": "generating_jsonfeed",
"uri": r.RequestURI,
diff --git a/go.mod b/go.mod
index 387df9d..183c97c 100644
--- a/go.mod
+++ b/go.mod
@@ -16,9 +16,7 @@ require (
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/snabb/sitemap v1.0.0
github.com/stretchr/testify v1.3.0
- github.com/tj/front v0.0.0-20170212063142-739be213b0a1
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
- gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 // indirect
gopkg.in/yaml.v2 v2.2.1
within.website/ln v0.5.2
)
diff --git a/go.sum b/go.sum
index de5831d..2c6d835 100644
--- a/go.sum
+++ b/go.sum
@@ -69,8 +69,6 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
-github.com/tj/front v0.0.0-20170212063142-739be213b0a1 h1:lA+aPRvltlx2fwv/BnxyYSDQo3pIeqzHgMO5GvK0T9E=
-github.com/tj/front v0.0.0-20170212063142-739be213b0a1/go.mod h1:deJrtusCTptAW4EUn5vBLpl3dhNqPqUwEjWJz5UNxpQ=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -84,8 +82,6 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 h1:POO/ycCATvegFmVuPpQzZFJ+pGZeX22Ufu6fibxDVjU=
-gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
within.website/ln v0.5.2 h1:4pTM2wzpLjeZAputLf2U29HD79vcmdoEI/VPm2QEYgs=
diff --git a/internal/blog/blog.go b/internal/blog/blog.go
new file mode 100644
index 0000000..54d5617
--- /dev/null
+++ b/internal/blog/blog.go
@@ -0,0 +1,98 @@
+package blog
+
+import (
+ "html/template"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "sort"
+ "strings"
+ "time"
+
+ "christine.website/internal/front"
+ "github.com/russross/blackfriday"
+)
+
+// Post is a single blogpost.
+type Post struct {
+ Title string `json:"title"`
+ Link string `json:"link"`
+ Summary string `json:"summary,omitifempty"`
+ Body string `json:"-"`
+ BodyHTML template.HTML `json:"body"`
+ Date time.Time `json:"date"`
+}
+
+// Posts implements sort.Interface for a slice of Post objects.
+type Posts []Post
+
+func (p Posts) Len() int { return len(p) }
+func (p Posts) Less(i, j int) bool {
+ iDate := p[i].Date
+ jDate := p[j].Date
+
+ return iDate.Unix() < jDate.Unix()
+}
+func (p Posts) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+
+// LoadPosts loads posts for a given directory.
+func LoadPosts(path string) (Posts, error) {
+ type postFM struct {
+ Title string
+ Date string
+ }
+ var result Posts
+
+ err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+
+ if info.IsDir() {
+ return nil
+ }
+
+ fin, err := os.Open(path)
+ if err != nil {
+ return err
+ }
+ defer fin.Close()
+
+ content, err := ioutil.ReadAll(fin)
+ if err != nil {
+ return err
+ }
+
+ var fm postFM
+ remaining, err := front.Unmarshal(content, &fm)
+ if err != nil {
+ return err
+ }
+
+ output := blackfriday.Run(remaining)
+
+ const timeFormat = `2006-01-02`
+ date, err := time.Parse(timeFormat, fm.Date)
+ if err != nil {
+ return err
+ }
+
+ p := Post{
+ Title: fm.Title,
+ Date: date,
+ Link: strings.Split(path, ".")[0],
+ Body: string(remaining),
+ BodyHTML: template.HTML(output),
+ }
+ result = append(result, p)
+
+ return nil
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ sort.Sort(sort.Reverse(result))
+
+ return result, nil
+}
diff --git a/internal/blog/blog_test.go b/internal/blog/blog_test.go
new file mode 100644
index 0000000..b073880
--- /dev/null
+++ b/internal/blog/blog_test.go
@@ -0,0 +1,12 @@
+package blog
+
+import (
+ "testing"
+)
+
+func TestLoadPosts(t *testing.T) {
+ _, err := LoadPosts("../../blog")
+ if err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/cmd/site/hash.go b/internal/hash.go
similarity index 95%
rename from cmd/site/hash.go
rename to internal/hash.go
index ed6112c..ee333da 100644
--- a/cmd/site/hash.go
+++ b/internal/hash.go
@@ -1,4 +1,4 @@
-package main
+package internal
import (
"crypto/md5"
diff --git a/internal/middleware/metrics.go b/internal/middleware/metrics.go
new file mode 100644
index 0000000..df4816d
--- /dev/null
+++ b/internal/middleware/metrics.go
@@ -0,0 +1,43 @@
+package middleware
+
+import (
+ "net/http"
+
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promhttp"
+)
+
+var (
+ requestCounter = prometheus.NewCounterVec(
+ prometheus.CounterOpts{
+ Name: "handler_requests_total",
+ Help: "Total number of request/responses by HTTP status code.",
+ }, []string{"handler", "code"})
+
+ requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
+ Name: "handler_request_duration",
+ Help: "Handler request duration.",
+ }, []string{"handler", "method"})
+
+ requestInFlight = prometheus.NewGaugeVec(prometheus.GaugeOpts{
+ Name: "handler_requests_in_flight",
+ Help: "Current number of requests being served.",
+ }, []string{"handler"})
+)
+
+func init() {
+ prometheus.Register(requestCounter)
+ prometheus.Register(requestDuration)
+ prometheus.Register(requestInFlight)
+}
+
+// Metrics captures request duration, request count and in-flight request count
+// metrics for HTTP handlers. The family field is used to discriminate handlers.
+func Metrics(family string, next http.Handler) http.Handler {
+ return promhttp.InstrumentHandlerDuration(
+ requestDuration.MustCurryWith(prometheus.Labels{"handler": family}),
+ promhttp.InstrumentHandlerCounter(requestCounter.MustCurryWith(prometheus.Labels{"handler": family}),
+ promhttp.InstrumentHandlerInFlight(requestInFlight.With(prometheus.Labels{"handler": family}), next),
+ ),
+ )
+}
diff --git a/internal/middleware/requestid.go b/internal/middleware/requestid.go
new file mode 100644
index 0000000..6914137
--- /dev/null
+++ b/internal/middleware/requestid.go
@@ -0,0 +1,31 @@
+package middleware
+
+import (
+ "net/http"
+
+ "github.com/celrenheit/sandflake"
+ "within.website/ln"
+)
+
+// RequestID appends a unique (sandflake) request ID to each request's
+// X-Request-Id header field, much like Heroku's router does.
+func RequestID(next http.Handler) http.Handler {
+ var g sandflake.Generator
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ id := g.Next().String()
+
+ if rid := r.Header.Get("X-Request-Id"); rid != "" {
+ id = rid + "," + id
+ }
+
+ ctx := ln.WithF(r.Context(), ln.F{
+ "request_id": id,
+ })
+ r = r.WithContext(ctx)
+
+ w.Header().Set("X-Request-Id", id)
+ r.Header.Set("X-Request-Id", id)
+
+ next.ServeHTTP(w, r)
+ })
+}
From 7e6a1cbd5841b094a7e168f8500075443f31e8e0 Mon Sep 17 00:00:00 2001
From: Christine Dodrill
Date: Wed, 27 Mar 2019 07:31:57 -0700
Subject: [PATCH 2/3] make logging cleaner, etag more
---
Dockerfile | 2 +-
cmd/site/html.go | 13 ++++++++++++-
cmd/site/main.go | 15 +++++++--------
cmd/site/rss.go | 1 -
internal/blog/blog.go | 24 +++++++++++++-----------
templates/blogindex.html | 2 +-
6 files changed, 34 insertions(+), 23 deletions(-)
diff --git a/Dockerfile b/Dockerfile
index 4391948..653a92c 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -16,5 +16,5 @@ COPY ./blog /site/blog
COPY ./css /site/css
COPY ./app /app
COPY ./app.json .
-HEALTHCHECK CMD wget --spider http://127.0.0.1:5000 || exit 1
+HEALTHCHECK CMD wget --spider http://127.0.0.1:5000/.within/health || exit 1
CMD ./site
diff --git a/cmd/site/html.go b/cmd/site/html.go
index ba2b4fa..9f208af 100644
--- a/cmd/site/html.go
+++ b/cmd/site/html.go
@@ -91,6 +91,17 @@ func (s *Site) showPost(w http.ResponseWriter, r *http.Request) {
return
}
- s.renderTemplatePage("blogpost.html", p).ServeHTTP(w, r)
+ 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)
postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc()
}
diff --git a/cmd/site/main.go b/cmd/site/main.go
index b6a809f..3719095 100644
--- a/cmd/site/main.go
+++ b/cmd/site/main.go
@@ -6,7 +6,6 @@ import (
"io/ioutil"
"net/http"
"os"
- "sync"
"time"
"christine.website/internal/blog"
@@ -38,8 +37,14 @@ func main() {
ln.FatalErr(context.Background(), err, ln.Action("Build"))
}
+ mux := http.NewServeMux()
+ mux.HandleFunc("/.within/health", func(w http.ResponseWriter, r *http.Request) {
+ http.Error(w, "OK", http.StatusOK)
+ })
+ mux.Handle("/", s)
+
ln.Log(context.Background(), ln.F{"action": "http_listening", "port": port})
- http.ListenAndServe(":"+port, s)
+ http.ListenAndServe(":"+port, mux)
}
// Site is the parent object for https://christine.website's backend.
@@ -53,9 +58,6 @@ type Site struct {
mux *http.ServeMux
sitemap []byte
xffmw *xff.XFF
-
- templates map[string]*template.Template
- tlock sync.RWMutex
}
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -169,9 +171,6 @@ func Build() (*Site, error) {
s.renderTemplatePage("index.html", nil).ServeHTTP(w, r)
})
- s.mux.HandleFunc("/.within/health", func(w http.ResponseWriter, r *http.Request) {
- http.Error(w, "OK", http.StatusOK)
- })
s.mux.Handle("/metrics", promhttp.Handler())
s.mux.Handle("/resume", middleware.Metrics("resume", s.renderTemplatePage("resume.html", s.Resume)))
s.mux.Handle("/blog", middleware.Metrics("blog", s.renderTemplatePage("blogindex.html", s.Posts)))
diff --git a/cmd/site/rss.go b/cmd/site/rss.go
index 5b2c73a..e03e07f 100644
--- a/cmd/site/rss.go
+++ b/cmd/site/rss.go
@@ -75,7 +75,6 @@ func (s *Site) createJSONFeed(w http.ResponseWriter, r *http.Request) {
return
}
-
w.Header().Set("Content-Type", "application/json")
e := json.NewEncoder(w)
e.SetIndent("", "\t")
diff --git a/internal/blog/blog.go b/internal/blog/blog.go
index 54d5617..a73f27b 100644
--- a/internal/blog/blog.go
+++ b/internal/blog/blog.go
@@ -15,12 +15,13 @@ import (
// Post is a single blogpost.
type Post struct {
- Title string `json:"title"`
- Link string `json:"link"`
- Summary string `json:"summary,omitifempty"`
- Body string `json:"-"`
- BodyHTML template.HTML `json:"body"`
- Date time.Time `json:"date"`
+ Title string `json:"title"`
+ Link string `json:"link"`
+ Summary string `json:"summary,omitifempty"`
+ Body string `json:"-"`
+ BodyHTML template.HTML `json:"body"`
+ Date time.Time
+ DateString string `json:"date"`
}
// Posts implements sort.Interface for a slice of Post objects.
@@ -78,11 +79,12 @@ func LoadPosts(path string) (Posts, error) {
}
p := Post{
- Title: fm.Title,
- Date: date,
- Link: strings.Split(path, ".")[0],
- Body: string(remaining),
- BodyHTML: template.HTML(output),
+ Title: fm.Title,
+ Date: date,
+ DateString: fm.Date,
+ Link: strings.Split(path, ".")[0],
+ Body: string(remaining),
+ BodyHTML: template.HTML(output),
}
result = append(result, p)
diff --git a/templates/blogindex.html b/templates/blogindex.html
index 8e33197..5a1ac02 100644
--- a/templates/blogindex.html
+++ b/templates/blogindex.html
@@ -10,7 +10,7 @@
From bce017f56f2413872a3a904067d6d19d173a9904 Mon Sep 17 00:00:00 2001
From: Christine Dodrill
Date: Wed, 27 Mar 2019 07:40:42 -0700
Subject: [PATCH 3/3] shadow errors
---
cmd/site/main.go | 13 ++++++++-----
internal/middleware/metrics.go | 6 +++---
2 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/cmd/site/main.go b/cmd/site/main.go
index 3719095..e3987b3 100644
--- a/cmd/site/main.go
+++ b/cmd/site/main.go
@@ -30,11 +30,15 @@ func main() {
port = "29384"
}
+ ctx := ln.WithF(opname.With(context.Background(), "main"), ln.F{
+ "port": port,
+ })
+
prometheus.Register(prommod.NewCollector("christine"))
s, err := Build()
if err != nil {
- ln.FatalErr(context.Background(), err, ln.Action("Build"))
+ ln.FatalErr(ctx, err, ln.Action("Build"))
}
mux := http.NewServeMux()
@@ -43,8 +47,8 @@ func main() {
})
mux.Handle("/", s)
- ln.Log(context.Background(), ln.F{"action": "http_listening", "port": port})
- http.ListenAndServe(":"+port, mux)
+ ln.Log(ctx, ln.Action("http_listening"))
+ ln.FatalErr(ctx, http.ListenAndServe(":"+port, mux))
}
// Site is the parent object for https://christine.website's backend.
@@ -56,7 +60,6 @@ type Site struct {
jsonFeed *jsonfeed.Feed
mux *http.ServeMux
- sitemap []byte
xffmw *xff.XFF
}
@@ -189,7 +192,7 @@ func Build() (*Site, error) {
})
s.mux.Handle("/sitemap.xml", middleware.Metrics("sitemap", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
- smi.WriteTo(w)
+ _, _ = smi.WriteTo(w)
})))
return s, nil
diff --git a/internal/middleware/metrics.go b/internal/middleware/metrics.go
index df4816d..f9d7e0f 100644
--- a/internal/middleware/metrics.go
+++ b/internal/middleware/metrics.go
@@ -26,9 +26,9 @@ var (
)
func init() {
- prometheus.Register(requestCounter)
- prometheus.Register(requestDuration)
- prometheus.Register(requestInFlight)
+ _ = prometheus.Register(requestCounter)
+ _ = prometheus.Register(requestDuration)
+ _ = prometheus.Register(requestInFlight)
}
// Metrics captures request duration, request count and in-flight request count