implement /raw/[0-9]+ endpoint
This commit is contained in:
parent
187f8b8417
commit
cceffc93ad
5
DB.go
5
DB.go
@ -99,6 +99,11 @@ func (this *WikiDB) GetArticleById(articleId int) (*Article, error) {
|
|||||||
return this.parseArticle(row)
|
return this.parseArticle(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *WikiDB) GetRevision(revId int) (*ArticleWithTitle, error) {
|
||||||
|
row := this.db.QueryRow(`SELECT articles.*, titles.title FROM articles JOIN titles ON articles.article=titles.id WHERE articles.id = ?`, revId)
|
||||||
|
return this.parseArticleWithTitle(row)
|
||||||
|
}
|
||||||
|
|
||||||
func (this *WikiDB) GetLatestVersion(title string) (*Article, error) {
|
func (this *WikiDB) GetLatestVersion(title string) (*Article, error) {
|
||||||
row := this.db.QueryRow(`SELECT articles.* FROM articles WHERE article = (SELECT id FROM titles WHERE title = ?) ORDER BY modified DESC LIMIT 1`, title)
|
row := this.db.QueryRow(`SELECT articles.* FROM articles WHERE article = (SELECT id FROM titles WHERE title = ?) ORDER BY modified DESC LIMIT 1`, title)
|
||||||
return this.parseArticle(row)
|
return this.parseArticle(row)
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -147,6 +148,28 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
this.servePageResponse(w, r, pto)
|
this.servePageResponse(w, r, pto)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
} 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
|
||||||
|
}
|
||||||
|
|
||||||
|
a, err := this.db.GetRevision(revId)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
this.serveErrorMessage(w, errors.New("No such revision."))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.serveErrorMessage(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set(`Content-Type`, `text/plain; charset=UTF-8`)
|
||||||
|
w.WriteHeader(200)
|
||||||
|
w.Write(a.Body)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -179,6 +202,7 @@ func (this *WikiServer) serveRedirect(w http.ResponseWriter, location string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *WikiServer) servePageResponse(w http.ResponseWriter, r *http.Request, pto *pageTemplateOptions) {
|
func (this *WikiServer) servePageResponse(w http.ResponseWriter, r *http.Request, pto *pageTemplateOptions) {
|
||||||
|
w.WriteHeader(200)
|
||||||
pto.SessionMessage = template.HTML(r.URL.Query().Get("error")) // FIXME reflected XSS (although Chrome automatically blocks it..)
|
pto.SessionMessage = template.HTML(r.URL.Query().Get("error")) // FIXME reflected XSS (although Chrome automatically blocks it..)
|
||||||
|
|
||||||
err := this.pageTmp.Execute(w, pto)
|
err := this.pageTmp.Execute(w, pto)
|
||||||
|
Loading…
Reference in New Issue
Block a user