make logging cleaner, etag more

This commit is contained in:
Cadey Ratio 2019-03-27 07:31:57 -07:00
parent d0ff1b2d04
commit 7e6a1cbd58
6 changed files with 34 additions and 23 deletions

View File

@ -16,5 +16,5 @@ COPY ./blog /site/blog
COPY ./css /site/css COPY ./css /site/css
COPY ./app /app COPY ./app /app
COPY ./app.json . 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 CMD ./site

View File

@ -91,6 +91,17 @@ func (s *Site) showPost(w http.ResponseWriter, r *http.Request) {
return 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() postView.With(prometheus.Labels{"base": filepath.Base(p.Link)}).Inc()
} }

View File

@ -6,7 +6,6 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"sync"
"time" "time"
"christine.website/internal/blog" "christine.website/internal/blog"
@ -38,8 +37,14 @@ func main() {
ln.FatalErr(context.Background(), err, ln.Action("Build")) 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}) 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. // Site is the parent object for https://christine.website's backend.
@ -53,9 +58,6 @@ type Site struct {
mux *http.ServeMux mux *http.ServeMux
sitemap []byte sitemap []byte
xffmw *xff.XFF xffmw *xff.XFF
templates map[string]*template.Template
tlock sync.RWMutex
} }
func (s *Site) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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.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("/metrics", promhttp.Handler())
s.mux.Handle("/resume", middleware.Metrics("resume", s.renderTemplatePage("resume.html", s.Resume))) 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("/blog", middleware.Metrics("blog", s.renderTemplatePage("blogindex.html", s.Posts)))

View File

@ -75,7 +75,6 @@ func (s *Site) createJSONFeed(w http.ResponseWriter, r *http.Request) {
return return
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
e := json.NewEncoder(w) e := json.NewEncoder(w)
e.SetIndent("", "\t") e.SetIndent("", "\t")

View File

@ -15,12 +15,13 @@ import (
// Post is a single blogpost. // Post is a single blogpost.
type Post struct { type Post struct {
Title string `json:"title"` Title string `json:"title"`
Link string `json:"link"` Link string `json:"link"`
Summary string `json:"summary,omitifempty"` Summary string `json:"summary,omitifempty"`
Body string `json:"-"` Body string `json:"-"`
BodyHTML template.HTML `json:"body"` BodyHTML template.HTML `json:"body"`
Date time.Time `json:"date"` Date time.Time
DateString string `json:"date"`
} }
// Posts implements sort.Interface for a slice of Post objects. // Posts implements sort.Interface for a slice of Post objects.
@ -78,11 +79,12 @@ func LoadPosts(path string) (Posts, error) {
} }
p := Post{ p := Post{
Title: fm.Title, Title: fm.Title,
Date: date, Date: date,
Link: strings.Split(path, ".")[0], DateString: fm.Date,
Body: string(remaining), Link: strings.Split(path, ".")[0],
BodyHTML: template.HTML(output), Body: string(remaining),
BodyHTML: template.HTML(output),
} }
result = append(result, p) result = append(result, p)

View File

@ -10,7 +10,7 @@
<p> <p>
<ul> <ul>
{{ range . }} {{ range . }}
<li>{{ .Date }} - <a href="{{ .Link }}">{{ .Title }}</a></li> <li>{{ .DateString }} - <a href="{{ .Link }}">{{ .Title }}</a></li>
{{ end }} {{ end }}
</ul> </ul>
</p> </p>