From 9a6321fe6f54b78d11b037bbebb88a8dd1d773d3 Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 11 Jul 2017 18:39:59 +1200 Subject: [PATCH] implement /random redirect --- WikiServer.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/WikiServer.go b/WikiServer.go index ed58522..71ad7c8 100644 --- a/WikiServer.go +++ b/WikiServer.go @@ -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 {