implement /archive/[0-9]+ endpoint

This commit is contained in:
mappu 2017-07-09 17:20:10 +12:00
parent 3127c14e75
commit 993864ea35
3 changed files with 60 additions and 3 deletions

View File

@ -1,8 +1,6 @@
Save changes
Archive endpoint
Article history
Recent history

View File

@ -7,12 +7,14 @@ import (
"net/url"
"strconv"
"strings"
"time"
)
type WikiServer struct {
db *WikiDB
opts *ServerOptions
pageTmp *template.Template
loc *time.Location
}
func NewWikiServer(opts *ServerOptions) (*WikiServer, error) {
@ -23,13 +25,19 @@ func NewWikiServer(opts *ServerOptions) (*WikiServer, error) {
tmpl, err := template.New("yatwiki/page").Parse(pageTemplate)
if err != nil {
panic(err)
return nil, err
}
loc, err := time.LoadLocation(opts.Timezone)
if err != nil {
return nil, err
}
ws := WikiServer{
db: wdb,
opts: opts,
pageTmp: tmpl,
loc: loc,
}
return &ws, nil
}
@ -94,6 +102,17 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.routeRawView(w, r, revId)
return
} 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
}
}

40
rArchive.go Normal file
View File

@ -0,0 +1,40 @@
package yatwiki3
import (
"database/sql"
"errors"
"html/template"
"net/http"
"net/url"
"time"
)
func (this *WikiServer) routeArchive(w http.ResponseWriter, r *http.Request, revId int) {
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
}
pto := DefaultPageTemplateOptions(this.opts)
pto.CurrentPageName = a.Title
pto.CurrentPageIsArticle = true
bcr := NewBBCodeRenderer(this.opts.ExpectBaseURL)
pto.Content = template.HTML(
`<div class="info">`+
`You are viewing specific revision of this page, last modified `+
time.Unix(a.Modified, 0).In(this.loc).Format(this.opts.DateFormat)+`. `+
`Click <a href="`+template.HTMLEscapeString(this.opts.ExpectBaseURL+`view/`+url.QueryEscape(a.Title))+`">here</a> to see the latest revision.`+
`</div>`,
) + bcr.RenderHTML(string(a.Body))
pto.LoadCodeResources = bcr.CodePresent
this.servePageResponse(w, r, pto)
return
}