refactor all routes out to separate files

This commit is contained in:
mappu 2017-07-09 13:26:26 +12:00
parent cceffc93ad
commit d1ff4c294d
6 changed files with 171 additions and 121 deletions

View File

@ -1,11 +1,8 @@
package yatwiki3
import (
"database/sql"
"errors"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"strconv"
@ -63,52 +60,11 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"formatting" {
pto := DefaultPageTemplateOptions(this.opts)
pto.CurrentPageName = "Formatting help"
pto.Content = `
<h2>Formatting help</h2><br><br>
<ul>
<li>[h]header[/h]</li>
<li>[b]bold[/b]</li>
<li>[u]underline[/u]</li>
<li>[i]italic[/i]</li>
<li>[s]strikethrough[/s]</li>
<li>[spoiler]spoiler[/spoiler]</li>
<li>[list] item [*] item [/list]</li>
<li>[url=address]title[/url]</li>
<li>[article=page name]title[/article] or [rev=id]title[/rev]</li>
<li>[img]image-url[/img]</li>
<li>[imgur]asdf.jpg[/imgur]</li>
<li>[code]fixed width[/code]</li>
<li>[section=header]content[/section]</li>
<li>[html]raw html[/html]</li>
</ul>`
this.servePageResponse(w, r, pto)
this.routeFormatting(w, r)
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"index" {
titles, err := this.db.ListTitles()
if err != nil {
this.serveInternalError(w, r, err)
return
}
totalRevs, err := this.db.TotalRevisions()
if err != nil {
this.serveInternalError(w, r, err)
return
}
content := fmt.Sprintf(`<h2>Article Index</h2><br><em>There are %d edits to %d pages.</em><br><br><ul>`, totalRevs, len(titles))
for _, title := range titles {
content += `<li><a href="` + template.HTMLEscapeString(this.opts.ExpectBaseURL+`view/`+url.QueryEscape(title)) + `">` + template.HTMLEscapeString(title) + `</a></li>`
}
content += `</ul>`
pto := DefaultPageTemplateOptions(this.opts)
pto.CurrentPageName = "Index"
pto.Content = template.HTML(content)
this.servePageResponse(w, r, pto)
this.routeIndex(w, r)
return
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"view/") {
@ -117,35 +73,7 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.serveErrorMessage(w, err)
return
}
a, err := this.db.GetLatestVersion(articleTitle)
if err != nil {
if err == sql.ErrNoRows {
// Yatwiki2 always required a trailing slash at the end of the URL
// If this was an old link, it might not be present.
// Redirect if possible
if len(articleTitle) > 0 && articleTitle[len(articleTitle)-1] == '/' {
this.serveRedirect(w, this.opts.ExpectBaseURL+"view/"+url.QueryEscape(articleTitle[0:len(articleTitle)-1]))
return
}
this.serveErrorHTMLMessage(w, this.noSuchArticleError(articleTitle))
return
}
this.serveErrorMessage(w, err)
return
}
pto := DefaultPageTemplateOptions(this.opts)
pto.CurrentPageName = articleTitle
pto.CurrentPageIsArticle = true
bcr := NewBBCodeRenderer(this.opts.ExpectBaseURL)
pto.Content = bcr.RenderHTML(string(a.Body))
pto.LoadCodeResources = bcr.CodePresent
this.servePageResponse(w, r, pto)
this.routeView(w, r, articleTitle)
return
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"raw/") {
@ -155,20 +83,7 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
a, err := this.db.GetRevision(revId)
if err != nil {
if err == sql.ErrNoRows {
this.serveErrorMessage(w, errors.New("No such revision."))
return
}
this.serveErrorMessage(w, err)
return
}
w.Header().Set(`Content-Type`, `text/plain; charset=UTF-8`)
w.WriteHeader(200)
w.Write(a.Body)
this.routeRawView(w, r, revId)
return
}
@ -178,35 +93,3 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.serveErrorMessage(w, errors.New("Page not found"))
}
func (this *WikiServer) noSuchArticleError(title string) template.HTML {
return template.HTML(`No such article exists. <a href="` + this.opts.ExpectBaseURL + `modify/` + template.HTMLEscapeString(url.QueryEscape(title)) + `">Click here</a> to create it.`)
}
func (this *WikiServer) serveErrorMessage(w http.ResponseWriter, message error) {
this.serveErrorHTMLMessage(w, template.HTML(template.HTMLEscapeString(message.Error())))
}
func (this *WikiServer) serveInternalError(w http.ResponseWriter, r *http.Request, e error) {
log.Printf("Internal error from %s while accessing %s(%s): %s", r.RemoteAddr, r.Method, r.URL.Path, e.Error())
http.Error(w, "An internal error occurred. Please ask an administrator to check the log file.", 500)
}
func (this *WikiServer) serveErrorHTMLMessage(w http.ResponseWriter, msg template.HTML) {
this.serveRedirect(w, this.opts.ExpectBaseURL+"view/"+url.QueryEscape(this.opts.DefaultPage)+"?error="+url.QueryEscape(string(msg)))
}
func (this *WikiServer) serveRedirect(w http.ResponseWriter, location string) {
w.Header().Set("Location", location)
w.WriteHeader(302) // moved (not permanently)
}
func (this *WikiServer) servePageResponse(w http.ResponseWriter, r *http.Request, pto *pageTemplateOptions) {
w.WriteHeader(200)
pto.SessionMessage = template.HTML(r.URL.Query().Get("error")) // FIXME reflected XSS (although Chrome automatically blocks it..)
err := this.pageTmp.Execute(w, pto)
if err != nil {
log.Println(err.Error())
}
}

40
rErrors.go Normal file
View File

@ -0,0 +1,40 @@
package yatwiki3
import (
"html/template"
"log"
"net/http"
"net/url"
)
func (this *WikiServer) noSuchArticleError(title string) template.HTML {
return template.HTML(`No such article exists. <a href="` + this.opts.ExpectBaseURL + `modify/` + template.HTMLEscapeString(url.QueryEscape(title)) + `">Click here</a> to create it.`)
}
func (this *WikiServer) serveErrorMessage(w http.ResponseWriter, message error) {
this.serveErrorHTMLMessage(w, template.HTML(template.HTMLEscapeString(message.Error())))
}
func (this *WikiServer) serveInternalError(w http.ResponseWriter, r *http.Request, e error) {
log.Printf("Internal error from %s while accessing %s(%s): %s", r.RemoteAddr, r.Method, r.URL.Path, e.Error())
http.Error(w, "An internal error occurred. Please ask an administrator to check the log file.", 500)
}
func (this *WikiServer) serveErrorHTMLMessage(w http.ResponseWriter, msg template.HTML) {
this.serveRedirect(w, this.opts.ExpectBaseURL+"view/"+url.QueryEscape(this.opts.DefaultPage)+"?error="+url.QueryEscape(string(msg)))
}
func (this *WikiServer) serveRedirect(w http.ResponseWriter, location string) {
w.Header().Set("Location", location)
w.WriteHeader(302) // moved (not permanently)
}
func (this *WikiServer) servePageResponse(w http.ResponseWriter, r *http.Request, pto *pageTemplateOptions) {
w.WriteHeader(200)
pto.SessionMessage = template.HTML(r.URL.Query().Get("error")) // FIXME reflected XSS (although Chrome automatically blocks it..)
err := this.pageTmp.Execute(w, pto)
if err != nil {
log.Println(err.Error())
}
}

29
rFormatting.go Normal file
View File

@ -0,0 +1,29 @@
package yatwiki3
import (
"net/http"
)
func (this *WikiServer) routeFormatting(w http.ResponseWriter, r *http.Request) {
pto := DefaultPageTemplateOptions(this.opts)
pto.CurrentPageName = "Formatting help"
pto.Content = `
<h2>Formatting help</h2><br><br>
<ul>
<li>[h]header[/h]</li>
<li>[b]bold[/b]</li>
<li>[u]underline[/u]</li>
<li>[i]italic[/i]</li>
<li>[s]strikethrough[/s]</li>
<li>[spoiler]spoiler[/spoiler]</li>
<li>[list] item [*] item [/list]</li>
<li>[url=address]title[/url]</li>
<li>[article=page name]title[/article] or [rev=id]title[/rev]</li>
<li>[img]image-url[/img]</li>
<li>[imgur]asdf.jpg[/imgur]</li>
<li>[code]fixed width[/code]</li>
<li>[section=header]content[/section]</li>
<li>[html]raw html[/html]</li>
</ul>`
this.servePageResponse(w, r, pto)
}

34
rIndex.go Normal file
View File

@ -0,0 +1,34 @@
package yatwiki3
import (
"fmt"
"html/template"
"net/http"
"net/url"
)
func (this *WikiServer) routeIndex(w http.ResponseWriter, r *http.Request) {
titles, err := this.db.ListTitles()
if err != nil {
this.serveInternalError(w, r, err)
return
}
totalRevs, err := this.db.TotalRevisions()
if err != nil {
this.serveInternalError(w, r, err)
return
}
content := fmt.Sprintf(`<h2>Article Index</h2><br><em>There are %d edits to %d pages.</em><br><br><ul>`, totalRevs, len(titles))
for _, title := range titles {
content += `<li><a href="` + template.HTMLEscapeString(this.opts.ExpectBaseURL+`view/`+url.QueryEscape(title)) + `">` + template.HTMLEscapeString(title) + `</a></li>`
}
content += `</ul>`
pto := DefaultPageTemplateOptions(this.opts)
pto.CurrentPageName = "Index"
pto.Content = template.HTML(content)
this.servePageResponse(w, r, pto)
return
}

25
rRawView.go Normal file
View File

@ -0,0 +1,25 @@
package yatwiki3
import (
"database/sql"
"errors"
"net/http"
)
func (this *WikiServer) routeRawView(w http.ResponseWriter, r *http.Request, revId int) {
a, err := this.db.GetRevision(revId)
if err != nil {
if err == sql.ErrNoRows {
this.serveErrorMessage(w, errors.New("No such revision."))
return
}
this.serveErrorMessage(w, err)
return
}
w.Header().Set(`Content-Type`, `text/plain; charset=UTF-8`)
w.WriteHeader(200)
w.Write(a.Body)
return
}

39
rView.go Normal file
View File

@ -0,0 +1,39 @@
package yatwiki3
import (
"database/sql"
"net/http"
"net/url"
)
func (this *WikiServer) routeView(w http.ResponseWriter, r *http.Request, articleTitle string) {
a, err := this.db.GetLatestVersion(articleTitle)
if err != nil {
if err == sql.ErrNoRows {
// Yatwiki2 always required a trailing slash at the end of the URL
// If this was an old link, it might not be present.
// Redirect if possible
if len(articleTitle) > 0 && articleTitle[len(articleTitle)-1] == '/' {
this.serveRedirect(w, this.opts.ExpectBaseURL+"view/"+url.QueryEscape(articleTitle[0:len(articleTitle)-1]))
return
}
this.serveErrorHTMLMessage(w, this.noSuchArticleError(articleTitle))
return
}
this.serveErrorMessage(w, err)
return
}
pto := DefaultPageTemplateOptions(this.opts)
pto.CurrentPageName = articleTitle
pto.CurrentPageIsArticle = true
bcr := NewBBCodeRenderer(this.opts.ExpectBaseURL)
pto.Content = bcr.RenderHTML(string(a.Body))
pto.LoadCodeResources = bcr.CodePresent
this.servePageResponse(w, r, pto)
return
}