2017-05-20 22:06:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-12-13 18:49:13 +00:00
|
|
|
"context"
|
2017-05-20 22:06:30 +00:00
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-03-21 14:55:32 +00:00
|
|
|
"christine.website/internal/front"
|
|
|
|
"christine.website/internal/jsonfeed"
|
2017-05-20 22:06:30 +00:00
|
|
|
"github.com/gorilla/feeds"
|
2019-03-21 14:31:49 +00:00
|
|
|
"github.com/povilasv/prommod"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2017-12-13 18:49:13 +00:00
|
|
|
blackfriday "github.com/russross/blackfriday"
|
2019-03-21 17:30:20 +00:00
|
|
|
"github.com/snabb/sitemap"
|
2019-01-26 19:47:16 +00:00
|
|
|
"within.website/ln"
|
2017-05-20 22:06:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var port = os.Getenv("PORT")
|
|
|
|
|
2019-03-21 14:31:49 +00:00
|
|
|
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),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-05-20 22:06:30 +00:00
|
|
|
func main() {
|
|
|
|
if port == "" {
|
|
|
|
port = "29384"
|
|
|
|
}
|
|
|
|
|
2019-03-21 14:31:49 +00:00
|
|
|
prometheus.Register(prommod.NewCollector("christine"))
|
|
|
|
|
2017-05-20 22:06:30 +00:00
|
|
|
s, err := Build()
|
|
|
|
if err != nil {
|
2017-12-13 18:49:13 +00:00
|
|
|
ln.FatalErr(context.Background(), err, ln.Action("Build"))
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 18:49:13 +00:00
|
|
|
ln.Log(context.Background(), ln.F{"action": "http_listening", "port": port})
|
2017-05-20 22:06:30 +00:00
|
|
|
http.ListenAndServe(":"+port, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Site is the parent object for https://christine.website's backend.
|
|
|
|
type Site struct {
|
|
|
|
Posts Posts
|
|
|
|
Resume template.HTML
|
|
|
|
|
|
|
|
rssFeed *feeds.Feed
|
|
|
|
jsonFeed *jsonfeed.Feed
|
|
|
|
|
2019-03-21 17:30:20 +00:00
|
|
|
mux *http.ServeMux
|
|
|
|
sitemap []byte
|
2017-05-20 22:06:30 +00:00
|
|
|
|
|
|
|
templates map[string]*template.Template
|
|
|
|
tlock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-12-13 18:49:13 +00:00
|
|
|
ln.Log(r.Context(), ln.F{"action": "Site.ServeHTTP", "user_ip_address": r.RemoteAddr, "path": r.RequestURI})
|
2018-08-22 03:17:59 +00:00
|
|
|
|
2017-05-20 22:06:30 +00:00
|
|
|
s.mux.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2019-03-21 17:30:20 +00:00
|
|
|
var arbDate = time.Date(2019, time.March, 21, 18, 0, 0, 0, time.UTC)
|
|
|
|
|
2017-05-20 22:06:30 +00:00
|
|
|
// Build creates a new Site instance or fails.
|
|
|
|
func Build() (*Site, error) {
|
|
|
|
type postFM struct {
|
|
|
|
Title string
|
|
|
|
Date string
|
|
|
|
}
|
|
|
|
|
2019-03-21 17:30:20 +00:00
|
|
|
smi := sitemap.New()
|
|
|
|
smi.Add(&sitemap.URL{
|
|
|
|
Loc: "https://christine.website/resume",
|
|
|
|
LastMod: &arbDate,
|
|
|
|
ChangeFreq: sitemap.Monthly,
|
|
|
|
})
|
|
|
|
|
|
|
|
smi.Add(&sitemap.URL{
|
|
|
|
Loc: "https://christine.website/contact",
|
|
|
|
LastMod: &arbDate,
|
|
|
|
ChangeFreq: sitemap.Monthly,
|
|
|
|
})
|
|
|
|
|
|
|
|
smi.Add(&sitemap.URL{
|
|
|
|
Loc: "https://christine.website/",
|
|
|
|
LastMod: &arbDate,
|
|
|
|
ChangeFreq: sitemap.Monthly,
|
|
|
|
})
|
|
|
|
|
|
|
|
smi.Add(&sitemap.URL{
|
|
|
|
Loc: "https://christine.website/blog",
|
|
|
|
LastMod: &arbDate,
|
|
|
|
ChangeFreq: sitemap.Weekly,
|
|
|
|
})
|
|
|
|
|
2017-05-20 22:06:30 +00:00
|
|
|
s := &Site{
|
|
|
|
rssFeed: &feeds.Feed{
|
|
|
|
Title: "Christine Dodrill's Blog",
|
|
|
|
Link: &feeds.Link{Href: "https://christine.website/blog"},
|
|
|
|
Description: "My blog posts and rants about various technology things.",
|
|
|
|
Author: &feeds.Author{Name: "Christine Dodrill", Email: "me@christine.website"},
|
|
|
|
Created: bootTime,
|
|
|
|
Copyright: "This work is copyright Christine Dodrill. My viewpoints are my own and not the view of any employer past, current or future.",
|
|
|
|
},
|
|
|
|
jsonFeed: &jsonfeed.Feed{
|
|
|
|
Version: jsonfeed.CurrentVersion,
|
|
|
|
Title: "Christine Dodrill's Blog",
|
|
|
|
HomePageURL: "https://christine.website",
|
|
|
|
FeedURL: "https://christine.website/blog.json",
|
|
|
|
Description: "My blog posts and rants about various technology things.",
|
|
|
|
UserComment: "This is a JSON feed of my blogposts. For more information read: https://jsonfeed.org/version/1",
|
|
|
|
Icon: icon,
|
|
|
|
Favicon: icon,
|
|
|
|
Author: jsonfeed.Author{
|
|
|
|
Name: "Christine Dodrill",
|
|
|
|
Avatar: icon,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mux: http.NewServeMux(),
|
|
|
|
templates: map[string]*template.Template{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := filepath.Walk("./blog/", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-21 17:30:20 +00:00
|
|
|
|
2017-05-20 22:06:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-13 18:49:13 +00:00
|
|
|
output := blackfriday.Run(remaining)
|
2017-05-20 22:06:30 +00:00
|
|
|
|
|
|
|
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)
|
2019-03-21 17:30:20 +00:00
|
|
|
itime, _ := time.Parse("2006-01-02", p.Date)
|
|
|
|
smi.Add(&sitemap.URL{
|
|
|
|
Loc: "https://christine.website/" + p.Link,
|
|
|
|
LastMod: &itime,
|
|
|
|
ChangeFreq: sitemap.Monthly,
|
|
|
|
})
|
2017-05-20 22:06:30 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sort.Reverse(s.Posts))
|
|
|
|
|
2018-07-01 20:36:09 +00:00
|
|
|
resumeData, err := ioutil.ReadFile("./static/resume/resume.md")
|
2017-05-20 22:06:30 +00:00
|
|
|
if err != nil {
|
2017-05-20 22:40:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-01 20:36:09 +00:00
|
|
|
s.Resume = template.HTML(blackfriday.Run(resumeData))
|
2017-05-20 22:06:30 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
|
|
|
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,
|
2017-05-21 01:03:16 +00:00
|
|
|
ContentHTML: string(item.BodyHTML),
|
2017-05-20 22:06:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add HTTP routes here
|
2018-12-14 04:52:16 +00:00
|
|
|
s.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/" {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2019-01-26 19:47:16 +00:00
|
|
|
s.renderTemplatePage("error.html", "can't find "+r.URL.Path).ServeHTTP(w, r)
|
2018-12-14 04:52:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.renderTemplatePage("index.html", nil).ServeHTTP(w, r)
|
|
|
|
})
|
2019-01-26 19:47:16 +00:00
|
|
|
s.mux.HandleFunc("/.within/health", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Error(w, "OK", http.StatusOK)
|
|
|
|
})
|
2019-03-21 14:31:49 +00:00
|
|
|
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)))
|
2018-07-01 20:36:09 +00:00
|
|
|
s.mux.Handle("/css/", http.FileServer(http.Dir(".")))
|
|
|
|
s.mux.Handle("/static/", http.FileServer(http.Dir(".")))
|
2018-10-20 20:34:44 +00:00
|
|
|
s.mux.HandleFunc("/sw.js", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "./static/js/sw.js")
|
|
|
|
})
|
2019-03-21 17:30:20 +00:00
|
|
|
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) {
|
|
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
|
|
smi.WriteTo(w)
|
|
|
|
})))
|
2017-05-20 22:06:30 +00:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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] }
|