From 06a07af36d2f14747fbc295809c2181c6979401f Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 13 Aug 2017 15:04:40 +1200 Subject: [PATCH] router: handle static assets, handle page errors --- ArchiveServer.go | 2 ++ ArchiveState.go | 33 ++++++++++++++++++++++----------- Router.go | 21 ++++++++++++++++++++- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/ArchiveServer.go b/ArchiveServer.go index 8436d6f..cbcbe21 100644 --- a/ArchiveServer.go +++ b/ArchiveServer.go @@ -12,6 +12,7 @@ const ( type ArchiveServer struct { timezone *time.Location cfg *Config + startup time.Time } func NewArchiveServer(cfg *Config) (*ArchiveServer, error) { @@ -37,5 +38,6 @@ func NewArchiveServer(cfg *Config) (*ArchiveServer, error) { return &ArchiveServer{ timezone: tz, cfg: cfg, + startup: time.Now(), }, nil } diff --git a/ArchiveState.go b/ArchiveState.go index 7ce0ee7..a477345 100644 --- a/ArchiveState.go +++ b/ArchiveState.go @@ -12,23 +12,34 @@ type ArchiveState struct { logBestSlug string query string queryIsRegex bool - nonce string ym YearMonth page int highestPage int } -func NewArchiveState(svr *ArchiveServer, log *LogSource) (*ArchiveState, error) { - logBestSlug, err := svr.bestSlugFor(log) +func NewArchiveState(svr *ArchiveServer) *ArchiveState { + return &ArchiveState{svr: svr} +} + +func (this *ArchiveState) selectSource(log *LogSource) error { + logBestSlug, err := this.svr.bestSlugFor(log) if err != nil { - return nil, err + return err } - return &ArchiveState{ - svr: svr, - log: log, - logBestSlug: logBestSlug, - }, nil + this.log = log + this.logBestSlug = logBestSlug + return nil +} + +func (this *ArchiveState) renderError(w http.ResponseWriter, msg string) { + this.renderTemplate(w, []byte(template.HTMLEscapeString(msg))) +} + +func (this *ArchiveState) renderTemplate(w http.ResponseWriter, body []byte) { + this.renderTemplateHead(w) + w.Write(body) + this.renderTemplateFoot(w) } func (this *ArchiveState) renderTemplateHead(w http.ResponseWriter) { @@ -75,7 +86,7 @@ func (this *ArchiveState) renderTemplateHead(w http.ResponseWriter) { slug, _ := this.svr.bestSlugFor(&this.svr.cfg.Logs[i]) current := (this.log == &this.svr.cfg.Logs[i]) - w.Write([]byte(``)) + w.Write([]byte(``)) } w.Write([]byte(` @@ -165,7 +176,7 @@ func (this *ArchiveState) renderTemplateHead(w http.ResponseWriter) { func (this *ArchiveState) renderTemplateFoot(w http.ResponseWriter) { w.Write([]byte(` - + `)) diff --git a/Router.go b/Router.go index 0912acf..cd090bd 100644 --- a/Router.go +++ b/Router.go @@ -1,6 +1,7 @@ package archive import ( + "bytes" "errors" "fmt" "net/http" @@ -62,11 +63,29 @@ func (this *ArchiveServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } + if len(r.URL.Path) == 0 { + http.Error(w, "Invalid request", http.StatusBadRequest) + return + } + + // Handle static assets + + static, err := Asset(r.URL.Path[1:]) + if err == nil { + http.ServeContent(w, r, r.URL.Path[1:], this.startup, bytes.NewReader(static)) + return + } + + // Handle application routes + + arc := NewArchiveState(this) + if len(r.URL.Query().Get("y")) > 0 || len(r.URL.Query().Get("q")) > 0 || len(r.URL.Query().Get("h")) > 0 { this.legacyRoute(w, r) } else { - // TODO + arc.renderError(w, "Unknown route.") + } }