implement 'article index' page, better error handling
This commit is contained in:
parent
db1ac7c3ad
commit
19b8fdb504
@ -2,9 +2,12 @@ package yatwiki3
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -79,7 +82,32 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
<li>[section=header]content[/section]</li>
|
<li>[section=header]content[/section]</li>
|
||||||
<li>[html]raw html[/html]</li>
|
<li>[html]raw html[/html]</li>
|
||||||
</ul>`
|
</ul>`
|
||||||
this.servePageResponse(w, pto)
|
this.servePageResponse(w, r, pto)
|
||||||
|
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)
|
||||||
return
|
return
|
||||||
|
|
||||||
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"view/") {
|
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"view/") {
|
||||||
@ -91,6 +119,11 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
a, err := this.db.GetLatestVersion(articleTitle)
|
a, err := this.db.GetLatestVersion(articleTitle)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
this.serveErrorHTMLMessage(w, this.noSuchArticleError(articleTitle))
|
||||||
|
return
|
||||||
|
}
|
||||||
this.serveErrorMessage(w, err)
|
this.serveErrorMessage(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -103,7 +136,7 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
pto.Content = bcr.RenderHTML(string(a.Body))
|
pto.Content = bcr.RenderHTML(string(a.Body))
|
||||||
pto.LoadCodeResources = bcr.CodePresent
|
pto.LoadCodeResources = bcr.CodePresent
|
||||||
|
|
||||||
this.servePageResponse(w, pto)
|
this.servePageResponse(w, r, pto)
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -111,30 +144,31 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// No match? Add 'Page not found' to next session response, and redirect to homepage
|
// No match? Add 'Page not found' to next session response, and redirect to homepage
|
||||||
http.Error(w, "Page not found", 404)
|
this.serveErrorMessage(w, errors.New("Page not found"))
|
||||||
|
|
||||||
/*
|
}
|
||||||
pto.SessionMessage = `Invalid request.`
|
|
||||||
//pto.CurrentPageIsArticle = true
|
|
||||||
//pto.CurrentPageName = "quotes/\"2017"
|
|
||||||
|
|
||||||
bcr := NewBBCodeRenderer(this.opts.ExpectBaseURL)
|
func (this *WikiServer) noSuchArticleError(title string) template.HTML {
|
||||||
pto.Content = bcr.RenderHTML(`[h]Hello World[/h] this content is [b]bold[/b] <script>alert("hi");</script>`)
|
return template.HTML(`No such article exists. <a href="` + this.opts.ExpectBaseURL + `modify/` + template.HTMLEscapeString(url.QueryEscape(title)) + `">Click here</a> to create it.`)
|
||||||
|
|
||||||
this.servePageResponse(w, pto)
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *WikiServer) serveErrorMessage(w http.ResponseWriter, message error) {
|
func (this *WikiServer) serveErrorMessage(w http.ResponseWriter, message error) {
|
||||||
if message == sql.ErrNoRows {
|
this.serveErrorHTMLMessage(w, template.HTML(template.HTMLEscapeString(message.Error())))
|
||||||
// 404 not found
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Add error message to session response, and redirect to homepage
|
func (this *WikiServer) serveInternalError(w http.ResponseWriter, r *http.Request, e error) {
|
||||||
http.Error(w, message.Error(), 500)
|
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) servePageResponse(w http.ResponseWriter, pto *pageTemplateOptions) {
|
func (this *WikiServer) serveErrorHTMLMessage(w http.ResponseWriter, msg template.HTML) {
|
||||||
|
w.Header().Set("Location", this.opts.ExpectBaseURL+"view/"+url.QueryEscape(this.opts.DefaultPage)+"?error="+url.QueryEscape(string(msg)))
|
||||||
|
w.WriteHeader(302) // moved (not permanently)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *WikiServer) servePageResponse(w http.ResponseWriter, r *http.Request, pto *pageTemplateOptions) {
|
||||||
|
pto.SessionMessage = template.HTML(r.URL.Query().Get("error")) // FIXME reflected XSS (although Chrome automatically blocks it..)
|
||||||
|
|
||||||
err := this.pageTmp.Execute(w, pto)
|
err := this.pageTmp.Execute(w, pto)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
|
Loading…
Reference in New Issue
Block a user