diff options
| author | Christine Dodrill <me@christine.website> | 2019-03-27 07:18:52 -0700 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2019-03-27 07:18:52 -0700 |
| commit | d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a (patch) | |
| tree | 29ae006563834338e663452e6f810561596582ac /cmd | |
| parent | 20c08e68d1218475d6c6f3ca8ae718e1f508c696 (diff) | |
| download | xesite-d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a.tar.xz xesite-d0ff1b2d04fc0caf57d99f1d34ce13cf4aae6a1a.zip | |
reorg: phase 1
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/site/hash.go | 14 | ||||
| -rw-r--r-- | cmd/site/html.go | 10 | ||||
| -rw-r--r-- | cmd/site/main.go | 171 | ||||
| -rw-r--r-- | cmd/site/rss.go | 47 |
4 files changed, 65 insertions, 177 deletions
diff --git a/cmd/site/hash.go b/cmd/site/hash.go deleted file mode 100644 index ed6112c..0000000 --- a/cmd/site/hash.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import ( - "crypto/md5" - "fmt" -) - -// Hash is a simple wrapper around the MD5 algorithm implementation in the -// Go standard library. It takes in data and a salt and returns the hashed -// representation. -func Hash(data string, salt string) string { - output := md5.Sum([]byte(data + salt)) - return fmt.Sprintf("%x", output) -} diff --git a/cmd/site/html.go b/cmd/site/html.go index a645551..ba2b4fa 100644 --- a/cmd/site/html.go +++ b/cmd/site/html.go @@ -8,6 +8,8 @@ import ( "path/filepath" "time" + "christine.website/internal" + "christine.website/internal/blog" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "within.website/ln" @@ -30,7 +32,7 @@ func logTemplateTime(ctx context.Context, name string, f ln.F, from time.Time) { func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := opname.With(r.Context(), "renderTemplatePage") - fetag := "W/" + Hash(templateFname, etag) + "-1" + fetag := "W/" + internal.Hash(templateFname, etag) + "-1" f := ln.F{"etag": fetag, "if_none_match": r.Header.Get("If-None-Match")} @@ -74,14 +76,16 @@ func (s *Site) showPost(w http.ResponseWriter, r *http.Request) { } cmp := r.URL.Path[1:] - var p *Post + var p blog.Post + var found bool for _, pst := range s.Posts { if pst.Link == cmp { p = pst + found = true } } - if p == nil { + if !found { w.WriteHeader(http.StatusNotFound) s.renderTemplatePage("error.html", "no such post found: "+r.RequestURI).ServeHTTP(w, r) return diff --git a/cmd/site/main.go b/cmd/site/main.go index ec9648b..b6a809f 100644 --- a/cmd/site/main.go +++ b/cmd/site/main.go @@ -6,15 +6,12 @@ import ( "io/ioutil" "net/http" "os" - "path/filepath" - "sort" - "strings" "sync" "time" - "christine.website/internal/front" + "christine.website/internal/blog" "christine.website/internal/jsonfeed" - "github.com/celrenheit/sandflake" + "christine.website/internal/middleware" "github.com/gorilla/feeds" "github.com/povilasv/prommod" "github.com/prometheus/client_golang/prometheus" @@ -29,39 +26,6 @@ import ( var port = os.Getenv("PORT") -var ( - requestCounter = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "handler_requests_total", - Help: "Total number of request/responses by HTTP status code.", - }, []string{"handler", "code"}) - - requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ - Name: "handler_request_duration", - Help: "Handler request duration.", - }, []string{"handler", "method"}) - - requestInFlight = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Name: "handler_requests_in_flight", - Help: "Current number of requests being served.", - }, []string{"handler"}) -) - -func init() { - prometheus.Register(requestCounter) - prometheus.Register(requestDuration) - prometheus.Register(requestInFlight) -} - -func middlewareMetrics(family string, next http.Handler) http.Handler { - return promhttp.InstrumentHandlerDuration( - requestDuration.MustCurryWith(prometheus.Labels{"handler": family}), - promhttp.InstrumentHandlerCounter(requestCounter.MustCurryWith(prometheus.Labels{"handler": family}), - promhttp.InstrumentHandlerInFlight(requestInFlight.With(prometheus.Labels{"handler": family}), next), - ), - ) -} - func main() { if port == "" { port = "29384" @@ -78,30 +42,9 @@ func main() { http.ListenAndServe(":"+port, s) } -func requestIDMiddleware(next http.Handler) http.Handler { - var g sandflake.Generator - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - id := g.Next().String() - - if rid := r.Header.Get("X-Request-Id"); rid != "" { - id = rid + "," + id - } - - ctx := ln.WithF(r.Context(), ln.F{ - "request_id": id, - }) - r = r.WithContext(ctx) - - w.Header().Set("X-Request-Id", id) - r.Header.Set("X-Request-Id", id) - - next.ServeHTTP(w, r) - }) -} - // Site is the parent object for https://christine.website's backend. type Site struct { - Posts Posts + Posts blog.Posts Resume template.HTML rssFeed *feeds.Feed @@ -109,7 +52,7 @@ type Site struct { mux *http.ServeMux sitemap []byte - xffmw *xff.XFF + xffmw *xff.XFF templates map[string]*template.Template tlock sync.RWMutex @@ -117,20 +60,18 @@ type Site struct { func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx := opname.With(r.Context(), "site.ServeHTTP") + ctx = ln.WithF(ctx, ln.F{ + "user_agent": r.Header.Get("User-Agent"), + }) r = r.WithContext(ctx) - requestIDMiddleware(s.xffmw.Handler(ex.HTTPLog(s.mux))).ServeHTTP(w, r) + middleware.RequestID(s.xffmw.Handler(ex.HTTPLog(s.mux))).ServeHTTP(w, r) } var arbDate = time.Date(2019, time.March, 21, 18, 0, 0, 0, time.UTC) // Build creates a new Site instance or fails. func Build() (*Site, error) { - type postFM struct { - Title string - Date string - } - smi := sitemap.New() smi.Add(&sitemap.URL{ Loc: "https://christine.website/resume", @@ -156,7 +97,6 @@ func Build() (*Site, error) { ChangeFreq: sitemap.Weekly, }) - xffmw, err := xff.Default() if err != nil { return nil, err @@ -185,61 +125,15 @@ func Build() (*Site, error) { Avatar: icon, }, }, - mux: http.NewServeMux(), + mux: http.NewServeMux(), xffmw: xffmw, } - err = filepath.Walk("./blog/", func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if info.IsDir() { - return nil - } - - fin, err := os.Open(path) - if err != nil { - return err - } - defer fin.Close() - - content, err := ioutil.ReadAll(fin) - if err != nil { - return err - } - - var fm postFM - remaining, err := front.Unmarshal(content, &fm) - if err != nil { - return err - } - - output := blackfriday.Run(remaining) - - p := &Post{ - Title: fm.Title, - Date: fm.Date, - Link: strings.Split(path, ".")[0], - Body: string(remaining), - BodyHTML: template.HTML(output), - } - - s.Posts = append(s.Posts, p) - itime, _ := time.Parse("2006-01-02", p.Date) - smi.Add(&sitemap.URL{ - Loc: "https://christine.website/" + p.Link, - LastMod: &itime, - ChangeFreq: sitemap.Monthly, - }) - - return nil - }) + posts, err := blog.LoadPosts("./blog/") if err != nil { return nil, err } - - sort.Sort(sort.Reverse(s.Posts)) + s.Posts = posts resumeData, err := ioutil.ReadFile("./static/resume/resume.md") if err != nil { @@ -249,19 +143,18 @@ func Build() (*Site, error) { s.Resume = template.HTML(blackfriday.Run(resumeData)) for _, item := range s.Posts { - itime, _ := time.Parse("2006-01-02", item.Date) s.rssFeed.Items = append(s.rssFeed.Items, &feeds.Item{ Title: item.Title, Link: &feeds.Link{Href: "https://christine.website/" + item.Link}, Description: item.Summary, - Created: itime, + Created: item.Date, }) s.jsonFeed.Items = append(s.jsonFeed.Items, jsonfeed.Item{ ID: "https://christine.website/" + item.Link, URL: "https://christine.website/" + item.Link, Title: item.Title, - DatePublished: itime, + DatePublished: item.Date, ContentHTML: string(item.BodyHTML), }) } @@ -280,13 +173,13 @@ func Build() (*Site, error) { http.Error(w, "OK", http.StatusOK) }) s.mux.Handle("/metrics", promhttp.Handler()) - s.mux.Handle("/resume", middlewareMetrics("resume", s.renderTemplatePage("resume.html", s.Resume))) - s.mux.Handle("/blog", middlewareMetrics("blog", s.renderTemplatePage("blogindex.html", s.Posts))) - s.mux.Handle("/contact", middlewareMetrics("contact", s.renderTemplatePage("contact.html", nil))) - s.mux.Handle("/blog.rss", middlewareMetrics("blog.rss", http.HandlerFunc(s.createFeed))) - s.mux.Handle("/blog.atom", middlewareMetrics("blog.atom", http.HandlerFunc(s.createAtom))) - s.mux.Handle("/blog.json", middlewareMetrics("blog.json", http.HandlerFunc(s.createJsonFeed))) - s.mux.Handle("/blog/", middlewareMetrics("blogpost", http.HandlerFunc(s.showPost))) + 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("/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("/css/", http.FileServer(http.Dir("."))) s.mux.Handle("/static/", http.FileServer(http.Dir("."))) s.mux.HandleFunc("/sw.js", func(w http.ResponseWriter, r *http.Request) { @@ -295,7 +188,7 @@ func Build() (*Site, error) { s.mux.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./static/robots.txt") }) - s.mux.Handle("/sitemap.xml", middlewareMetrics("sitemap", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + s.mux.Handle("/sitemap.xml", middleware.Metrics("sitemap", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/xml") smi.WriteTo(w) }))) @@ -304,25 +197,3 @@ func Build() (*Site, error) { } const icon = "https://christine.website/static/img/avatar.png" - -// 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 string `json:"date"` -} - -// Posts implements sort.Interface for a slice of Post objects. -type Posts []*Post - -func (p Posts) Len() int { return len(p) } -func (p Posts) Less(i, j int) bool { - iDate, _ := time.Parse("2006-01-02", p[i].Date) - jDate, _ := time.Parse("2006-01-02", p[j].Date) - - return iDate.Unix() < jDate.Unix() -} -func (p Posts) Swap(i, j int) { p[i], p[j] = p[j], p[i] } diff --git a/cmd/site/rss.go b/cmd/site/rss.go index 2f319b2..5b2c73a 100644 --- a/cmd/site/rss.go +++ b/cmd/site/rss.go @@ -5,19 +5,29 @@ import ( "net/http" "time" + "christine.website/internal" "within.website/ln" + "within.website/ln/opname" ) var bootTime = time.Now() -var etag = Hash(bootTime.String(), IncrediblySecureSalt) +var etag = internal.Hash(bootTime.String(), IncrediblySecureSalt) // IncrediblySecureSalt ******* const IncrediblySecureSalt = "hunter2" func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/rss+xml") - w.Header().Set("ETag", "W/"+Hash(bootTime.String(), IncrediblySecureSalt)) + ctx := opname.With(r.Context(), "rss-feed") + fetag := "W/" + internal.Hash(bootTime.String(), IncrediblySecureSalt) + w.Header().Set("ETag", fetag) + + if r.Header.Get("If-None-Match") == fetag { + http.Error(w, "Cached data OK", http.StatusNotModified) + ln.Log(ctx, ln.Info("cache hit")) + return + } + w.Header().Set("Content-Type", "application/rss+xml") err := s.rssFeed.WriteRss(w) if err != nil { http.Error(w, "Internal server error", http.StatusInternalServerError) @@ -31,13 +41,21 @@ func (s *Site) createFeed(w http.ResponseWriter, r *http.Request) { } func (s *Site) createAtom(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/atom+xml") - w.Header().Set("ETag", "W/"+Hash(bootTime.String(), IncrediblySecureSalt)) + ctx := opname.With(r.Context(), "atom-feed") + fetag := "W/" + internal.Hash(bootTime.String(), IncrediblySecureSalt) + w.Header().Set("ETag", fetag) + + if r.Header.Get("If-None-Match") == fetag { + http.Error(w, "Cached data OK", http.StatusNotModified) + ln.Log(ctx, ln.Info("cache hit")) + return + } + w.Header().Set("Content-Type", "application/atom+xml") err := s.rssFeed.WriteAtom(w) if err != nil { http.Error(w, "Internal server error", http.StatusInternalServerError) - ln.Error(r.Context(), err, ln.F{ + ln.Error(ctx, err, ln.F{ "remote_addr": r.RemoteAddr, "action": "generating_atom", "uri": r.RequestURI, @@ -46,16 +64,25 @@ func (s *Site) createAtom(w http.ResponseWriter, r *http.Request) { } } -func (s *Site) createJsonFeed(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - w.Header().Set("ETag", Hash(bootTime.String(), IncrediblySecureSalt)) +func (s *Site) createJSONFeed(w http.ResponseWriter, r *http.Request) { + ctx := opname.With(r.Context(), "atom-feed") + fetag := "W/" + internal.Hash(bootTime.String(), IncrediblySecureSalt) + w.Header().Set("ETag", fetag) + + if r.Header.Get("If-None-Match") == fetag { + http.Error(w, "Cached data OK", http.StatusNotModified) + ln.Log(ctx, ln.Info("cache hit")) + return + } + + w.Header().Set("Content-Type", "application/json") e := json.NewEncoder(w) e.SetIndent("", "\t") err := e.Encode(s.jsonFeed) if err != nil { http.Error(w, "Internal server error", http.StatusInternalServerError) - ln.Error(r.Context(), err, ln.F{ + ln.Error(ctx, err, ln.F{ "remote_addr": r.RemoteAddr, "action": "generating_jsonfeed", "uri": r.RequestURI, |
