From 993864ea35fabc8c845617dfca43897ba039fa95 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 9 Jul 2017 17:20:10 +1200 Subject: [PATCH] implement /archive/[0-9]+ endpoint --- TODO.txt | 2 -- WikiServer.go | 21 ++++++++++++++++++++- rArchive.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 rArchive.go diff --git a/TODO.txt b/TODO.txt index 7ee5338..d7d343c 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,8 +1,6 @@ Save changes -Archive endpoint - Article history Recent history diff --git a/WikiServer.go b/WikiServer.go index f47ed7d..590ea76 100644 --- a/WikiServer.go +++ b/WikiServer.go @@ -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 + } } diff --git a/rArchive.go b/rArchive.go new file mode 100644 index 0000000..8c3ace1 --- /dev/null +++ b/rArchive.go @@ -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( + `
`+ + `You are viewing specific revision of this page, last modified `+ + time.Unix(a.Modified, 0).In(this.loc).Format(this.opts.DateFormat)+`. `+ + `Click here to see the latest revision.`+ + `
`, + ) + bcr.RenderHTML(string(a.Body)) + pto.LoadCodeResources = bcr.CodePresent + + this.servePageResponse(w, r, pto) + return +}