-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapp.mjs
133 lines (116 loc) Β· 3.25 KB
/
app.mjs
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
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import "dotenv/config";
import AutoLoad from "@fastify/autoload";
import Bree from "bree";
import Graceful from "@ladjs/graceful";
import * as Sentry from "@sentry/node";
import SegfaultHandler from "segfault-handler";
import jobs from "./jobs/index.mjs";
import { CHAINS } from "./constants/chains.mjs";
const __dirname = dirname(fileURLToPath(import.meta.url));
const chainIdParamSchema = {
$id: "chainIdParam",
params: {
type: "object",
properties: {
chainId: {
type: "number",
enum: CHAINS,
},
},
required: ["chainId"],
},
};
/**
* @param {import("fastify").FastifyInstance} fastify
*/
export async function server(fastify, opts) {
console.log("node version: " + process.version);
// enable SEGFAULT handler
SegfaultHandler.registerHandler("crash.log", function (signal, address, stack) {
console.log("*** RECEIVED SEGFAULT ***");
console.log("signal : " + signal);
console.log("address : " + address);
console.log("stack :");
for (const line of stack) {
console.log(line);
}
});
// sentry is enabled
if (process.env.SENTRY_DSN) {
setSentryErrorHandler(fastify);
// sentry is disabled
} else {
setDefaultErrorHandler(fastify);
}
fastify.setNotFoundHandler({}, function (request, reply) {
const { url, method } = request.raw;
const message = `Route ${method}:${url} not found`;
reply.code(404).send({
message,
error: "Not Found",
statusCode: 404,
});
});
fastify.register(AutoLoad, {
dir: join(__dirname, "plugins"),
options: Object.assign({}, opts),
});
fastify.register(AutoLoad, {
dir: join(__dirname, "routes"),
options: Object.assign({}, opts),
});
fastify.addSchema(chainIdParamSchema);
// bree needs at least one job to work
if (jobs.length > 0) {
const bree = new Bree({
logger: fastify.log,
defaultExtension: "mjs", // i'm a unicorn,
jobs: jobs,
errorHandler: (error, metadata) => {
// sentry is enabled
if (process.env.SENTRY_DSN) {
Sentry.captureException(error, metadata);
// sentry is disabled
} else {
console.log(error);
console.log(metadata);
}
},
});
const graceful = new Graceful({ brees: [bree] });
graceful.listen();
bree.start();
fastify.decorate("bree", bree);
}
}
export async function setSentryErrorHandler(fastify) {
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV || "development",
});
fastify.setErrorHandler(function (err, request, reply) {
Sentry.withScope((scope) => {
if (request.raw.ip) {
scope.setUser({
ip_address: request.raw.ip,
});
}
scope.setTag("path", request.raw.url);
Sentry.captureException(err);
});
return reply.send(err);
});
}
export async function setDefaultErrorHandler(fastify) {
fastify.setErrorHandler(function (err, request, reply) {
console.log(err);
return reply.send(err);
});
}
const REQUEST_TIMEOUT = parseInt(process.env.REQUEST_TIMEOUT || "1000", 10);
server.options = {
requestTimeout: REQUEST_TIMEOUT,
};
export default server;