routing for assets, page views, formatting help

This commit is contained in:
mappu 2017-07-09 12:15:49 +12:00
parent ced027c57d
commit 4bcc7f5dfa

View File

@ -1,9 +1,11 @@
package yatwiki3 package yatwiki3
import ( import (
"database/sql"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"strings"
) )
type WikiServer struct { type WikiServer struct {
@ -38,21 +40,93 @@ func (this *WikiServer) Close() {
func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "Yatwiki3") w.Header().Set("Server", "Yatwiki3")
if r.URL.Path == this.opts.ExpectBaseURL+"wiki.css" { if r.Method == "GET" {
w.Header().Set("Content-Type", "text/css") if r.URL.Path == this.opts.ExpectBaseURL+"wiki.css" {
w.Write(tmplWikiCss) w.Header().Set("Content-Type", "text/css")
return content, _ := wikiCssBytes()
w.Write(content)
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"highlight.js" {
w.Header().Set("Content-Type", "application/javascript")
content, _ := highlightJsBytes()
w.Write(content)
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"favicon.ico" && len(this.opts.FaviconFilePath) > 0 { } else if r.URL.Path == this.opts.ExpectBaseURL+"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+"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, pto)
return
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"view/") {
articleTitle := r.URL.Path[len(this.opts.ExpectBaseURL+"view/"):]
a, err := this.db.GetLatestVersion(articleTitle)
if err != nil {
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, pto)
return
}
} }
pto := DefaultPageTemplateOptions(this.opts) // No match? Add 'Page not found' to next session response, and redirect to homepage
pto.SessionMessage = `Invalid request.` http.Error(w, "Page not found", 404)
//pto.CurrentPageIsArticle = true
//pto.CurrentPageName = "quotes/\"2017" /*
this.servePageResponse(w, pto) pto.SessionMessage = `Invalid request.`
//pto.CurrentPageIsArticle = true
//pto.CurrentPageName = "quotes/\"2017"
bcr := NewBBCodeRenderer(this.opts.ExpectBaseURL)
pto.Content = bcr.RenderHTML(`[h]Hello World[/h] this content is [b]bold[/b] <script>alert("hi");</script>`)
this.servePageResponse(w, pto)
*/
}
func (this *WikiServer) serveErrorMessage(w http.ResponseWriter, message error) {
if message == sql.ErrNoRows {
// 404 not found
}
// TODO Add error message to session response, and redirect to homepage
http.Error(w, message.Error(), 500)
} }
func (this *WikiServer) servePageResponse(w http.ResponseWriter, pto *pageTemplateOptions) { func (this *WikiServer) servePageResponse(w http.ResponseWriter, pto *pageTemplateOptions) {