-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
149 lines (126 loc) · 5.06 KB
/
index.js
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
149
require("module-alias/register");
require("dotenv").config();
require("~strings/i18nextInit");
const path = require("path");
const express = require("express");
const morgan = require("morgan");
const basicAuth = require("express-basic-auth");
const bodyParser = require("body-parser");
const airtableWorker = require("./src/workers/airtable-sync/worker");
const airtablePaymentsWorker = require("./src/workers/airtable-sync/paymentWorker");
const { nycmaIntakeHandler } = require("./src/api/authed/intake/manyc.js");
const { nycmaOuttakeHandler } = require("./src/api/authed/outtake/manyc.js");
const {
addressHandler,
} = require("./src/api/neighborhood-finder/get-address-meta.js");
const {
neighborhoodFinderUpdateRequestHandler,
} = require("./src/api/neighborhood-finder/update-request.js");
const {
deliveryNeededRequestHandler,
assignDeliveryHandler,
} = require("./src/api/delivery-needed/index.js");
/* eslint-disable global-require */
const app = express();
app.use(morgan("tiny"));
if (!process.env.AIRTABLE_BASE || !process.env.AIRTABLE_KEY) {
console.warn("An airtable key is missing. Something will probably break :/");
}
// ==================================================================
// Slack (must come before body parser)
// ==================================================================
if (process.env.SLACK_BOT_TOKEN && process.env.SLACK_SIGNING_SECRET) {
app.post("/slack/events", require("./src/slackapp/endpoints/events.js"));
app.post(
"/slack/interactivity",
require("./src/slackapp/endpoints/interactivity.js")
);
} else {
console.warn("Slack tokens are missing! Slack routes won't exist.");
}
// ==================================================================
// Common Middleware
// ==================================================================
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// ==================================================================
// Twilio Functions: written separately, deployed as serverless functions in src/twilio subpackage
// ==================================================================
// ==================================================================
// API Routes
// ==================================================================
if (process.env.GOOGLE_MAPS_API_KEY && process.env.GEONAME_CLIENT_ID) {
app.post("/api/geo/address-metadata", addressHandler);
} else {
console.warn("Geo keys missing. Not starting geo routes.");
}
app.post(
"/api/neighborhood-finder/update-request",
neighborhoodFinderUpdateRequestHandler
);
app.get("/api/delivery-needed/requests.json", deliveryNeededRequestHandler);
if (!process.env.TWILIO_SMS_DELIVERY_ENDPOINT) {
console.warn(
"TWILIO_SMS_DELIVERY_ENDPOINT env var missing. Assigning deliveries via SMS will not work."
);
}
app.post("/api/delivery-needed/assign", assignDeliveryHandler);
// ==================================================================
// API Routes (w/ Basic Auth)
// ==================================================================
if (process.env.BASIC_AUTH_USERS) {
const allUsers = {};
const userPass = process.env.BASIC_AUTH_USERS.split(";");
userPass.forEach((pair) => {
const [user, pass] = pair.split(":");
allUsers[user] = pass;
});
app.use(
"/api/authed/*",
basicAuth({
users: allUsers,
})
);
app.post("/api/authed/intake/nycma", nycmaIntakeHandler);
app.post("/api/authed/outtake/nycma", nycmaOuttakeHandler);
} else if (process.env.NODE_ENV !== "production") {
console.warn("Not production environment and no authed users set.");
console.warn("Authed API routes are accessible without authentication.");
app.post("/api/authed/intake/nycma", nycmaIntakeHandler);
app.post("/api/authed/outtake/nycma", nycmaOuttakeHandler);
} else {
console.warn(
"No basic auth users registered. Not starting authed API routes."
);
}
// ==================================================================
// React App + Static Serving
// ==================================================================
app.use(
"/locales",
express.static(path.join(__dirname, "src/lib/strings/locales"))
);
app.use(express.static(path.join(__dirname, "build")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
// ==================================================================
// In-process Workers
// ==================================================================
const airtableIntervalMs = parseInt(process.env.AIRTABLE_SYNC || 0);
if (airtableIntervalMs > 0) {
airtableWorker(airtableIntervalMs);
if (process.env.AIRTABLE_PAYMENTS_BASE) {
airtablePaymentsWorker(airtableIntervalMs + 2000); // Stagger polling to avoid rate limit
}
}
// ==================================================================
// Start Express Server
// ==================================================================
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Mutual Aid app listening on ${port}!`);
});