Skip to content

Commit

Permalink
Add some tweaks for api and socket.io server:
Browse files Browse the repository at this point in the history
- Remove access token when send debug-data to logger
- Do not log OPTIONS request
- Make socket.io server accept http request so we can do healthcheck
  • Loading branch information
tobernguyen committed Aug 18, 2016
1 parent 3b76261 commit fe6b7bb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
41 changes: 23 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ const app = express();
*/
app.set('port', process.env.PORT || 3000);
app.use(compression());
app.use(bodyParser.json());
app.use(expressValidator());
app.all('/*', function(req, res, next) {
// CORS headers
res.header('Access-Control-Allow-Origin', '*'); // restrict it to the required domain
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// Set custom headers for CORS
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
if (req.method == 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});

if (process.env.NODE_ENV !== 'test') {
app.use(require('express-bunyan-logger')({
name: process.env.LOGGER_NAME,
Expand All @@ -43,6 +58,10 @@ if (process.env.NODE_ENV !== 'test') {
meta['debug-data'] = {
req: _.pick(meta['req'], ['headers', 'params', 'query', 'body', 'user'])
};

// Delete access token from debug-data
_.unset(meta, 'debug-data.req.headers.x-access-token');

return 'fatal';
} else if (meta['response-time'] > 500) {
meta['debug-data'] = {
Expand All @@ -54,11 +73,11 @@ if (process.env.NODE_ENV !== 'test') {
}
},
excludes: [
'remote-address',
'remote-address',
'pid', 'req_id',
'ip', 'referer',
'user-agent',
'short-body',
'ip', 'referer',
'user-agent',
'short-body',
'body', 'response-hrtime',
'http-version',
'req-headers',
Expand All @@ -68,20 +87,6 @@ if (process.env.NODE_ENV !== 'test') {
]
}));
}
app.use(bodyParser.json());
app.use(expressValidator());
app.all('/*', function(req, res, next) {
// CORS headers
res.header('Access-Control-Allow-Origin', '*'); // restrict it to the required domain
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// Set custom headers for CORS
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
if (req.method == 'OPTIONS') {
res.status(200).end();
} else {
next();
}
});

// Auth Middleware - This will check if the token is valid
// Only the requests that start with /api/v1/* will be checked for the token.
Expand Down
14 changes: 11 additions & 3 deletions socket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
*/
require('./libs/load-env');

var io = require('socket.io')(process.env.SOCKET_IO_PORT);
var app = require('http').createServer(handler);

function handler (req, res) {
res.writeHead(200);
res.end();
}

var io = require('socket.io')(app);
var redis = require('socket.io-redis');
io.adapter(redis(process.env.REDIS_URI.replace('redis://', ''), {key: process.env.SOCKET_IO_REDIS_PREFIX || 'socket.io'}));

Expand All @@ -12,8 +19,6 @@ process.env.LOGGER_NAME = process.env.SOCKET_IO_LOGGER_NAME;
var logger = require('./libs/logger');
var validateSocketIOToken = require('./middlewares/validateSocketIOToken');

logger.info(`SocketIO server listening on port ${process.env.SOCKET_IO_PORT} in ${process.env.NODE_ENV} mode`);

io.on('connection', validateSocketIOToken)
.on('authenticated', socket => {
logger.debug('User connected with ID: ' + socket.user.id);
Expand All @@ -24,3 +29,6 @@ io.on('connection', validateSocketIOToken)
// Join current socket to private channel
socket.join(`user.${socket.user.id}`);
});

app.listen(process.env.SOCKET_IO_PORT);
logger.info(`SocketIO server listening on port ${process.env.SOCKET_IO_PORT} in ${process.env.NODE_ENV} mode`);
6 changes: 3 additions & 3 deletions test/features/admin-manage-shop.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('GET /api/v1/admin/shops/:id', () => {
});
});

describe('GET /api/v1/admin/shops/', () => {
describe('GET /api/v1/admin/shops/', function() {
let adminToken, normalUserAccessToken, createdShop, owner;

before(done => {
Expand All @@ -79,8 +79,8 @@ describe('GET /api/v1/admin/shops/', () => {
});

describe('with admin access token', function() {
this.retries(5);
it('should return 200 OK and return an array which contain created shop info', (done) => {
it('should return 200 OK and return an array which contain created shop info', function(done) {
this.retries(5);
request(app)
.get('/api/v1/admin/shops/')
.set('X-Access-Token', adminToken)
Expand Down

0 comments on commit fe6b7bb

Please sign in to comment.