cmd/site: more clacks

This commit is contained in:
Cadey Ratio 2020-05-21 18:37:50 -04:00
parent 60b7fc345e
commit 77db7e371b
3 changed files with 39 additions and 2 deletions

25
cmd/site/clacks.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"math/rand"
"net/http"
"time"
)
type ClackSet []string
func (cs ClackSet) Name() string {
return "GNU " + cs[rand.Intn(len(cs))]
}
func (cs ClackSet) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("X-Clacks-Overhead", cs.Name())
next.ServeHTTP(w, r)
})
}
func init() {
rand.Seed(time.Now().Unix())
}

View File

@ -7,6 +7,7 @@ import (
"net/http"
"os"
"sort"
"strings"
"time"
"christine.website/cmd/site/internal/blog"
@ -63,6 +64,7 @@ type Site struct {
Series []string
SignalBoost []Person
clacks ClackSet
patrons []string
rssFeed *feeds.Feed
jsonFeed *jsonfeed.Feed
@ -73,6 +75,14 @@ type Site struct {
var gitRev = os.Getenv("GIT_REV")
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) {
ctx := opname.With(r.Context(), "site.ServeHTTP")
ctx = ln.WithF(ctx, ln.F{
@ -82,9 +92,8 @@ func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if gitRev != "" {
w.Header().Add("X-Git-Rev", gitRev)
}
w.Header().Add("X-Clacks-Overhead", "GNU Ashlynn")
middleware.RequestID(s.xffmw.Handler(ex.HTTPLog(s.mux))).ServeHTTP(w, r)
s.clacks.Middleware(middleware.RequestID(s.xffmw.Handler(ex.HTTPLog(s.mux)))).ServeHTTP(w, r)
}
var arbDate = time.Date(2020, time.February, 29, 0, 0, 0, 0, time.UTC)
@ -168,6 +177,7 @@ func Build() (*Site, error) {
mux: http.NewServeMux(),
xffmw: xffmw,
clacks: ClackSet(strings.Split(envOr("CLACK_SET", "Ashlynn"), ",")),
patrons: pledges,
SignalBoost: people,
}

View File

@ -27,4 +27,6 @@ mkShell {
# tools
ispell
];
CLACK_SET = "Ashlynn,Terry Davis,Dennis Ritchie";
}