forked from galenmaly/lighterpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·113 lines (96 loc) · 3.25 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
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const compression = require('compression');
const config = require('config');
const express = require('express');
const morgan = require('morgan');
const uuid = require('uuid');
const { logger } = require('./server/log.js');
morgan.token('username', function getUsername (req) {
return req.lighterpackusername
});
morgan.token('requestid', function getUsername (req) {
return req.uuid
});
const app = express();
app.enable('trust proxy');
app.use(function (req, res, next) {
req.uuid = uuid.v4();
next();
});
app.use(morgan(function (tokens, req, res) {
return JSON.stringify({
'timestamp': tokens.date(req, res, 'iso'),
'requestid': tokens.requestid(req, res),
"remote-addr": tokens['remote-addr'](req, res),
'method': tokens.method(req, res),
'http-version': tokens['http-version'](req, res),
'user-agent': tokens['user-agent'](req, res),
'url': tokens.url(req, res),
'status': tokens.status(req, res),
'referrer': tokens.referrer(req, res),
'content-length': tokens.res(req, res, 'content-length'),
'response-time': tokens['response-time'](req, res),
'username': tokens.username(req, res),
})
}, { stream: logger.stream.write }));
const oneDay = 86400000;
app.use(compression());
app.use(cookieParser());
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({
extended: true,
limit: '50mb',
}));
app.use(express.static(`${__dirname}/public/`, { maxAge: oneDay }));
const endpoints = require('./server/endpoints.js');
const moderationEndpoints = require('./server/moderation-endpoints.js');
const views = require('./server/views.js');
app.use('/', endpoints);
app.use('/', moderationEndpoints);
app.use('/', views);
logger.info("Starting up Lighterpack...");
if (config.get('environment') === 'production') {
webpackConfig = require('./webpack.config');
} else {
webpackConfig = require('./webpack.development.config');
}
webpackCompiler = webpack(webpackConfig);
// Default port is 3000; we can have multiple bindings
config.get('bindings').map(
(bind) => {
app.listen(config.get('port'), bind);
logger.info(`Listening on [${bind}]:${config.get('port')}`);
},
);
if (config.get('environment') !== 'production') {
new WebpackDevServer(webpack(webpackConfig), {
historyApiFallback: true,
disableHostCheck: true,
publicPath: webpackConfig.output.publicPath,
hot: true,
proxy: {
'*': {
target: `http://localhost:${config.get('port')}`,
secure: false,
changeOrigin: true,
},
},
stats: {
cached: false,
cachedAssets: false,
colors: { level: 2 },
},
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
},
}).listen(config.get('devServerPort'), (err, result) => {
if (err) {
return logger.info(err);
}
logger.info(`Webpack dev server listening on port ${config.get('devServerPort')}`);
});
}