yatwiki/WikiServer.go

223 lines
5.2 KiB
Go
Raw Normal View History

2017-07-08 23:13:36 +00:00
package yatwiki3
import (
"errors"
2017-07-09 06:11:39 +00:00
"fmt"
2017-07-08 23:13:36 +00:00
"html/template"
2017-07-11 06:39:59 +00:00
"math/rand"
2017-07-08 23:13:36 +00:00
"net/http"
"net/url"
2017-07-11 06:31:50 +00:00
"regexp"
2017-07-09 01:18:18 +00:00
"strconv"
"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) {
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) {
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-11 06:36:08 +00:00
remainingPath := r.URL.Path[len(this.opts.ExpectBaseURL):]
2017-07-11 06:31:50 +00:00
if r.Method == "GET" {
2017-07-11 06:36:08 +00:00
if remainingPath == "wiki.css" {
w.Header().Set("Content-Type", "text/css")
content, _ := wikiCssBytes()
w.Write(content)
return
2017-07-11 06:36:08 +00:00
} else if remainingPath == "highlight.js" {
w.Header().Set("Content-Type", "application/javascript")
content, _ := highlightJsBytes()
w.Write(content)
return
2017-07-11 06:36:08 +00:00
} else if remainingPath == "favicon.ico" && len(this.opts.FaviconFilePath) > 0 {
2017-07-09 00:14:28 +00:00
w.Header().Set("Content-Type", "image/x-icon")
http.ServeFile(w, r, this.opts.FaviconFilePath)
return
2017-07-11 06:36:08 +00:00
} else if remainingPath == "download-database" {
2017-07-09 06:11:39 +00:00
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-11 06:36:08 +00:00
} else if remainingPath == "formatting" {
this.routeFormatting(w, r)
return
2017-07-11 06:36:08 +00:00
} else if remainingPath == "index" {
this.routeIndex(w, r)
return
2017-07-11 06:36:14 +00:00
} else if remainingPath == "" {
this.serveRedirect(w, this.opts.ExpectBaseURL+`view/`+url.QueryEscape(this.opts.DefaultPage))
return
2017-07-11 06:36:08 +00:00
2017-07-11 06:39:59 +00:00
} else if remainingPath == "random" {
titles, err := this.db.ListTitles()
if err != nil {
this.serveInternalError(w, r, err)
return
}
chosenArticle := titles[rand.Intn(len(titles))]
this.serveRedirect(w, this.opts.ExpectBaseURL+`view/`+url.QueryEscape(chosenArticle))
return
2017-07-11 06:36:08 +00:00
} else if strings.HasPrefix(remainingPath, "view/") {
articleTitle, err := url.QueryUnescape(remainingPath[len("view/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
}
this.routeView(w, r, articleTitle)
return
2017-07-11 06:36:08 +00:00
} else if strings.HasPrefix(remainingPath, "modify/") {
articleTitle, err := url.QueryUnescape(remainingPath[len("modify/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
}
this.routeModify(w, r, articleTitle)
return
2017-07-11 06:36:08 +00:00
} else if strings.HasPrefix(remainingPath, "history/") {
articleTitle, err := url.QueryUnescape(remainingPath[len("history/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
}
this.routeHistory(w, r, articleTitle)
return
2017-07-11 06:36:08 +00:00
} else if strings.HasPrefix(remainingPath, "raw/") {
revId, err := strconv.Atoi(remainingPath[len("raw/"):])
2017-07-09 01:18:18 +00:00
if err != nil {
this.serveErrorMessage(w, err)
return
}
this.routeRawView(w, r, revId)
2017-07-09 01:18:18 +00:00
return
2017-07-09 05:20:10 +00:00
2017-07-11 06:36:08 +00:00
} else if strings.HasPrefix(remainingPath, "archive/") {
revId, err := strconv.Atoi(remainingPath[len("archive/"):])
2017-07-09 05:20:10 +00:00
if err != nil {
this.serveErrorMessage(w, err)
return
}
this.routeArchive(w, r, revId)
return
2017-07-11 06:36:08 +00:00
} else if strings.HasPrefix(remainingPath, "recent/") {
pageNum, err := strconv.Atoi(remainingPath[len("recent/"):])
if err != nil {
this.serveErrorMessage(w, err)
return
}
this.routeRecentChanges(w, r, pageNum)
return
2017-07-11 06:36:08 +00:00
} else if remainingPath == "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
2017-07-11 06:36:08 +00:00
} else if match := this.rxDiff.FindStringSubmatch(remainingPath); 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
}
} 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
}
}
// No match? Add 'Page not found' to next session response, and redirect to homepage
this.serveErrorMessage(w, errors.New("Page not found"))
}