delete old sessions

This commit is contained in:
Timon Ringwald 2022-09-06 12:36:21 +02:00
parent f75720e0ec
commit 12298408da
2 changed files with 13 additions and 3 deletions

7
db.go
View File

@ -9,9 +9,10 @@ import (
var Database = &advsql.Database{}
var (
InsertSession = advsql.Insert(Database, "INSERT INTO sessions VALUES (?, ?, ?)", ScanSessionPkFirst)
UpdateSession = advsql.Update(Database, "UPDATE sessions SET expiration_date = ?, creation_date = ? WHERE id = ?", ScanSessionPkLast)
GetSessionByID = advsql.QueryOne(Database, "SELECT * FROM sessions WHERE id = ?", ScanSessionPkFirst)
InsertSession = advsql.Insert(Database, "INSERT INTO sessions VALUES (?, ?, ?)", ScanSessionPkFirst)
UpdateSession = advsql.Update(Database, "UPDATE sessions SET expiration_date = ?, creation_date = ? WHERE id = ?", ScanSessionPkLast)
GetSessionByID = advsql.QueryOne(Database, "SELECT * FROM sessions WHERE id = ?", ScanSessionPkFirst)
DeleteSessionsByExpirationDateBefore = advsql.Delete(Database, "DELETE FROM sessions WHERE expiration_date <= ?")
InsertBookmark = advsql.Insert(Database, "INSERT INTO bookmarks VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", ScanBookmarkPkFirst)
UpdateBookmark = advsql.Update(Database, "UPDATE bookmarks SET link = ?, image = ?, color = ?, image_size = ?, icon_padding = ?, hide_border = ?, order_priority = ? WHERE session_id = ? AND title = ?", ScanBookmarkPkLast)

View File

@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"strings"
"time"
"git.milar.in/milarin/advsql"
"git.milar.in/milarin/channel"
@ -50,6 +51,14 @@ func main() {
r.HandleFunc("/search", search)
r.PathPrefix("/static/").Handler(http.FileServer(http.FS(StaticFS)))
go func() {
for range time.Tick(time.Hour) {
if err := DeleteSessionsByExpirationDateBefore(time.Now()); err != nil {
fmt.Println("could not delete expired sessions", err)
}
}
}()
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", envvars.String("HTTP_INTF", ""), envvars.Uint16("HTTP_PORT", 80)), r); err != nil {
panic(err)
}