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

View File

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