From ca39609fefada1817c7daf1c113b90221304c4ba Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 13 Aug 2017 15:18:03 +1200 Subject: [PATCH] router: scaffolding for remaining routes --- ArchiveServer.go | 13 ++++++++++--- Router.go | 23 ++++++++++++++++++++++- tplError.go | 0 3 files changed, 32 insertions(+), 4 deletions(-) delete mode 100644 tplError.go diff --git a/ArchiveServer.go b/ArchiveServer.go index cbcbe21..2e7e717 100644 --- a/ArchiveServer.go +++ b/ArchiveServer.go @@ -2,6 +2,7 @@ package archive import ( "fmt" + "regexp" "time" ) @@ -13,6 +14,8 @@ type ArchiveServer struct { timezone *time.Location cfg *Config startup time.Time + + rxViewRoot, rxViewPage, rxSearch, rxSearchRx *regexp.Regexp } func NewArchiveServer(cfg *Config) (*ArchiveServer, error) { @@ -36,8 +39,12 @@ func NewArchiveServer(cfg *Config) (*ArchiveServer, error) { } return &ArchiveServer{ - timezone: tz, - cfg: cfg, - startup: time.Now(), + timezone: tz, + cfg: cfg, + startup: time.Now(), + rxViewRoot: regexp.MustCompile(`^/([^/]+)/(\d+)/(\d+)$`), + rxViewPage: regexp.MustCompile(`^/([^/]+)/(\d+)/(\d+)/(?:page-)?(\d+)$`), + rxSearch: regexp.MustCompile(`^/([^/]+)/search/(.*)$`), + rxSearchRx: regexp.MustCompile(`^/([^/]+)/rx/(.*)$`), }, nil } diff --git a/Router.go b/Router.go index cd090bd..c03f94d 100644 --- a/Router.go +++ b/Router.go @@ -80,9 +80,30 @@ func (this *ArchiveServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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 { + if r.URL.Path == `/` { + ls := &this.cfg.Logs[0] + slug, _ := this.bestSlugFor(ls) + dt := ls.LatestDate() + http.Redirect(w, r, fmt.Sprintf(`/%s/%d/%d`, slug, dt.Year, dt.Month), http.StatusTemporaryRedirect) + + } else 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 if r.URL.Path == `/download` { + arc.renderError(w, "Not implemented.") // FIXME + + } else if this.rxViewRoot.MatchString(r.URL.Path) { + arc.renderError(w, "Not implemented.") // FIXME + + } else if this.rxViewPage.MatchString(r.URL.Path) { + arc.renderError(w, "Not implemented.") // FIXME + + } else if this.rxSearch.MatchString(r.URL.Path) { + arc.renderError(w, "Not implemented.") // FIXME + + } else if this.rxSearchRx.MatchString(r.URL.Path) { + arc.renderError(w, "Not implemented.") // FIXME + } else { arc.renderError(w, "Unknown route.") diff --git a/tplError.go b/tplError.go deleted file mode 100644 index e69de29..0000000