reorg: phase 1
This commit is contained in:
parent
20c08e68d1
commit
d0ff1b2d04
|
@ -8,6 +8,8 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"christine.website/internal"
|
||||||
|
"christine.website/internal/blog"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
"within.website/ln"
|
"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 {
|
func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := opname.With(r.Context(), "renderTemplatePage")
|
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")}
|
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:]
|
cmp := r.URL.Path[1:]
|
||||||
var p *Post
|
var p blog.Post
|
||||||
|
var found bool
|
||||||
for _, pst := range s.Posts {
|
for _, pst := range s.Posts {
|
||||||
if pst.Link == cmp {
|
if pst.Link == cmp {
|
||||||
p = pst
|
p = pst
|
||||||
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if p == nil {
|
if !found {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
s.renderTemplatePage("error.html", "no such post found: "+r.RequestURI).ServeHTTP(w, r)
|
s.renderTemplatePage("error.html", "no such post found: "+r.RequestURI).ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
|
|
171
cmd/site/main.go
171
cmd/site/main.go
|
@ -6,15 +6,12 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"christine.website/internal/front"
|
"christine.website/internal/blog"
|
||||||
"christine.website/internal/jsonfeed"
|
"christine.website/internal/jsonfeed"
|
||||||
"github.com/celrenheit/sandflake"
|
"christine.website/internal/middleware"
|
||||||
"github.com/gorilla/feeds"
|
"github.com/gorilla/feeds"
|
||||||
"github.com/povilasv/prommod"
|
"github.com/povilasv/prommod"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
@ -29,39 +26,6 @@ import (
|
||||||
|
|
||||||
var port = os.Getenv("PORT")
|
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() {
|
func main() {
|
||||||
if port == "" {
|
if port == "" {
|
||||||
port = "29384"
|
port = "29384"
|
||||||
|
@ -78,30 +42,9 @@ func main() {
|
||||||
http.ListenAndServe(":"+port, s)
|
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.
|
// Site is the parent object for https://christine.website's backend.
|
||||||
type Site struct {
|
type Site struct {
|
||||||
Posts Posts
|
Posts blog.Posts
|
||||||
Resume template.HTML
|
Resume template.HTML
|
||||||
|
|
||||||
rssFeed *feeds.Feed
|
rssFeed *feeds.Feed
|
||||||
|
@ -109,7 +52,7 @@ type Site struct {
|
||||||
|
|
||||||
mux *http.ServeMux
|
mux *http.ServeMux
|
||||||
sitemap []byte
|
sitemap []byte
|
||||||
xffmw *xff.XFF
|
xffmw *xff.XFF
|
||||||
|
|
||||||
templates map[string]*template.Template
|
templates map[string]*template.Template
|
||||||
tlock sync.RWMutex
|
tlock sync.RWMutex
|
||||||
|
@ -117,20 +60,18 @@ type Site struct {
|
||||||
|
|
||||||
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := opname.With(r.Context(), "site.ServeHTTP")
|
ctx := opname.With(r.Context(), "site.ServeHTTP")
|
||||||
|
ctx = ln.WithF(ctx, ln.F{
|
||||||
|
"user_agent": r.Header.Get("User-Agent"),
|
||||||
|
})
|
||||||
r = r.WithContext(ctx)
|
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)
|
var arbDate = time.Date(2019, time.March, 21, 18, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
// Build creates a new Site instance or fails.
|
// Build creates a new Site instance or fails.
|
||||||
func Build() (*Site, error) {
|
func Build() (*Site, error) {
|
||||||
type postFM struct {
|
|
||||||
Title string
|
|
||||||
Date string
|
|
||||||
}
|
|
||||||
|
|
||||||
smi := sitemap.New()
|
smi := sitemap.New()
|
||||||
smi.Add(&sitemap.URL{
|
smi.Add(&sitemap.URL{
|
||||||
Loc: "https://christine.website/resume",
|
Loc: "https://christine.website/resume",
|
||||||
|
@ -156,7 +97,6 @@ func Build() (*Site, error) {
|
||||||
ChangeFreq: sitemap.Weekly,
|
ChangeFreq: sitemap.Weekly,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
xffmw, err := xff.Default()
|
xffmw, err := xff.Default()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -185,61 +125,15 @@ func Build() (*Site, error) {
|
||||||
Avatar: icon,
|
Avatar: icon,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mux: http.NewServeMux(),
|
mux: http.NewServeMux(),
|
||||||
xffmw: xffmw,
|
xffmw: xffmw,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = filepath.Walk("./blog/", func(path string, info os.FileInfo, err error) error {
|
posts, err := blog.LoadPosts("./blog/")
|
||||||
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
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
s.Posts = posts
|
||||||
sort.Sort(sort.Reverse(s.Posts))
|
|
||||||
|
|
||||||
resumeData, err := ioutil.ReadFile("./static/resume/resume.md")
|
resumeData, err := ioutil.ReadFile("./static/resume/resume.md")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -249,19 +143,18 @@ func Build() (*Site, error) {
|
||||||
s.Resume = template.HTML(blackfriday.Run(resumeData))
|
s.Resume = template.HTML(blackfriday.Run(resumeData))
|
||||||
|
|
||||||
for _, item := range s.Posts {
|
for _, item := range s.Posts {
|
||||||
itime, _ := time.Parse("2006-01-02", item.Date)
|
|
||||||
s.rssFeed.Items = append(s.rssFeed.Items, &feeds.Item{
|
s.rssFeed.Items = append(s.rssFeed.Items, &feeds.Item{
|
||||||
Title: item.Title,
|
Title: item.Title,
|
||||||
Link: &feeds.Link{Href: "https://christine.website/" + item.Link},
|
Link: &feeds.Link{Href: "https://christine.website/" + item.Link},
|
||||||
Description: item.Summary,
|
Description: item.Summary,
|
||||||
Created: itime,
|
Created: item.Date,
|
||||||
})
|
})
|
||||||
|
|
||||||
s.jsonFeed.Items = append(s.jsonFeed.Items, jsonfeed.Item{
|
s.jsonFeed.Items = append(s.jsonFeed.Items, jsonfeed.Item{
|
||||||
ID: "https://christine.website/" + item.Link,
|
ID: "https://christine.website/" + item.Link,
|
||||||
URL: "https://christine.website/" + item.Link,
|
URL: "https://christine.website/" + item.Link,
|
||||||
Title: item.Title,
|
Title: item.Title,
|
||||||
DatePublished: itime,
|
DatePublished: item.Date,
|
||||||
ContentHTML: string(item.BodyHTML),
|
ContentHTML: string(item.BodyHTML),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -280,13 +173,13 @@ func Build() (*Site, error) {
|
||||||
http.Error(w, "OK", http.StatusOK)
|
http.Error(w, "OK", http.StatusOK)
|
||||||
})
|
})
|
||||||
s.mux.Handle("/metrics", promhttp.Handler())
|
s.mux.Handle("/metrics", promhttp.Handler())
|
||||||
s.mux.Handle("/resume", middlewareMetrics("resume", s.renderTemplatePage("resume.html", s.Resume)))
|
s.mux.Handle("/resume", middleware.Metrics("resume", s.renderTemplatePage("resume.html", s.Resume)))
|
||||||
s.mux.Handle("/blog", middlewareMetrics("blog", s.renderTemplatePage("blogindex.html", s.Posts)))
|
s.mux.Handle("/blog", middleware.Metrics("blog", s.renderTemplatePage("blogindex.html", s.Posts)))
|
||||||
s.mux.Handle("/contact", middlewareMetrics("contact", s.renderTemplatePage("contact.html", nil)))
|
s.mux.Handle("/contact", middleware.Metrics("contact", s.renderTemplatePage("contact.html", nil)))
|
||||||
s.mux.Handle("/blog.rss", middlewareMetrics("blog.rss", http.HandlerFunc(s.createFeed)))
|
s.mux.Handle("/blog.rss", middleware.Metrics("blog.rss", http.HandlerFunc(s.createFeed)))
|
||||||
s.mux.Handle("/blog.atom", middlewareMetrics("blog.atom", http.HandlerFunc(s.createAtom)))
|
s.mux.Handle("/blog.atom", middleware.Metrics("blog.atom", http.HandlerFunc(s.createAtom)))
|
||||||
s.mux.Handle("/blog.json", middlewareMetrics("blog.json", http.HandlerFunc(s.createJsonFeed)))
|
s.mux.Handle("/blog.json", middleware.Metrics("blog.json", http.HandlerFunc(s.createJSONFeed)))
|
||||||
s.mux.Handle("/blog/", middlewareMetrics("blogpost", http.HandlerFunc(s.showPost)))
|
s.mux.Handle("/blog/", middleware.Metrics("blogpost", http.HandlerFunc(s.showPost)))
|
||||||
s.mux.Handle("/css/", http.FileServer(http.Dir(".")))
|
s.mux.Handle("/css/", http.FileServer(http.Dir(".")))
|
||||||
s.mux.Handle("/static/", http.FileServer(http.Dir(".")))
|
s.mux.Handle("/static/", http.FileServer(http.Dir(".")))
|
||||||
s.mux.HandleFunc("/sw.js", func(w http.ResponseWriter, r *http.Request) {
|
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) {
|
s.mux.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeFile(w, r, "./static/robots.txt")
|
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")
|
w.Header().Set("Content-Type", "application/xml")
|
||||||
smi.WriteTo(w)
|
smi.WriteTo(w)
|
||||||
})))
|
})))
|
||||||
|
@ -304,25 +197,3 @@ func Build() (*Site, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const icon = "https://christine.website/static/img/avatar.png"
|
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] }
|
|
||||||
|
|
|
@ -5,19 +5,29 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"christine.website/internal"
|
||||||
"within.website/ln"
|
"within.website/ln"
|
||||||
|
"within.website/ln/opname"
|
||||||
)
|
)
|
||||||
|
|
||||||
var bootTime = time.Now()
|
var bootTime = time.Now()
|
||||||
var etag = Hash(bootTime.String(), IncrediblySecureSalt)
|
var etag = internal.Hash(bootTime.String(), IncrediblySecureSalt)
|
||||||
|
|
||||||
// IncrediblySecureSalt *******
|
// IncrediblySecureSalt *******
|
||||||
const IncrediblySecureSalt = "hunter2"
|
const IncrediblySecureSalt = "hunter2"
|
||||||
|
|
||||||
func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) {
|
func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/rss+xml")
|
ctx := opname.With(r.Context(), "rss-feed")
|
||||||
w.Header().Set("ETag", "W/"+Hash(bootTime.String(), IncrediblySecureSalt))
|
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)
|
err := s.rssFeed.WriteRss(w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
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) {
|
func (s *Site) createAtom(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/atom+xml")
|
ctx := opname.With(r.Context(), "atom-feed")
|
||||||
w.Header().Set("ETag", "W/"+Hash(bootTime.String(), IncrediblySecureSalt))
|
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)
|
err := s.rssFeed.WriteAtom(w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
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,
|
"remote_addr": r.RemoteAddr,
|
||||||
"action": "generating_atom",
|
"action": "generating_atom",
|
||||||
"uri": r.RequestURI,
|
"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) {
|
func (s *Site) createJSONFeed(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
ctx := opname.With(r.Context(), "atom-feed")
|
||||||
w.Header().Set("ETag", Hash(bootTime.String(), IncrediblySecureSalt))
|
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 := json.NewEncoder(w)
|
||||||
e.SetIndent("", "\t")
|
e.SetIndent("", "\t")
|
||||||
err := e.Encode(s.jsonFeed)
|
err := e.Encode(s.jsonFeed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
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,
|
"remote_addr": r.RemoteAddr,
|
||||||
"action": "generating_jsonfeed",
|
"action": "generating_jsonfeed",
|
||||||
"uri": r.RequestURI,
|
"uri": r.RequestURI,
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -16,9 +16,7 @@ require (
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||||
github.com/snabb/sitemap v1.0.0
|
github.com/snabb/sitemap v1.0.0
|
||||||
github.com/stretchr/testify v1.3.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/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
|
gopkg.in/yaml.v2 v2.2.1
|
||||||
within.website/ln v0.5.2
|
within.website/ln v0.5.2
|
||||||
)
|
)
|
||||||
|
|
4
go.sum
4
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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
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/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-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/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 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 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
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 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
|
||||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
within.website/ln v0.5.2 h1:4pTM2wzpLjeZAputLf2U29HD79vcmdoEI/VPm2QEYgs=
|
within.website/ln v0.5.2 h1:4pTM2wzpLjeZAputLf2U29HD79vcmdoEI/VPm2QEYgs=
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package blog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoadPosts(t *testing.T) {
|
||||||
|
_, err := LoadPosts("../../blog")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
|
@ -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),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
|
@ -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)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue