-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.js
More file actions
104 lines (90 loc) · 3.13 KB
/
server.js
File metadata and controls
104 lines (90 loc) · 3.13 KB
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
const express = require('express');
const app = express();
const morgan = require('morgan');
const toml = require('toml');
const fs = require('fs');
const settings = toml.parse(fs.readFileSync('./settings.toml'));
const Database = require('./structures/Database');
const mailTransport = require('./structures/mailTransport');
const webhookTransport = require('./structures/webhookTransport');
const ApiRouter = require('./api/v1/Router');
const db = new Database(settings.mongo);
const sentry = require('@sentry/node');
const hotShots = require('hot-shots');
mailTransport.config({ from: settings.email.from, test: settings.email.test });
webhookTransport.config(settings.webhooks);
if (process.env.NODE_ENV === 'production') {
sentry.init({
dsn: settings.raven.url,
release: (require('./package.json')).version,
attachStacktrace: true,
maxBreadcrumbs: 50,
beforeSend(event) {
return process.env.NODE_ENV === 'production' ? event : null;
},
defaultIntegrations: [
new sentry.Integrations.Console(),
new sentry.Integrations.Http({
breadcrumbs: true,
tracing: false
}),
new sentry.Integrations.OnUncaughtException(),
new sentry.Integrations.OnUnhandledRejection()
]
});
app.use(sentry.Handlers.requestHandler());
new hotShots({
host: settings.statsd.host,
port: settings.statsd.port,
globalize: true,
cacheDNS: true,
errorHandler(error) {
console.error('[hot-shots]', error);
sentry.captureException(error);
}
});
var datadog = require('connect-datadog')({
dogstatsd: statsd,
tags: ['app:catgirls-api'],
response_code: true,
path: true,
method: true
});
app.use(datadog);
} else {
global.statsd = new Proxy({ }, {
get() { return function() { } }
});
}
app.set('trust proxy', 'loopback');
app.set('env', 'production');
app.disable('x-powered-by');
app.use(morgan(':req[cf-connecting-ip] :method :url :status :response-time[0]ms', {
skip: (req, res) => res.statusCode < 400 // Only log failed requests/responses
}));
// Parse data into req.body
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// In dev we need a way to get images
if (process.env.NODE_ENV === 'development') {
app.use('/image', express.static('image', { index: false, extensions: ['jpg'] }));
app.use('/thumbnail', express.static('thumbnail', { index: false, extensions: ['jpg'] }));
}
app.use((req, res, next) => {
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Credentials', 'true');
res.set('Access-Control-Allow-Methods', 'GET,OPTIONS,POST,PUT,PATCH,DELETE');
res.set('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization');
return next();
});
// Load in the routes for express
const api = new ApiRouter(settings, db, mailTransport, webhookTransport);
app.use(api.path, api.router);
if (process.env.NODE_ENV === 'production')
app.use(sentry.Handlers.errorHandler());
// Start the express server
app.listen(settings.port, error => {
if (error)
return console.log(error);
console.log('Server listening for requests on port', settings.port);
});