forked from code-golf/code-golf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (131 loc) · 3.55 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/code-golf/code-golf/db"
"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)
db := db.Open()
// Every 10 seconds.
go func() {
// Refreshing the mat views every 10 seconds is overkill on dev.
// FIXME Maybe it would be better to do something with NOTIFY/TRIGGER.
duration := 10 * time.Second
if _, dev := os.LookupEnv("DEV"); dev {
duration = 5 * time.Minute
}
for range time.Tick(duration) {
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, 'big-brother'::cheevo
FROM points
WHERE points >= 1984
UNION ALL
SELECT user_id, 'its-over-9000'
FROM points
WHERE points > 9000
UNION ALL
SELECT user_id, 'twenty-kiloleagues'
FROM points
WHERE points >= 20000
UNION ALL
SELECT user_id, 'marathon-runner'
FROM points
WHERE points >= 42195
UNION ALL
SELECT user_id, '0xdead'
FROM points
WHERE points >= 57005
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 := github.Wiki(db); err != nil {
log.Println(err)
}
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)
}
}
if _, err := db.Exec(
`INSERT INTO trophies(user_id, trophy)
SELECT user_id, 'aged-like-fine-wine'
FROM solutions
WHERE NOT failing
GROUP BY user_id
HAVING EXTRACT(days FROM TIMEZONE('UTC', NOW()) - MIN(submitted)) >= 365
ON CONFLICT DO NOTHING`,
); err != nil {
log.Println(err)
}
}
}()
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)))
}