archive/Router.go

102 lines
2.1 KiB
Go
Raw Normal View History

2017-08-13 01:57:08 +00:00
package archive
import (
"fmt"
"net/http"
"net/url"
"strconv"
)
func (this *ArchiveServer) lookupSourceByNumericString(slug string) *LogSource {
intval, err := strconv.Atoi(slug)
if err != nil {
return nil
}
if intval >= 0 && intval < len(this.cfg.Logs) {
return &this.cfg.Logs[intval]
}
return nil
}
func (this *ArchiveServer) lookupSource(slug string) *LogSource {
if src := this.lookupSourceByNumericString(slug); src != nil {
return src
}
for i, ls := range this.cfg.Logs {
for _, s := range ls.Slugs {
if s == slug {
return &this.cfg.Logs[i]
}
}
}
return nil // not found
}
func (this *ArchiveServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", SERVER_VERSION)
if r.Method != "GET" {
http.Error(w, "Expected GET", http.StatusMethodNotAllowed)
return
}
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
}
}
func (this *ArchiveServer) legacyRoute(w http.ResponseWriter, r *http.Request) {
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 hasGet("h") {
hubid = intval(get("h"))
}
if hasGet("q") {
if hasGet("rx") {
redirectf(`/%d/rx/%s`, hubid, url.QueryEscape(get("q")))
} else {
redirectf(`/%d/search/%s`, hubid, url.QueryEscape(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 {
redirectf(`/%d/%d/%d`, hubid, year, month)
}
} else {
if hi := this.lookupSourceByNumericString(get("h")); hi != nil {
currentDate := hi.LatestDate()
redirectf(`/%d/%d/%d`, hubid, currentDate.Year, currentDate.Month)
} else {
redirectf(`/`)
}
}
}