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