forked from artsy/metaphysics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (122 loc) · 3.48 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
import "moment-timezone"
import Bluebird from "bluebird"
import xapp from "artsy-xapp"
import compression from "compression"
import forceSSL from "express-force-ssl"
import bodyParser from "body-parser"
import { info, error } from "./src/lib/loggers"
import config from "config"
import cache from "lib/cache"
import { init as initTracer } from "lib/tracer"
import { IpFilter as ipfilter } from "express-ipfilter"
import { errorHandler } from "lib/errorHandler"
const {
ENABLE_ASYNC_STACK_TRACES,
ENABLE_QUERY_TRACING,
GRAVITY_API_URL,
GRAVITY_ID,
GRAVITY_SECRET,
IP_BLACKLIST,
NODE_ENV,
PORT,
PRODUCTION_ENV,
} = config
global.Promise = Bluebird
const port = PORT
const isDevelopment = NODE_ENV === "development"
const enableAsyncStackTraces = ENABLE_ASYNC_STACK_TRACES === "true"
const enableQueryTracing = ENABLE_QUERY_TRACING === "true"
let server, isShuttingDown
// This needs to happen as early as possible so the plugins can hook into the
// modules we use before any other code gets a chance to use them.
if (enableQueryTracing) {
if (isDevelopment) {
console.warn(
"[WARNING] You probably don't want ENABLE_QUERY_TRACING set to true in .env"
)
}
console.warn("[FEATURE] Enabling query tracing")
initTracer()
}
if (enableAsyncStackTraces) {
console.warn("[FEATURE] Enabling long async stack traces") // eslint-disable-line
require("longjohn")
}
// Use require here so that it definitely gets loaded /after/ initializing the
// datadog client, which is needed so it can hook into supported modules such as
// express.
const app = require("express")()
app.use(compression())
xapp.on("error", err => {
error(
"Could not start Metaphysics because it could not set up the xapp token, this is likely due to your `GRAVITY_*` env vars:"
)
error(err)
process.exit(1)
})
const xappConfig = {
url: GRAVITY_API_URL,
id: GRAVITY_ID,
secret: GRAVITY_SECRET,
}
xapp.init(xappConfig, bootApp) // eslint-disable-line
function bootApp() {
if (PRODUCTION_ENV) {
app.set("forceSSLOptions", { trustXFPHeader: true }).use(forceSSL)
app.set("trust proxy", 1)
}
if (IP_BLACKLIST) {
app.use(
ipfilter(IP_BLACKLIST.split(","), {
allowedHeaders: ["x-forwarded-for"],
log: false,
mode: "deny",
})
)
}
app.use(bodyParser.json())
app.get("/favicon.ico", (_req, res) => {
res
.status(200)
.set({ "Content-Type": "image/x-icon" })
.end()
})
app.get("/health", (req, res) => {
if (isShuttingDown) {
return res.status(503).end()
}
cache
.isAvailable()
.then(stats => {
return res.status(200).end()
})
.catch(err => {
return res.status(503).end()
})
})
app.all("/graphql", (_req, res) => res.redirect("/"))
if (isDevelopment) {
const { createReloadable } = require("@artsy/express-reloadable")
const mountAndReload = createReloadable(app, require)
mountAndReload("./src")
} else {
app.use(require("./src").default)
}
server = require("http-shutdown")(
app.listen(port, () =>
info(`[Metaphysics] Listening on http://localhost:${port}`)
)
)
// General error handler, should be last (and after Sentry's).
app.use(errorHandler)
}
process.on("SIGTERM", gracefulExit)
function gracefulExit() {
if (isShuttingDown) return
isShuttingDown = true
console.log("Received signal SIGTERM, shutting down")
server.shutdown(function() {
console.log("Closed existing connections.")
process.exit(0)
})
}