This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathapp.js
158 lines (144 loc) · 4.48 KB
/
app.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
150
151
152
153
154
155
156
157
158
const logger = require('winston');
const path = require('path');
const config = require('config');
const favicon = require('serve-favicon');
const compress = require('compression');
const cors = require('cors');
const helmet = require('helmet');
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const configuration = require('@feathersjs/configuration');
const Sentry = require('@sentry/node');
const swagger = require('feathers-swagger');
const socketsConfig = require('./socketsConfig');
const configureLogger = require('./utils/configureLogger');
const middleware = require('./middleware');
const services = require('./services');
const appHooks = require('./app.hooks');
const authentication = require('./authentication');
const blockchain = require('./blockchain');
const mongoose = require('./mongoose');
const ipfsFetcher = require('./utils/ipfsFetcher');
const ipfsPinner = require('./utils/ipfsPinner');
const { configureAuditLog } = require('./auditLog/feathersElasticSearch');
const channels = require('./channels');
const app = express(feathers());
Sentry.init({
dsn: config.sentryDsn,
environment: process.env.NODE_ENV,
release: `Giveth-Feathers@${process.env.npm_package_version}`,
// we want to capture 100% of errors
sampleRate: 1,
/**
* @see{@link https://docs.sentry.io/platforms/node/configuration/sampling/#setting-a-uniform-sample-rate}
*/
// Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
// But we recommend adjusting this value in production
tracesSampleRate: 0.1,
});
function initSwagger() {
app.configure(
swagger({
docsPath: '/docs',
openApiVersion: 3,
idType: 'string',
uiIndex: true,
specs: {
info: {
title: 'Feathers-Giveth API',
description: 'In reality the user use socketio instead of REST',
version: process.env.npm_package_version,
},
components: {
securitySchemes: {
BearerAuth: {
type: 'http',
scheme: 'bearer',
description:
'Open giveth-dapp in chrome and copy accessToken from Network -> WS -> Messages section',
},
},
},
servers: [
{
url: 'http://localhost:3030',
description: 'Localhost',
},
{
url: 'https://feathers.develop.giveth.io',
description: 'UAT',
},
{
url: 'https://feathers.giveth.io',
description: 'Production',
},
],
security: [{ BearerAuth: [] }],
schemes: ['http', 'https'], // Optionally set the protocol schema used (sometimes required when host on https)
},
}),
);
}
function initFeatherApp() {
// Load app configuration
app.configure(configuration());
app.configure(configureLogger);
app.use(cors());
app.use(helmet());
app.use(compress());
app.use(express.json({ limit: '10mb' }));
app.use(
express.urlencoded({
limit: '10mb',
extended: true,
}),
);
app.use(favicon(path.join(app.get('public'), 'favicon.ico')));
// Host the public folder
app.use('/', express.static(app.get('public')));
app.configure(mongoose);
app.configure(express.rest());
app.configure(socketsConfig);
initSwagger();
// Configure other middleware (see `middleware/index.js`)
app.configure(middleware);
app.configure(authentication);
// Set up our services (see `services/index.js`)
app.configure(services);
app.configure(channels);
// blockchain must be initialized after services
app.configure(blockchain);
app.configure(ipfsFetcher);
app.configure(ipfsPinner);
// Configure a middleware for 404s and the error handler
app.use(express.notFound());
app.use(
express.errorHandler({
logger: {
error: e => {
if (e.name === 'NotFound') {
logger.warn(`404 - NotFound - ${e.data.url}`);
} else {
logger.error('Express error handler:', e);
}
},
info: e => {
if (e.name === 'NotFound') {
logger.warn(`404 - NotFound - ${e.data.url}`);
} else {
logger.error('Express error handler:', e);
}
},
},
}),
);
app.hooks(appHooks);
if (config.enableAuditLog) {
configureAuditLog(app);
}
return app;
}
function getFeatherAppInstance() {
return app;
}
module.exports = { initFeatherApp, getFeatherAppInstance };