This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.ts
96 lines (79 loc) · 2.52 KB
/
app.ts
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
import * as express from 'express';
import * as cors from 'cors';
import * as Sentry from '@sentry/node';
import * as expressWinston from 'express-winston';
import * as uuid from 'uuid/v4';
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
import { createMiddleware as createPrometheusMiddleware } from '@promster/express';
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
import { createServer } from '@promster/server';
import makeAPIController from './controllers/api-controller';
import V2ApiController from './controllers/v2-controller';
import { getAccounts } from './lib/addresses';
import { winstonLogger } from './lib/utils';
if (process.env.SENTRY_DSN) {
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.SENTRY_ENV || 'production',
ignoreErrors: [
'Account not found',
'Home page is not available',
],
});
}
const getApp = async () => {
const Genesis = await getAccounts();
const app: express.Application = express();
if (process.env.SENTRY_DSN) {
app.use(Sentry.Handlers.requestHandler());
}
app.use(createPrometheusMiddleware({
app,
options: {
normalizePath: (path: string) => {
if (path.startsWith('/api/stacks/addresses/SP1P72Z3704VMT3DMHPP2CB8TGQWGDBHD3RPR9GZS')) {
return path;
}
if (path.startsWith('/api/stacks/addresses/')) {
return '/api/stacks/addresses/';
}
return '/';
}
}
}));
// Create `/metrics` endpoint on separate server
createServer({ port: 9152 }).then(() => console.log('@promster/server started on port 9152.'));
app.get('/', () => {
throw new Error('Home page is not available.');
});
if (process.env.SENTRY_DSN) {
app.use(Sentry.Handlers.errorHandler());
}
app.use(cors());
app.use(expressWinston.logger({
winstonInstance: winstonLogger,
metaField: null,
}));
app.use('/api', makeAPIController(Genesis));
app.use('/api/v2', V2ApiController);
app.use((error: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
if (error && !res.finished) {
const errorTag = uuid();
Object.assign(error, { errorTag: errorTag });
res.status(500).json({
success: false,
errorTag: errorTag
}).end();
}
next(error);
});
app.use(expressWinston.errorLogger({
winstonInstance: winstonLogger,
metaField: null,
blacklistedMetaFields: [ 'trace', 'os', 'process' ],
}));
return app;
};
export default getApp;