-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
121 lines (96 loc) · 2.52 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"cloud.google.com/go/firestore"
)
// It's alive! The application starts here.
func main() {
// setting up database connection: 2 clients encapsulated into PairingLogic struct
ctx := context.Background()
appEnv := os.Getenv("APP_ENV")
projectId := "pairing-bot-284823"
botUsername := "pairing-bot@recurse.zulipchat.com"
log.Printf("Running the app in environment = %s", appEnv)
//We have two pairing bot projects. One for production and one for testing/dev work.
if appEnv != "production" {
projectId = "pairing-bot-dev"
botUsername = "dev-pairing-bot@recurse.zulipchat.com"
log.Println("Running pairing bot in the testing environment for development")
}
rc, err := firestore.NewClient(ctx, projectId)
if err != nil {
log.Panic(err)
}
defer rc.Close()
ac, err := firestore.NewClient(ctx, projectId)
if err != nil {
log.Panic(err)
}
defer ac.Close()
pc, err := firestore.NewClient(ctx, projectId)
if err != nil {
log.Panic(err)
}
defer pc.Close()
revc, err := firestore.NewClient(ctx, projectId)
if err != nil {
log.Panic(err)
}
defer revc.Close()
rdb := &FirestoreRecurserDB{
client: rc,
}
rcapi := RecurseAPI{
rcAPIURL: "https://www.recurse.com/api/v1",
}
adb := &FirestoreAPIAuthDB{
client: ac,
}
pdb := &FirestorePairingsDB{
client: pc,
}
revdb := &FirestoreReviewDB{
client: revc,
}
ur := &zulipUserRequest{}
un := &zulipUserNotification{
botUsername: botUsername,
zulipAPIURL: "https://recurse.zulipchat.com/api/v1/messages",
}
sm := &zulipStreamMessage{
botUsername: botUsername,
zulipAPIURL: "https://recurse.zulipchat.com/api/v1/messages",
}
pl := &PairingLogic{
rdb: rdb,
adb: adb,
pdb: pdb,
ur: ur,
un: un,
sm: sm,
rcapi: rcapi,
revdb: revdb,
}
http.HandleFunc("/", http.NotFound) // will this handle anything that's not defined?
http.HandleFunc("/webhooks", pl.handle) // from zulip
http.HandleFunc("/match", pl.match) // from GCP- daily
http.HandleFunc("/endofbatch", pl.endofbatch) // from GCP- weekly
http.HandleFunc("/welcome", pl.welcome) // from GCP- weekly
http.HandleFunc("/checkin", pl.checkin) // from GCP- weekly
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
if m, ok := os.LookupEnv("PB_MAINT"); ok {
if m == "true" {
maintenanceMode = true
}
}
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}