120 lines
2.5 KiB
Go
120 lines
2.5 KiB
Go
package archive
|
|
|
|
import (
|
|
"errors"
|
|
"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) bestSlugFor(ls *LogSource) (string, error) {
|
|
for i := 0; i < len(this.cfg.Logs); i += 1 {
|
|
if ls != &this.cfg.Logs[i] {
|
|
continue
|
|
}
|
|
|
|
if len(this.cfg.Logs[i].Slugs) > 0 {
|
|
return this.cfg.Logs[i].Slugs[0], nil // first slug is preferred
|
|
} else {
|
|
return fmt.Sprintf(`%d`, i), nil // no slugs present? use numeric log index
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.New("Unregistered log source")
|
|
}
|
|
|
|
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(`/`)
|
|
}
|
|
|
|
}
|
|
}
|