-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
132 lines (109 loc) · 3.41 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
package main
import (
"context"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"cloud.google.com/go/firestore"
"github.com/recursecenter/pairing-bot/recurse"
"github.com/recursecenter/pairing-bot/store"
"github.com/recursecenter/pairing-bot/zulip"
)
// It's alive! The application starts here.
func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
AddSource: true,
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
// Leave all nested attributes as-is.
if len(groups) != 0 {
return attr
}
// Rewrite some standard fields to match the logging agent's
// expectations.
//
// https://cloud.google.com/logging/docs/agent/logging/configuration#special-fields
switch attr.Key {
case slog.MessageKey:
attr.Key = "message"
case slog.LevelKey:
attr.Key = "severity"
case slog.SourceKey:
attr.Key = "logging.googleapis.com/sourceLocation"
}
return attr
},
})))
ctx := context.Background()
appVersion := os.Getenv("GAE_VERSION")
appEnv := os.Getenv("APP_ENV")
projectId := "pairing-bot-284823"
botUsername := "[email protected]"
welcomeStream := "🧑💻 current batches" // The emoji is literally part of the channel name!
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" {
slog.Info("Setting dev/test config values")
projectId = "pairing-bot-dev"
botUsername = "[email protected]"
welcomeStream = "test-bot"
}
// Set up database wrappers. The Firestore client has a connection pool, so
// we can share this one DB handle among all the collection helpers.
db, err := firestore.NewClient(ctx, projectId)
if err != nil {
log.Panic(err)
}
defer db.Close()
zulipCredentials := func(ctx context.Context) (zulip.Credentials, error) {
password, err := store.Secrets(db).Get(ctx, "zulip_api_key")
if err != nil {
return zulip.Credentials{}, err
}
return zulip.Credentials{
Username: botUsername,
Password: password,
}, nil
}
zulipClient, err := zulip.NewClient(zulipCredentials)
if err != nil {
panic(err)
}
recurseAccessToken := func(ctx context.Context) (recurse.AccessToken, error) {
token, err := store.Secrets(db).Get(ctx, "recurse_access_token")
if err != nil {
return "", err
}
return recurse.AccessToken(token), nil
}
recurseClient, err := recurse.NewClient(recurseAccessToken)
if err != nil {
panic(err)
}
pl := &PairingLogic{
db: db,
recurse: recurseClient,
zulip: zulipClient,
version: appVersion,
welcomeStream: welcomeStream,
}
http.HandleFunc("/", http.NotFound) // will this handle anything that's not defined?
http.HandleFunc("/webhooks", pl.handle) // from zulip
http.HandleFunc("/match", cron(pl.Match)) // from GCP- daily
http.HandleFunc("/endofbatch", cron(pl.EndOfBatch)) // from GCP- weekly
http.HandleFunc("/welcome", cron(pl.Welcome)) // from GCP- weekly
http.HandleFunc("/checkin", cron(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" {
pl.maintenanceMode = true
}
}
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}