router: scaffolding for remaining routes

This commit is contained in:
mappu 2017-08-13 15:18:03 +12:00
parent 06a07af36d
commit ca39609fef
3 changed files with 32 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package archive
import ( import (
"fmt" "fmt"
"regexp"
"time" "time"
) )
@ -13,6 +14,8 @@ type ArchiveServer struct {
timezone *time.Location timezone *time.Location
cfg *Config cfg *Config
startup time.Time startup time.Time
rxViewRoot, rxViewPage, rxSearch, rxSearchRx *regexp.Regexp
} }
func NewArchiveServer(cfg *Config) (*ArchiveServer, error) { func NewArchiveServer(cfg *Config) (*ArchiveServer, error) {
@ -36,8 +39,12 @@ func NewArchiveServer(cfg *Config) (*ArchiveServer, error) {
} }
return &ArchiveServer{ return &ArchiveServer{
timezone: tz, timezone: tz,
cfg: cfg, cfg: cfg,
startup: time.Now(), startup: time.Now(),
rxViewRoot: regexp.MustCompile(`^/([^/]+)/(\d+)/(\d+)$`),
rxViewPage: regexp.MustCompile(`^/([^/]+)/(\d+)/(\d+)/(?:page-)?(\d+)$`),
rxSearch: regexp.MustCompile(`^/([^/]+)/search/(.*)$`),
rxSearchRx: regexp.MustCompile(`^/([^/]+)/rx/(.*)$`),
}, nil }, nil
} }

View File

@ -80,9 +80,30 @@ func (this *ArchiveServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
arc := NewArchiveState(this) 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) 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 { } else {
arc.renderError(w, "Unknown route.") arc.renderError(w, "Unknown route.")

View File