-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
61 lines (48 loc) · 1.49 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
"use strict";
require("dotenv").config();
const Sentry = require("@sentry/node");
const Koa = require("koa");
const cors = require("@koa/cors");
const auth = require("koa-basic-auth");
const serve = require("koa-static");
const logger = require("koa-logger");
const { bodyParser } = require("@koa/bodyparser");
const routes = require("./app/routes");
const middleware = require("./app/middleware");
const app = new Koa();
// initialize Sentry and capture errors
Sentry.init({
dsn: process.env.SENTRY_DSN,
autoSessionTracking: false,
});
app.on("error", (err, ctx) => {
Sentry.withScope((scope) => {
scope.addEventProcessor((event) => {
return Sentry.Handlers.parseRequest(event, ctx.request);
});
Sentry.captureException(err);
});
});
// setup initial middleware
app.use(logger());
app.use(cors());
app.use(bodyParser({ jsonLimit: "50mb" }));
app.use(middleware.customHeaders);
// setup routes
routes.forEach((route) => app.use(route));
// conditionally apply basic auth to downstream middleware
if (process.env.AVERT_AUTH === "true") {
app.use(middleware.basicAuth);
app.use(auth({ name: process.env.AVERT_USER, pass: process.env.AVERT_PASS }));
}
// setup serving of static files and custom 404
app.use(serve("./app/public"));
app.use(middleware.pageNotFound);
// configure port and start server
app.port = process.env.PORT || 3001;
if (!module.parent) {
app.listen(app.port, () => {
console.log(`Koa is running on http://localhost:${app.port}`);
});
}
module.exports = app;