From c14ec0dcb5ecfd125c49bcbbbdff2dfddc9e2183 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Fri, 23 Jul 2021 09:53:35 +0200 Subject: [PATCH] Save paths of deleted posts and show 410 error --- databaseMigrations.go | 10 ++++++++++ errors.go | 4 ++++ http.go | 8 ++++++++ postsDb.go | 11 ++++++++++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/databaseMigrations.go b/databaseMigrations.go index 56d2ecb..f7fdf79 100644 --- a/databaseMigrations.go +++ b/databaseMigrations.go @@ -240,6 +240,16 @@ func migrateDb(db *sql.DB, logging bool) error { return err }, }, + &migrator.Migration{ + Name: "00020", + Func: func(tx *sql.Tx) error { + _, err := tx.Exec(` + create table deleted (path text primary key); + create index index_post_parameters_par_val_pat on post_parameters (parameter, value, path); + `) + return err + }, + }, ), ) if err != nil { diff --git a/errors.go b/errors.go index 0769270..6eb2d9f 100644 --- a/errors.go +++ b/errors.go @@ -17,6 +17,10 @@ func (a *goBlog) serve404(w http.ResponseWriter, r *http.Request) { a.serveError(w, r, fmt.Sprintf("%s was not found", r.RequestURI), http.StatusNotFound) } +func (a *goBlog) serve410(w http.ResponseWriter, r *http.Request) { + a.serveError(w, r, fmt.Sprintf("%s doesn't exist anymore", r.RequestURI), http.StatusGone) +} + func (a *goBlog) serveNotAllowed(w http.ResponseWriter, r *http.Request) { a.serveError(w, r, "", http.StatusMethodNotAllowed) } diff --git a/http.go b/http.go index 409fc98..44623c2 100644 --- a/http.go +++ b/http.go @@ -493,6 +493,8 @@ func (a *goBlog) servePostsAliasesRedirects(pmh ...func(http.Handler) http.Handl select 'post', status from posts where path = @path union all select 'alias', path from post_parameters where parameter = 'aliases' and value = @path + union all + select 'deleted', '' from deleted where path = @path limit 1 `, sql.Named("path", path)) if err != nil { @@ -527,6 +529,12 @@ func (a *goBlog) servePostsAliasesRedirects(pmh ...func(http.Handler) http.Handl http.Redirect(w, r, value, http.StatusFound) }).ServeHTTP(w, r) return + case "deleted": + // Is deleted, serve 410 + alicePrivate.Append(a.cache.cacheMiddleware).ThenFunc(func(w http.ResponseWriter, r *http.Request) { + a.serve410(w, r) + }).ServeHTTP(w, r) + return } } // No post, check regex redirects or serve 404 error diff --git a/postsDb.go b/postsDb.go index f822170..106e527 100644 --- a/postsDb.go +++ b/postsDb.go @@ -204,11 +204,20 @@ func (db *database) deletePost(path string) (*post, error) { if path == "" { return nil, nil } + db.pcm.Lock() + defer db.pcm.Unlock() p, err := db.getPost(path) if err != nil { return nil, err } - _, err = db.exec("begin;delete from posts where path = ?;delete from post_parameters where path = ?;commit;", dbNoCache, p.Path, p.Path) + _, err = db.exec( + `begin; + delete from posts where path = ?; + delete from post_parameters where path = ?; + insert or ignore into deleted (path) values (?); + commit;`, + dbNoCache, p.Path, p.Path, p.Path, + ) if err != nil { return nil, err }