-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
144 lines (119 loc) · 3.77 KB
/
server.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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const mongoose = require('mongoose');
const config = require('./config');
const path = require('path');
const cors = require('cors');
const ip = require('ip');
const app = express();
// HMR
//---
if (process.env.NODE_ENV !== 'test' && process.env.NODE_ENV !== 'production') {
const webpack = require('webpack');
const webpackConfig = require('./webpack.config');
const compiler = webpack(webpackConfig);
app.use(
require('webpack-dev-middleware')(compiler, {
logLevel: 'debug',
publicPath: webpackConfig.output.publicPath
})
);
app.use(
require('webpack-hot-middleware')(compiler, {
log: console.log,
path: '/__webpack_hmr',
heartbeat: 10 * 1000
})
);
}
// APP CONFIGURATION
//-------------------
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// configure our backend to handle CORS requests
app.use(cors());
// log API PaperTrail - Console
const winston = require('winston');
require('winston-papertrail').Papertrail;
const winstonPapertrail = new winston.transports.Papertrail({
host: 'logs4.papertrailapp.com',
port: 31389
});
winstonPapertrail.on('error', () => {});
const logger = new winston.Logger({
transports: [
winstonPapertrail,
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
logger.stream = {
write: message => {
if (message.includes('api')) logger.info(message);
}
};
// LOG
if (process.env.NODE_ENV === 'production') {
app.use(
morgan(
'{"remote_addr": ":remote-addr", "date": ":date[clf]", "method": ":method", "url": ":url", "status": ":status", "result_length": ":res[content-length]", "user_agent": ":user-agent", "response_time": ":response-time"}',
{ stream: logger.stream }
)
);
} else {
app.use(
morgan('{method: ":method", url: ":url", status: ":status", response_time: ":response-time"}', {
stream: logger.stream
})
);
}
// connect to our database
mongoose.Promise = require('bluebird');
const connectWithRetry = () => {
mongoose
.connect(process.env.MONGODB_URI)
.then(() => {
console.log(`DB CONNECTED (${process.env.MONGODB_URI}) `);
})
.catch(() => {
console.log('RETRY DB CONNECT in 5 seg');
setTimeout(connectWithRetry, 5000);
});
};
connectWithRetry();
// API ROUTES
//------------------------
const apiRoutesAuth = require('./backend/routes/api_auth')(app, express);
const apiRoutesUser = require('./backend/routes/api_user')(app, express);
const apiRoutesMovement = require('./backend/routes/api_movement')(app, express);
const apiRoutesSet = require('./backend/routes/api_set')(app, express);
const apiRoutesExercise = require('./backend/routes/api_exercise')(app, express);
const apiRoutesSession = require('./backend/routes/api_session')(app, express);
const apiRoutesStats = require('./backend/routes/api_stats')(app, express);
app.use('/api', apiRoutesAuth);
app.use('/api', apiRoutesUser);
app.use('/api', apiRoutesMovement);
app.use('/api', apiRoutesSet);
app.use('/api', apiRoutesExercise);
app.use('/api', apiRoutesSession);
app.use('/api', apiRoutesStats);
// MAIN CATCHALL ROUTE ---------------
// SEND USERS TO FRONTEND ------------
app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '/dist/index.html'));
});
// START THE SERVER
//------------------
let server = app.listen(config.port, () => {
console.log(`Express server ${ip.address()} listening on port ${process.env.PORT}`);
});
server = require('http-shutdown')(server);
module.exports = server; // for testing