simplify router

This commit is contained in:
mappu 2017-07-11 18:36:08 +12:00
parent 6ba86ca170
commit 3a421829f1

View File

@ -57,41 +57,43 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Bad request", 400)
return
}
remainingPath := r.URL.Path[len(this.opts.ExpectBaseURL):]
if r.Method == "GET" {
if r.URL.Path == this.opts.ExpectBaseURL+"wiki.css" {
if remainingPath == "wiki.css" {
w.Header().Set("Content-Type", "text/css")
content, _ := wikiCssBytes()
w.Write(content)
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"highlight.js" {
} else if remainingPath == "highlight.js" {
w.Header().Set("Content-Type", "application/javascript")
content, _ := highlightJsBytes()
w.Write(content)
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"favicon.ico" && len(this.opts.FaviconFilePath) > 0 {
} else if remainingPath == "favicon.ico" && len(this.opts.FaviconFilePath) > 0 {
w.Header().Set("Content-Type", "image/x-icon")
http.ServeFile(w, r, this.opts.FaviconFilePath)
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"download-database" {
} else if remainingPath == "download-database" {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", `attachment; filename="database-`+fmt.Sprintf("%d", time.Now().Unix())+`.db"`)
http.ServeFile(w, r, this.opts.DBFilePath)
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"formatting" {
} else if remainingPath == "formatting" {
this.routeFormatting(w, r)
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"index" {
} else if remainingPath == "index" {
this.routeIndex(w, r)
return
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"view/") {
articleTitle, err := url.QueryUnescape(r.URL.Path[len(this.opts.ExpectBaseURL+"view/"):])
} else if strings.HasPrefix(remainingPath, "view/") {
articleTitle, err := url.QueryUnescape(remainingPath[len("view/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
@ -99,8 +101,8 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.routeView(w, r, articleTitle)
return
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"modify/") {
articleTitle, err := url.QueryUnescape(r.URL.Path[len(this.opts.ExpectBaseURL+"modify/"):])
} else if strings.HasPrefix(remainingPath, "modify/") {
articleTitle, err := url.QueryUnescape(remainingPath[len("modify/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
@ -108,8 +110,8 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.routeModify(w, r, articleTitle)
return
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"history/") {
articleTitle, err := url.QueryUnescape(r.URL.Path[len(this.opts.ExpectBaseURL+"history/"):])
} else if strings.HasPrefix(remainingPath, "history/") {
articleTitle, err := url.QueryUnescape(remainingPath[len("history/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
@ -117,8 +119,8 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.routeHistory(w, r, articleTitle)
return
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"raw/") {
revId, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"raw/"):])
} else if strings.HasPrefix(remainingPath, "raw/") {
revId, err := strconv.Atoi(remainingPath[len("raw/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
@ -127,8 +129,8 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.routeRawView(w, r, revId)
return
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"archive/") {
revId, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"archive/"):])
} else if strings.HasPrefix(remainingPath, "archive/") {
revId, err := strconv.Atoi(remainingPath[len("archive/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
@ -137,8 +139,8 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.routeArchive(w, r, revId)
return
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"recent/") {
pageNum, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"recent/"):])
} else if strings.HasPrefix(remainingPath, "recent/") {
pageNum, err := strconv.Atoi(remainingPath[len("recent/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
@ -147,11 +149,11 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.routeRecentChanges(w, r, pageNum)
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"diff" {
} else if remainingPath == "diff" {
this.serveRedirect(w, this.opts.ExpectBaseURL+`diff/`+r.URL.Query().Get("f")+`/`+r.URL.Query().Get("t"))
return
} else if match := this.rxDiff.FindStringSubmatch(r.URL.Path[len(this.opts.ExpectBaseURL):]); len(match) == 3 {
} else if match := this.rxDiff.FindStringSubmatch(remainingPath); len(match) == 3 {
fromRev, err := strconv.Atoi(match[1])
if err != nil {