Merge pull request #18 from Xe/reorg

Reorg
This commit is contained in:
Cadey Ratio 2019-03-27 07:47:10 -07:00 committed by GitHub
commit dcfac4d41f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 278 additions and 184 deletions

View File

@ -16,5 +16,5 @@ COPY ./blog /site/blog
COPY ./css /site/css COPY ./css /site/css
COPY ./app /app COPY ./app /app
COPY ./app.json . 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 CMD ./site

View File

@ -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,19 +76,32 @@ 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
} }
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() postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc()
} }

View File

@ -6,15 +6,11 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath"
"sort"
"strings"
"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,108 +25,58 @@ 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"
} }
ctx := ln.WithF(opname.With(context.Background(), "main"), ln.F{
"port": port,
})
prometheus.Register(prommod.NewCollector("christine")) prometheus.Register(prommod.NewCollector("christine"))
s, err := Build() s, err := Build()
if err != nil { if err != nil {
ln.FatalErr(context.Background(), err, ln.Action("Build")) ln.FatalErr(ctx, err, ln.Action("Build"))
} }
ln.Log(context.Background(), ln.F{"action": "http_listening", "port": port}) mux := http.NewServeMux()
http.ListenAndServe(":"+port, s) mux.HandleFunc("/.within/health", func(w http.ResponseWriter, r *http.Request) {
} http.Error(w, "OK", http.StatusOK)
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) mux.Handle("/", s)
w.Header().Set("X-Request-Id", id) ln.Log(ctx, ln.Action("http_listening"))
r.Header.Set("X-Request-Id", id) ln.FatalErr(ctx, http.ListenAndServe(":"+port, mux))
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
jsonFeed *jsonfeed.Feed jsonFeed *jsonfeed.Feed
mux *http.ServeMux mux *http.ServeMux
sitemap []byte
xffmw *xff.XFF xffmw *xff.XFF
templates map[string]*template.Template
tlock sync.RWMutex
} }
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 +102,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
@ -189,57 +134,11 @@ func Build() (*Site, error) {
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 +148,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),
}) })
} }
@ -276,17 +174,14 @@ func Build() (*Site, error) {
s.renderTemplatePage("index.html", nil).ServeHTTP(w, r) 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("/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,34 +190,12 @@ 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)
}))) })))
return s, nil return s, nil
} }
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] }

View File

@ -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,24 @@ 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
View File

@ -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
View File

@ -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=

100
internal/blog/blog.go Normal file
View File

@ -0,0 +1,100 @@
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
DateString 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 := 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,
DateString: fm.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
}

View File

@ -0,0 +1,12 @@
package blog
import (
"testing"
)
func TestLoadPosts(t *testing.T) {
_, err := LoadPosts("../../blog")
if err != nil {
t.Fatal(err)
}
}

View File

@ -1,4 +1,4 @@
package main package internal
import ( import (
"crypto/md5" "crypto/md5"

View File

@ -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),
),
)
}

View File

@ -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)
})
}

View File

@ -10,7 +10,7 @@
<p> <p>
<ul> <ul>
{{ range . }} {{ range . }}
<li>{{ .Date }} - <a href="{{ .Link }}">{{ .Title }}</a></li> <li>{{ .DateString }} - <a href="{{ .Link }}">{{ .Title }}</a></li>
{{ end }} {{ end }}
</ul> </ul>
</p> </p>