forked from code-golf/code-golf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (108 loc) · 2.79 KB
/
main.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"database/sql"
"log"
"math/rand"
"net/http"
"os"
"time"
"github.com/code-golf/code-golf/discord"
"github.com/code-golf/code-golf/github"
"github.com/code-golf/code-golf/routes"
_ "github.com/lib/pq"
)
func main() {
log.SetFlags(log.Ltime)
rand.Seed(time.Now().UnixNano())
db, err := sql.Open("postgres", "")
if err != nil {
panic(err)
}
// Every 30 seconds.
go func() {
for range time.Tick(30 * time.Second) {
for _, view := range []string{"medals", "rankings", "points"} {
if _, err := db.Exec(
"REFRESH MATERIALIZED VIEW CONCURRENTLY " + view,
); err != nil {
log.Println(err)
}
}
// Once points is refreshed, award points based cheevos.
if _, err := db.Exec(
`INSERT INTO trophies(user_id, trophy)
SELECT user_id, 'its-over-9000'::cheevo
FROM points
WHERE points > 9000
UNION ALL
SELECT user_id, 'twenty-kiloleagues'::cheevo
FROM points
WHERE points >= 20000
UNION ALL
SELECT user_id, 'marathon-runner'::cheevo
FROM points
WHERE points >= 42195
ON CONFLICT DO NOTHING`,
); err != nil {
log.Println(err)
}
}
}()
// Every 5 minutes.
go func() {
for range time.Tick(5 * time.Minute) {
// Various GitHub API requests.
github.Run(db, false)
if err := discord.AwardRoles(db); err != nil {
log.Println(err)
}
}
}()
// Every hour.
go func() {
for range time.Tick(time.Hour) {
// Update GitHub usernames.
github.Run(db, true)
for _, job := range [...]struct{ name, sql string }{
{
"expired sessions",
`DELETE FROM sessions
WHERE last_used < TIMEZONE('UTC', NOW()) - INTERVAL '30 days'`,
},
{
"superfluous users",
`DELETE FROM users u
WHERE NOT EXISTS (SELECT FROM sessions WHERE user_id = u.id)
AND NOT EXISTS (SELECT FROM trophies WHERE user_id = u.id)`,
},
{
"users scheduled for deletion",
"DELETE FROM users WHERE delete < TIMEZONE('UTC', NOW())",
},
} {
if res, err := db.Exec(job.sql); err != nil {
log.Println(err)
} else if rows, _ := res.RowsAffected(); rows != 0 {
log.Printf("Deleted %d %s\n", rows, job.name)
}
}
}
}()
log.Println("Listening…")
// Dev.
if _, dev := os.LookupEnv("DEV"); dev {
// Redirect HTTP to HTTPS.
go func() {
panic(http.ListenAndServe(":80",
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://localhost"+r.RequestURI,
http.StatusMovedPermanently)
})))
}()
// Serve HTTPS.
panic(http.ListenAndServeTLS(
":443", "localhost.pem", "localhost-key.pem", routes.Router(db)))
}
// Live only listens on HTTP, TLS is handled by Caddy.
panic(http.ListenAndServe(":80", routes.Router(db)))
}