From 77db7e371bc71fb671d38fa3b09cc4ec9e8657b6 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 21 May 2020 18:37:50 -0400 Subject: [PATCH] cmd/site: more clacks --- cmd/site/clacks.go | 25 +++++++++++++++++++++++++ cmd/site/main.go | 14 ++++++++++++-- shell.nix | 2 ++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 cmd/site/clacks.go diff --git a/cmd/site/clacks.go b/cmd/site/clacks.go new file mode 100644 index 0000000..3415d14 --- /dev/null +++ b/cmd/site/clacks.go @@ -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()) +} diff --git a/cmd/site/main.go b/cmd/site/main.go index bb7b369..b5f7df9 100644 --- a/cmd/site/main.go +++ b/cmd/site/main.go @@ -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, } diff --git a/shell.nix b/shell.nix index 6464c11..9453b2a 100644 --- a/shell.nix +++ b/shell.nix @@ -27,4 +27,6 @@ mkShell { # tools ispell ]; + + CLACK_SET = "Ashlynn,Terry Davis,Dennis Ritchie"; }