From 7e6a1cbd5841b094a7e168f8500075443f31e8e0 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 27 Mar 2019 07:31:57 -0700 Subject: [PATCH] make logging cleaner, etag more --- Dockerfile | 2 +- cmd/site/html.go | 13 ++++++++++++- cmd/site/main.go | 15 +++++++-------- cmd/site/rss.go | 1 - internal/blog/blog.go | 24 +++++++++++++----------- templates/blogindex.html | 2 +- 6 files changed, 34 insertions(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4391948..653a92c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,5 +16,5 @@ COPY ./blog /site/blog COPY ./css /site/css COPY ./app /app COPY ./app.json . -HEALTHCHECK CMD wget --spider http://127.0.0.1:5000 || exit 1 +HEALTHCHECK CMD wget --spider http://127.0.0.1:5000/.within/health || exit 1 CMD ./site diff --git a/cmd/site/html.go b/cmd/site/html.go index ba2b4fa..9f208af 100644 --- a/cmd/site/html.go +++ b/cmd/site/html.go @@ -91,6 +91,17 @@ func (s *Site) showPost(w http.ResponseWriter, r *http.Request) { return } - s.renderTemplatePage("blogpost.html", p).ServeHTTP(w, r) + const dateFormat = `2006-01-02` + s.renderTemplatePage("blogpost.html", struct { + Title string + Link string + BodyHTML template.HTML + Date string + }{ + Title: p.Title, + Link: p.Link, + BodyHTML: p.BodyHTML, + Date: p.Date.Format(dateFormat), + }).ServeHTTP(w, r) postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc() } diff --git a/cmd/site/main.go b/cmd/site/main.go index b6a809f..3719095 100644 --- a/cmd/site/main.go +++ b/cmd/site/main.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "net/http" "os" - "sync" "time" "christine.website/internal/blog" @@ -38,8 +37,14 @@ func main() { ln.FatalErr(context.Background(), err, ln.Action("Build")) } + mux := http.NewServeMux() + mux.HandleFunc("/.within/health", func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "OK", http.StatusOK) + }) + mux.Handle("/", s) + ln.Log(context.Background(), ln.F{"action": "http_listening", "port": port}) - http.ListenAndServe(":"+port, s) + http.ListenAndServe(":"+port, mux) } // Site is the parent object for https://christine.website's backend. @@ -53,9 +58,6 @@ type Site struct { mux *http.ServeMux sitemap []byte xffmw *xff.XFF - - templates map[string]*template.Template - tlock sync.RWMutex } func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -169,9 +171,6 @@ func Build() (*Site, error) { s.renderTemplatePage("index.html", nil).ServeHTTP(w, r) }) - s.mux.HandleFunc("/.within/health", func(w http.ResponseWriter, r *http.Request) { - http.Error(w, "OK", http.StatusOK) - }) s.mux.Handle("/metrics", promhttp.Handler()) 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))) diff --git a/cmd/site/rss.go b/cmd/site/rss.go index 5b2c73a..e03e07f 100644 --- a/cmd/site/rss.go +++ b/cmd/site/rss.go @@ -75,7 +75,6 @@ func (s *Site) createJSONFeed(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "application/json") e := json.NewEncoder(w) e.SetIndent("", "\t") diff --git a/internal/blog/blog.go b/internal/blog/blog.go index 54d5617..a73f27b 100644 --- a/internal/blog/blog.go +++ b/internal/blog/blog.go @@ -15,12 +15,13 @@ import ( // Post is a single blogpost. type Post struct { - Title string `json:"title"` - Link string `json:"link"` - Summary string `json:"summary,omitifempty"` - Body string `json:"-"` - BodyHTML template.HTML `json:"body"` - Date time.Time `json:"date"` + Title string `json:"title"` + Link string `json:"link"` + Summary string `json:"summary,omitifempty"` + Body string `json:"-"` + BodyHTML template.HTML `json:"body"` + Date time.Time + DateString string `json:"date"` } // Posts implements sort.Interface for a slice of Post objects. @@ -78,11 +79,12 @@ func LoadPosts(path string) (Posts, error) { } p := Post{ - Title: fm.Title, - Date: date, - Link: strings.Split(path, ".")[0], - Body: string(remaining), - BodyHTML: template.HTML(output), + Title: fm.Title, + Date: date, + DateString: fm.Date, + Link: strings.Split(path, ".")[0], + Body: string(remaining), + BodyHTML: template.HTML(output), } result = append(result, p) diff --git a/templates/blogindex.html b/templates/blogindex.html index 8e33197..5a1ac02 100644 --- a/templates/blogindex.html +++ b/templates/blogindex.html @@ -10,7 +10,7 @@