2017-07-08 23:13:36 +00:00
|
|
|
package yatwiki3
|
|
|
|
|
|
|
|
import (
|
2017-07-09 01:00:45 +00:00
|
|
|
"errors"
|
2017-07-08 23:13:36 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2017-07-09 01:00:45 +00:00
|
|
|
"net/url"
|
2017-07-09 01:18:18 +00:00
|
|
|
"strconv"
|
2017-07-09 00:15:49 +00:00
|
|
|
"strings"
|
2017-07-08 23:13:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WikiServer struct {
|
|
|
|
db *WikiDB
|
|
|
|
opts *ServerOptions
|
|
|
|
pageTmp *template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWikiServer(opts *ServerOptions) (*WikiServer, error) {
|
|
|
|
wdb, err := NewWikiDB(opts.DBFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New("yatwiki/page").Parse(pageTemplate)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := WikiServer{
|
|
|
|
db: wdb,
|
|
|
|
opts: opts,
|
|
|
|
pageTmp: tmpl,
|
|
|
|
}
|
|
|
|
return &ws, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *WikiServer) Close() {
|
|
|
|
this.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-07-09 01:26:38 +00:00
|
|
|
w.Header().Set("Server", "YATWiki3")
|
2017-07-08 23:13:36 +00:00
|
|
|
|
2017-07-09 00:15:49 +00:00
|
|
|
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
|
|
|
|
|
2017-07-09 00:14:28 +00:00
|
|
|
} 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
|
2017-07-09 00:15:49 +00:00
|
|
|
|
|
|
|
} else if r.URL.Path == this.opts.ExpectBaseURL+"formatting" {
|
2017-07-09 01:26:26 +00:00
|
|
|
this.routeFormatting(w, r)
|
2017-07-09 01:00:45 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
} else if r.URL.Path == this.opts.ExpectBaseURL+"index" {
|
2017-07-09 01:26:26 +00:00
|
|
|
this.routeIndex(w, r)
|
2017-07-09 00:15:49 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"view/") {
|
2017-07-09 01:00:11 +00:00
|
|
|
articleTitle, err := url.QueryUnescape(r.URL.Path[len(this.opts.ExpectBaseURL+"view/"):])
|
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
2017-07-09 01:26:26 +00:00
|
|
|
this.routeView(w, r, articleTitle)
|
2017-07-09 00:15:49 +00:00
|
|
|
return
|
|
|
|
|
2017-07-09 01:50:13 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"modify/") {
|
|
|
|
articleTitle, err := url.QueryUnescape(r.URL.Path[len(this.opts.ExpectBaseURL+"modify/"):])
|
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.routeModify(w, r, articleTitle)
|
|
|
|
return
|
|
|
|
|
2017-07-09 01:18:18 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"raw/") {
|
|
|
|
revId, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"raw/"):])
|
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-09 01:26:26 +00:00
|
|
|
this.routeRawView(w, r, revId)
|
2017-07-09 01:18:18 +00:00
|
|
|
return
|
2017-07-09 00:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// No match? Add 'Page not found' to next session response, and redirect to homepage
|
2017-07-09 01:00:45 +00:00
|
|
|
this.serveErrorMessage(w, errors.New("Page not found"))
|
2017-07-09 00:15:49 +00:00
|
|
|
|
2017-07-09 01:00:45 +00:00
|
|
|
}
|