diff --git a/WikiServer.go b/WikiServer.go
index 7d5b424..645b7d8 100644
--- a/WikiServer.go
+++ b/WikiServer.go
@@ -1,9 +1,11 @@
package yatwiki3
import (
+ "database/sql"
"html/template"
"log"
"net/http"
+ "strings"
)
type WikiServer struct {
@@ -38,21 +40,93 @@ func (this *WikiServer) Close() {
func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "Yatwiki3")
- if r.URL.Path == this.opts.ExpectBaseURL+"wiki.css" {
- w.Header().Set("Content-Type", "text/css")
- w.Write(tmplWikiCss)
- return
+ if r.Method == "GET" {
+ if r.URL.Path == this.opts.ExpectBaseURL+"wiki.css" {
+ w.Header().Set("Content-Type", "text/css")
+ 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 {
w.Header().Set("Content-Type", "image/x-icon")
http.ServeFile(w, r, this.opts.FaviconFilePath)
return
+
+ } else if r.URL.Path == this.opts.ExpectBaseURL+"formatting" {
+ pto := DefaultPageTemplateOptions(this.opts)
+ pto.CurrentPageName = "Formatting help"
+ pto.Content = `
+
Formatting help
+
+ - [h]header[/h]
+ - [b]bold[/b]
+ - [u]underline[/u]
+ - [i]italic[/i]
+ - [s]strikethrough[/s]
+ - [spoiler]spoiler[/spoiler]
+ - [list] item [*] item [/list]
+ - [url=address]title[/url]
+ - [article=page name]title[/article] or [rev=id]title[/rev]
+ - [img]image-url[/img]
+ - [imgur]asdf.jpg[/imgur]
+ - [code]fixed width[/code]
+ - [section=header]content[/section]
+ - [html]raw html[/html]
+
`
+ 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)
- pto.SessionMessage = `Invalid request.`
- //pto.CurrentPageIsArticle = true
- //pto.CurrentPageName = "quotes/\"2017"
- this.servePageResponse(w, pto)
+ // No match? Add 'Page not found' to next session response, and redirect to homepage
+ http.Error(w, "Page not found", 404)
+
+ /*
+ 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] `)
+
+ 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) {