2017-07-08 23:13:36 +00:00
|
|
|
package yatwiki3
|
|
|
|
|
|
|
|
import (
|
2017-07-09 01:00:45 +00:00
|
|
|
"errors"
|
2017-07-09 06:11:39 +00:00
|
|
|
"fmt"
|
2017-07-08 23:13:36 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2017-07-09 01:00:45 +00:00
|
|
|
"net/url"
|
2017-07-11 06:31:50 +00:00
|
|
|
"regexp"
|
2017-07-09 01:18:18 +00:00
|
|
|
"strconv"
|
2017-07-09 00:15:49 +00:00
|
|
|
"strings"
|
2017-07-09 05:20:10 +00:00
|
|
|
"time"
|
2017-07-08 23:13:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WikiServer struct {
|
|
|
|
db *WikiDB
|
|
|
|
opts *ServerOptions
|
|
|
|
pageTmp *template.Template
|
2017-07-09 05:20:10 +00:00
|
|
|
loc *time.Location
|
2017-07-11 06:31:50 +00:00
|
|
|
rxDiff *regexp.Regexp
|
2017-07-08 23:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewWikiServer(opts *ServerOptions) (*WikiServer, error) {
|
2017-07-09 06:45:28 +00:00
|
|
|
wdb, err := NewWikiDB(opts.DBFilePath, opts.GzipCompressionLevel)
|
2017-07-08 23:13:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New("yatwiki/page").Parse(pageTemplate)
|
|
|
|
if err != nil {
|
2017-07-09 05:20:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
loc, err := time.LoadLocation(opts.Timezone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-07-08 23:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ws := WikiServer{
|
|
|
|
db: wdb,
|
|
|
|
opts: opts,
|
|
|
|
pageTmp: tmpl,
|
2017-07-09 05:20:10 +00:00
|
|
|
loc: loc,
|
2017-07-11 06:31:50 +00:00
|
|
|
rxDiff: regexp.MustCompile(`diff/(\d+)/(\d+)`),
|
2017-07-08 23:13:36 +00:00
|
|
|
}
|
|
|
|
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-11 06:31:50 +00:00
|
|
|
if !strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL) {
|
|
|
|
http.Error(w, "Bad request", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-07-09 06:11:39 +00:00
|
|
|
} else if r.URL.Path == this.opts.ExpectBaseURL+"download-database" {
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
w.Header().Set("Content-Disposition", `attachment; filename="database-`+fmt.Sprintf("%d", time.Now().Unix())+`.db"`)
|
|
|
|
http.ServeFile(w, r, this.opts.DBFilePath)
|
|
|
|
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 06:05:03 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"history/") {
|
|
|
|
articleTitle, err := url.QueryUnescape(r.URL.Path[len(this.opts.ExpectBaseURL+"history/"):])
|
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.routeHistory(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 05:20:10 +00:00
|
|
|
|
|
|
|
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"archive/") {
|
|
|
|
revId, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"archive/"):])
|
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.routeArchive(w, r, revId)
|
|
|
|
return
|
|
|
|
|
2017-07-09 06:05:03 +00:00
|
|
|
} else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"recent/") {
|
|
|
|
pageNum, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"recent/"):])
|
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.routeRecentChanges(w, r, pageNum)
|
|
|
|
return
|
|
|
|
|
2017-07-11 05:52:29 +00:00
|
|
|
} else if r.URL.Path == this.opts.ExpectBaseURL+"diff" {
|
2017-07-11 06:31:50 +00:00
|
|
|
this.serveRedirect(w, this.opts.ExpectBaseURL+`diff/`+r.URL.Query().Get("f")+`/`+r.URL.Query().Get("t"))
|
|
|
|
return
|
|
|
|
|
|
|
|
} else if match := this.rxDiff.FindStringSubmatch(r.URL.Path[len(this.opts.ExpectBaseURL):]); len(match) == 3 {
|
2017-07-11 05:52:29 +00:00
|
|
|
|
2017-07-11 06:31:50 +00:00
|
|
|
fromRev, err := strconv.Atoi(match[1])
|
2017-07-11 05:52:29 +00:00
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-11 06:31:50 +00:00
|
|
|
toRev, err := strconv.Atoi(match[2])
|
2017-07-11 05:52:29 +00:00
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.routeDiff(w, r, fromRev, toRev)
|
|
|
|
return
|
|
|
|
|
2017-07-09 00:15:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 06:45:28 +00:00
|
|
|
} else if r.Method == "POST" {
|
|
|
|
|
|
|
|
if r.URL.Path == this.opts.ExpectBaseURL+"save" {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
title := r.Form.Get("pname")
|
|
|
|
body := r.Form.Get("content")
|
|
|
|
expectRev, err := strconv.Atoi(r.Form.Get("baserev"))
|
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = this.db.SaveArticle(title, Author(r), body, int64(expectRev))
|
|
|
|
if err != nil {
|
|
|
|
this.serveErrorMessage(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.serveRedirect(w, this.opts.ExpectBaseURL+`view/`+url.QueryEscape(title))
|
|
|
|
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
|
|
|
}
|