-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
31 lines (28 loc) · 810 Bytes
/
cron.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main
import (
"database/sql"
"fmt"
"net/http"
)
func makeCronHandler(db *sql.DB, handler func(*sql.DB) error) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// X-Appengine-Cron is added by App Engine; if a client sends this header, it is removed.
if r.Header.Get("X-Appengine-Cron") == "" {
logError(r, nil, fmt.Errorf("illegal request to cron"))
w.WriteHeader(http.StatusForbidden)
} else {
if err := handler(db); err != nil {
logError(r, nil, fmt.Errorf("error running cron handler: %w", err))
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}
}
}
func cleanupHandler(db *sql.DB) error {
if err := deleteExpiredSessions(db); err != nil {
return err
}
return nil
}