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( + `