Skip to content

Commit

Permalink
allow to disable accesslog (ENABLE_MIDDLEWARE_ACCESS_LOG, default fal…
Browse files Browse the repository at this point in the history
…se) and compression (ENABLE_MIDDLEWARE_COMPRESSION, default true)
  • Loading branch information
majodev committed Feb 6, 2023
1 parent b70c05e commit d3ccd37
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 24 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
- "8080:8080" # production
- "35729:35729" # livereload
- "5858:5858" # debugger
- "9229:9229" # profiler
working_dir: &PROJECT_ROOT_DIR /app
# linux permissions: we must explicitly run as the node user
user: node
Expand Down
2 changes: 1 addition & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require('./routes')(app);

// Start server
server.listen(config.port, config.ip, function () {
console.log('Express server listening on %d, in %s mode (timeout=%dms)', config.port, app.get('env'), server.timeout);
console.log('Express server listening on %d, in %s mode (timeout=%dms, compress=%s, accesslog=%s)', config.port, app.get('env'), server.timeout, config.enableMiddlewareCompression, config.enableMiddlewareAccessLog);
debug("debug enabled.");
});

Expand Down
4 changes: 1 addition & 3 deletions server/config/environment/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

// Development specific configuration
// ==================================
module.exports = {
seedDB: true
};
module.exports = {};
7 changes: 4 additions & 3 deletions server/config/environment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ var all = {
port: process.env.PORT
|| 9000,

// Should we populate the DB with sample data?
seedDB: false,

// Server port
serverTimeout: process.env.TIMEOUT_MS
|| 60 * 1000, // 60 seconds

// Middlewares
enableMiddlewareAccessLog: process.env.ENABLE_MIDDLEWARE_ACCESS_LOG === 'true' ? true : false, // default false
enableMiddlewareCompression: process.env.ENABLE_MIDDLEWARE_COMPRESSION === 'false' ? false : true // default true
};

// Export the config object based on the NODE_ENV
Expand Down
6 changes: 2 additions & 4 deletions server/config/environment/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
// =================================
module.exports = {
// Server IP
ip: process.env.OPENSHIFT_NODEJS_IP
|| process.env.IP
ip: process.env.IP
|| undefined,

// Server port
port: process.env.OPENSHIFT_NODEJS_PORT
|| process.env.PORT
port: process.env.PORT
|| 8080,
};
19 changes: 11 additions & 8 deletions server/config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
'use strict';

var express = require('express');
var morgan = require('morgan');
var compression = require('compression');
var path = require('path');
var config = require('./environment');

Expand All @@ -16,20 +14,25 @@ module.exports = function (app) {
app.set('views', config.root + '/server/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(compression());

if ('production' === env) {
if (config.enableMiddlewareCompression) {
app.use(require('compression')());
}

if (env === 'production') {
app.use(express.static(path.join(config.root, 'public')));
app.set('appPath', config.root + '/public');
app.use(morgan(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]'));
if (config.enableMiddlewareAccessLog) {
app.use(require('morgan')(':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]'));
}
}

if ('development' === env || 'test' === env) {
if (env === 'development' || env === 'test') {
app.use(require('connect-livereload')());
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(path.join(config.root, 'client')));
app.set('appPath', 'client');
app.use(morgan('dev'));
app.set('appPath', config.root + '/client');
app.use(require('morgan')('dev'));
app.use(require('errorhandler')()); // Error handler - has to be last
}
};
14 changes: 14 additions & 0 deletions server/logic/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,20 @@ _.mixin({
// Exports for REST API
// -----------------------------------------------------------------------------

module.exports.healthy = function healthy() {

var events = emitter.eventNames();
var listenersCount = _.reduce(_.map(events, function (event) {
return emitter.listenerCount(event);
}), function (sum, n) {
return sum + n;
}, 0);

return `${cachedFonts.length} fonts.
Cached ${_.keys(fileStore).length} variants, ${_.sumBy(_.values(fileStore), function (f) { return (f.files) ? f.files.length : 0; })} files (${events.length}/${listenersCount} in-flight).
${events.length > 0 ? events.join(", ") : "No in-flight requests."}`;
}

module.exports.getAll = function getAll(callback) {
if (cachedFonts.length > 0) {
callback(cachedFonts);
Expand Down
17 changes: 12 additions & 5 deletions server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
'use strict';

var errors = require('./components/errors');
var core = require('./logic/core');

module.exports = function(app) {
module.exports = function (app) {

// Insert routes below
app.use('/api/fonts', require('./api/font'));


app.route('/-/healthy')
.get(function (req, res) {
res.type('text/plain');
res.send(core.healthy());
});

// All undefined asset or api routes should return a 404
app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get(errors[404]);
.get(errors[404]);

// All other routes should redirect to the index.html
app.route('/*')
.get(function(req, res) {
res.sendfile(app.get('appPath') + '/index.html');
.get(function (req, res) {
res.sendFile(app.get('appPath') + '/index.html');
});
};

0 comments on commit d3ccd37

Please sign in to comment.