xesite/cmd/site/main.go

296 lines
8.3 KiB
Go
Raw Normal View History

package main
import (
2017-12-13 18:49:13 +00:00
"context"
"html/template"
"io/ioutil"
"net/http"
"os"
"sort"
2020-05-21 22:37:50 +00:00
"strings"
"time"
"christine.website/cmd/site/internal/blog"
"christine.website/cmd/site/internal/middleware"
2020-01-11 18:32:56 +00:00
"christine.website/jsonfeed"
"github.com/gorilla/feeds"
_ "github.com/joho/godotenv/autoload"
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"
)
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,
"git_rev": gitRev,
2019-03-27 14:40:42 +00:00
})
2019-03-27 14:51:17 +00:00
_ = prometheus.Register(prommod.NewCollector("christine"))
2019-03-21 14:31:49 +00:00
s, err := Build()
if err != nil {
2019-03-27 14:40:42 +00:00
ln.FatalErr(ctx, err, ln.Action("Build"))
}
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))
}
// Site is the parent object for https://christine.website's backend.
type Site struct {
2020-03-18 20:45:55 +00:00
Posts blog.Posts
Talks blog.Posts
Gallery blog.Posts
Resume template.HTML
Series []string
SignalBoost []Person
2020-05-21 22:37:50 +00:00
clacks ClackSet
2020-03-18 20:45:55 +00:00
patrons []string
rssFeed *feeds.Feed
jsonFeed *jsonfeed.Feed
mux *http.ServeMux
xffmw *xff.XFF
}
var gitRev = os.Getenv("GIT_REV")
2020-05-21 22:37:50 +00:00
func envOr(key, or string) string {
if result, ok := os.LookupEnv(key); ok {
return result
}
return or
}
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)
if gitRev != "" {
w.Header().Add("X-Git-Rev", gitRev)
}
2018-08-22 03:17:59 +00:00
2020-05-21 22:40:08 +00:00
w.Header().Add("X-Hacker", "If you are reading this, check out /signalboost to find people for your team")
s.clacks.Middleware(
middleware.RequestID(
s.xffmw.Handler(
ex.HTTPLog(s.mux),
),
),
).ServeHTTP(w, r)
}
2020-05-21 22:40:08 +00:00
var arbDate = time.Date(2020, time.May, 21, 0, 0, 0, 0, time.UTC)
2019-03-21 17:30:20 +00:00
// Build creates a new Site instance or fails.
func Build() (*Site, error) {
pc, err := NewPatreonClient()
if err != nil {
return nil, err
}
pledges, err := GetPledges(pc)
if err != nil {
return nil, err
}
2020-03-18 20:45:55 +00:00
people, err := loadPeople("./signalboost.dhall")
if err != nil {
return nil, err
}
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/patrons",
LastMod: &arbDate,
ChangeFreq: sitemap.Weekly,
})
2019-03-21 17:30:20 +00:00
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
}
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,
2020-05-21 22:37:50 +00:00
clacks: ClackSet(strings.Split(envOr("CLACK_SET", "Ashlynn"), ",")),
2020-03-18 20:45:55 +00:00
patrons: pledges,
SignalBoost: people,
}
posts, err := blog.LoadPosts("./blog/", "blog")
if err != nil {
return nil, err
}
2019-03-27 14:18:52 +00:00
s.Posts = posts
s.Series = posts.Series()
sort.Strings(s.Series)
talks, err := blog.LoadPosts("./talks", "talks")
if err != nil {
return nil, err
}
s.Talks = talks
gallery, err := blog.LoadPosts("./gallery", "gallery")
if err != nil {
return nil, err
}
s.Gallery = gallery
var everything blog.Posts
everything = append(everything, posts...)
everything = append(everything, talks...)
everything = append(everything, gallery...)
sort.Sort(sort.Reverse(everything))
2018-07-01 20:36:09 +00:00
resumeData, err := ioutil.ReadFile("./static/resume/resume.md")
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))
for _, item := range everything {
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,
Content: string(item.BodyHTML),
})
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),
2020-01-11 18:32:56 +00:00
Tags: item.Tags,
})
2019-04-03 14:26:37 +00:00
smi.Add(&sitemap.URL{
Loc: "https://christine.website/" + item.Link,
LastMod: &item.Date,
ChangeFreq: sitemap.Monthly,
})
}
// 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())
s.mux.Handle("/patrons", middleware.Metrics("patrons", s.renderTemplatePage("patrons.html", s.patrons)))
2020-03-18 20:45:55 +00:00
s.mux.Handle("/signalboost", middleware.Metrics("signalboost", s.renderTemplatePage("signalboost.html", s.SignalBoost)))
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)))
s.mux.Handle("/talks", middleware.Metrics("talks", s.renderTemplatePage("talkindex.html", s.Talks)))
s.mux.Handle("/gallery", middleware.Metrics("gallery", s.renderTemplatePage("galleryindex.html", s.Gallery)))
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)))
s.mux.Handle("/blog/series", http.HandlerFunc(s.listSeries))
s.mux.Handle("/blog/series/", http.HandlerFunc(s.showSeries))
s.mux.Handle("/talks/", middleware.Metrics("talks", http.HandlerFunc(s.showTalk)))
s.mux.Handle("/gallery/", middleware.Metrics("gallery", http.HandlerFunc(s.showGallery)))
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
})))
s.mux.HandleFunc("/api/pageview-timer", handlePageViewTimer)
return s, nil
}
const icon = "https://christine.website/static/img/avatar.png"