diff --git a/Router.go b/Router.go index a61fbf2..ff67938 100644 --- a/Router.go +++ b/Router.go @@ -121,48 +121,37 @@ func (this *ArchiveServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (this *ArchiveServer) legacyRoute(w http.ResponseWriter, r *http.Request) { + u := URLHelper{w, r} - intval := func(sz string) int { - ret, _ := strconv.Atoi(sz) - return ret - } - get := r.URL.Query().Get - hasGet := func(sz string) bool { - return len(get(sz)) > 0 - } - redirectf := func(format string, a ...interface{}) { - http.Redirect(w, r, fmt.Sprintf(format, a...), http.StatusTemporaryRedirect) + hubid := 0 + if u.hasGet("h") { + hubid = u.intval(u.get("h")) } // - hubid := 0 - if hasGet("h") { - hubid = intval(get("h")) - } - - if hasGet("q") { - if hasGet("rx") { - redirectf(`/%d/rx/%s`, hubid, url.QueryEscape(get("q"))) + if u.hasGet("q") { + if u.hasGet("rx") { + u.redirectf(`/%d/rx/%s`, hubid, url.QueryEscape(u.get("q"))) } else { - redirectf(`/%d/search/%s`, hubid, url.QueryEscape(get("q"))) + u.redirectf(`/%d/search/%s`, hubid, url.QueryEscape(u.get("q"))) } - } else if hasGet("y") && hasGet("m") { - year := intval(get("y")) - month := intval(get("m")) - if hasGet("p") { - redirectf(`/%d/%d/%d/page-%d`, hubid, year, month, intval(get("p"))) + } else if u.hasGet("y") && u.hasGet("m") { + year := u.intval(u.get("y")) + month := u.intval(u.get("m")) + if u.hasGet("p") { + u.redirectf(`/%d/%d/%d/page-%d`, hubid, year, month, u.intval(u.get("p"))) } else { - redirectf(`/%d/%d/%d`, hubid, year, month) + u.redirectf(`/%d/%d/%d`, hubid, year, month) } } else { - if hi := this.lookupSourceByNumericString(get("h")); hi != nil { + if hi := this.lookupSourceByNumericString(u.get("h")); hi != nil { currentDate := hi.LatestDate() - redirectf(`/%d/%d/%d`, hubid, currentDate.Year, currentDate.Month) + u.redirectf(`/%d/%d/%d`, hubid, currentDate.Year, currentDate.Month) } else { - redirectf(`/`) + u.redirectf(`/`) } } diff --git a/util.go b/util.go index b1b035a..715fe84 100644 --- a/util.go +++ b/util.go @@ -1,5 +1,11 @@ package archive +import ( + "fmt" + "net/http" + "strconv" +) + func attr(condition bool, whenMet string) string { if condition { return whenMet @@ -7,3 +13,25 @@ func attr(condition bool, whenMet string) string { return "" } } + +type URLHelper struct { + w http.ResponseWriter + r *http.Request +} + +func (this *URLHelper) intval(sz string) int { + ret, _ := strconv.Atoi(sz) + return ret +} + +func (this *URLHelper) get(sz string) string { + return this.r.URL.Query().Get(sz) +} + +func (this *URLHelper) hasGet(sz string) bool { + return len(this.r.URL.Query().Get(sz)) > 0 +} + +func (this *URLHelper) redirectf(format string, a ...interface{}) { + http.Redirect(this.w, this.r, fmt.Sprintf(format, a...), http.StatusTemporaryRedirect) +}