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"
|
2019-05-22 01:47:09 +00:00
|
|
|
"sort"
|
2017-05-20 22:06:30 +00:00
|
|
|
"time"
|
|
|
|
|
2019-05-25 19:14:09 +00:00
|
|
|
"christine.website/internal/blog"
|
|
|
|
"christine.website/internal/jsonfeed"
|
|
|
|
"christine.website/internal/middleware"
|
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-23 01:42:25 +00:00
|
|
|
"github.com/sebest/xff"
|
2019-03-21 17:30:20 +00:00
|
|
|
"github.com/snabb/sitemap"
|
2019-01-26 19:47:16 +00:00
|
|
|
"within.website/ln"
|
2019-03-23 01:42:25 +00:00
|
|
|
"within.website/ln/ex"
|
|
|
|
"within.website/ln/opname"
|
2017-05-20 22:06:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var port = os.Getenv("PORT")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if port == "" {
|
|
|
|
port = "29384"
|
|
|
|
}
|
|
|
|
|
2019-03-27 14:40:42 +00:00
|
|
|
ctx := ln.WithF(opname.With(context.Background(), "main"), ln.F{
|
|
|
|
"port": port,
|
|
|
|
})
|
|
|
|
|
2019-03-27 14:51:17 +00:00
|
|
|
_ = prometheus.Register(prommod.NewCollector("christine"))
|
2019-03-21 14:31:49 +00:00
|
|
|
|
2017-05-20 22:06:30 +00:00
|
|
|
s, err := Build()
|
|
|
|
if err != nil {
|
2019-03-27 14:40:42 +00:00
|
|
|
ln.FatalErr(ctx, err, ln.Action("Build"))
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 14:31:57 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/.within/health", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Error(w, "OK", http.StatusOK)
|
|
|
|
})
|
|
|
|
mux.Handle("/", s)
|
|
|
|
|
2019-03-27 14:40:42 +00:00
|
|
|
ln.Log(ctx, ln.Action("http_listening"))
|
|
|
|
ln.FatalErr(ctx, http.ListenAndServe(":"+port, mux))
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Site is the parent object for https://christine.website's backend.
|
|
|
|
type Site struct {
|
2019-03-27 14:18:52 +00:00
|
|
|
Posts blog.Posts
|
2019-05-22 01:47:09 +00:00
|
|
|
Talks blog.Posts
|
2017-05-20 22:06:30 +00:00
|
|
|
Resume template.HTML
|
|
|
|
|
|
|
|
rssFeed *feeds.Feed
|
|
|
|
jsonFeed *jsonfeed.Feed
|
|
|
|
|
2019-04-17 04:40:59 +00:00
|
|
|
mux *http.ServeMux
|
|
|
|
xffmw *xff.XFF
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-03-23 01:42:25 +00:00
|
|
|
ctx := opname.With(r.Context(), "site.ServeHTTP")
|
2019-03-27 14:18:52 +00:00
|
|
|
ctx = ln.WithF(ctx, ln.F{
|
|
|
|
"user_agent": r.Header.Get("User-Agent"),
|
|
|
|
})
|
2019-03-23 01:42:25 +00:00
|
|
|
r = r.WithContext(ctx)
|
2018-08-22 03:17:59 +00:00
|
|
|
|
2019-03-27 14:18:52 +00:00
|
|
|
middleware.RequestID(s.xffmw.Handler(ex.HTTPLog(s.mux))).ServeHTTP(w, r)
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 23:58:09 +00:00
|
|
|
var arbDate = time.Date(2019, time.May, 20, 18, 0, 0, 0, time.UTC)
|
2019-03-21 17:30:20 +00:00
|
|
|
|
2017-05-20 22:06:30 +00:00
|
|
|
// Build creates a new Site instance or fails.
|
|
|
|
func Build() (*Site, error) {
|
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,
|
|
|
|
})
|
|
|
|
|
2019-03-23 01:42:25 +00:00
|
|
|
xffmw, err := xff.Default()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
2019-03-27 14:18:52 +00:00
|
|
|
mux: http.NewServeMux(),
|
2019-03-23 01:42:25 +00:00
|
|
|
xffmw: xffmw,
|
2017-05-20 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 01:47:09 +00:00
|
|
|
posts, err := blog.LoadPosts("./blog/", "blog")
|
2017-05-20 22:06:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-27 14:18:52 +00:00
|
|
|
s.Posts = posts
|
2017-05-20 22:06:30 +00:00
|
|
|
|
2019-05-22 01:47:09 +00:00
|
|
|
talks, err := blog.LoadPosts("./talks", "talks")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.Talks = talks
|
|
|
|
|
|
|
|
var everything blog.Posts
|
|
|
|
everything = append(everything, posts...)
|
|
|
|
everything = append(everything, talks...)
|
|
|
|
|
|
|
|
sort.Sort(sort.Reverse(everything))
|
|
|
|
|
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
|
|
|
|
2019-05-22 01:47:09 +00:00
|
|
|
for _, item := range everything {
|
2017-05-20 22:06:30 +00:00
|
|
|
s.rssFeed.Items = append(s.rssFeed.Items, &feeds.Item{
|
|
|
|
Title: item.Title,
|
|
|
|
Link: &feeds.Link{Href: "https://christine.website/" + item.Link},
|
|
|
|
Description: item.Summary,
|
2019-03-27 14:18:52 +00:00
|
|
|
Created: item.Date,
|
2019-04-17 04:40:59 +00:00
|
|
|
Content: string(item.BodyHTML),
|
2017-05-20 22:06:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
s.jsonFeed.Items = append(s.jsonFeed.Items, jsonfeed.Item{
|
|
|
|
ID: "https://christine.website/" + item.Link,
|
|
|
|
URL: "https://christine.website/" + item.Link,
|
|
|
|
Title: item.Title,
|
2019-03-27 14:18:52 +00:00
|
|
|
DatePublished: item.Date,
|
2017-05-21 01:03:16 +00:00
|
|
|
ContentHTML: string(item.BodyHTML),
|
2017-05-20 22:06:30 +00:00
|
|
|
})
|
2019-04-03 14:26:37 +00:00
|
|
|
|
|
|
|
smi.Add(&sitemap.URL{
|
|
|
|
Loc: "https://christine.website/" + item.Link,
|
|
|
|
LastMod: &item.Date,
|
|
|
|
ChangeFreq: sitemap.Monthly,
|
|
|
|
})
|
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-03-21 14:31:49 +00:00
|
|
|
s.mux.Handle("/metrics", promhttp.Handler())
|
2019-03-27 14:18:52 +00:00
|
|
|
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)))
|
2019-05-22 01:47:09 +00:00
|
|
|
s.mux.Handle("/talks", middleware.Metrics("talks", s.renderTemplatePage("talkindex.html", s.Talks)))
|
2019-03-27 14:18:52 +00:00
|
|
|
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)))
|
2019-05-22 01:47:09 +00:00
|
|
|
s.mux.Handle("/talks/", middleware.Metrics("talks", http.HandlerFunc(s.showTalk)))
|
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")
|
|
|
|
})
|
2019-03-27 14:18:52 +00:00
|
|
|
s.mux.Handle("/sitemap.xml", middleware.Metrics("sitemap", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-03-21 17:30:20 +00:00
|
|
|
w.Header().Set("Content-Type", "application/xml")
|
2019-03-27 14:40:42 +00:00
|
|
|
_, _ = smi.WriteTo(w)
|
2019-03-21 17:30:20 +00:00
|
|
|
})))
|
2017-05-20 22:06:30 +00:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const icon = "https://christine.website/static/img/avatar.png"
|