Browse Source

Save paths of deleted posts and show 410 error

master
Jan-Lukas Else 4 months ago
parent
commit
c14ec0dcb5
  1. 10
      databaseMigrations.go
  2. 4
      errors.go
  3. 8
      http.go
  4. 11
      postsDb.go

10
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 {

4
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)
}

8
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

11
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
}

Loading…
Cancel
Save