diff options
| author | Christine Dodrill <me@christine.website> | 2019-03-27 07:18:52 -0700 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2019-03-27 07:18:52 -0700 |
| commit | d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a (patch) | |
| tree | 29ae006563834338e663452e6f810561596582ac /internal | |
| parent | 20c08e68d1218475d6c6f3ca8ae718e1f508c696 (diff) | |
| download | xesite-d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a.tar.xz xesite-d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a.zip | |
reorg: phase 1
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/blog/blog.go | 98 | ||||
| -rw-r--r-- | internal/blog/blog_test.go | 12 | ||||
| -rw-r--r-- | internal/hash.go | 14 | ||||
| -rw-r--r-- | internal/middleware/metrics.go | 43 | ||||
| -rw-r--r-- | internal/middleware/requestid.go | 31 |
5 files changed, 198 insertions, 0 deletions
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/internal/hash.go b/internal/hash.go new file mode 100644 index 0000000..ee333da --- /dev/null +++ b/internal/hash.go @@ -0,0 +1,14 @@ +package internal + +import ( + "crypto/md5" + "fmt" +) + +// Hash is a simple wrapper around the MD5 algorithm implementation in the +// Go standard library. It takes in data and a salt and returns the hashed +// representation. +func Hash(data string, salt string) string { + output := md5.Sum([]byte(data + salt)) + return fmt.Sprintf("%x", output) +} 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) + }) +} |
