implement /random redirect

This commit is contained in:
mappu 2017-07-11 18:39:59 +12:00
parent 308aa11bf0
commit 9a6321fe6f

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"html/template"
"math/rand"
"net/http"
"net/url"
"regexp"
@ -95,6 +96,17 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.serveRedirect(w, this.opts.ExpectBaseURL+`view/`+url.QueryEscape(this.opts.DefaultPage))
return
} else if remainingPath == "random" {
titles, err := this.db.ListTitles()
if err != nil {
this.serveInternalError(w, r, err)
return
}
chosenArticle := titles[rand.Intn(len(titles))]
this.serveRedirect(w, this.opts.ExpectBaseURL+`view/`+url.QueryEscape(chosenArticle))
return
} else if strings.HasPrefix(remainingPath, "view/") {
articleTitle, err := url.QueryUnescape(remainingPath[len("view/"):])
if err != nil {