From a4854209db4b4b682793b99c96af13b89d7dddfe Mon Sep 17 00:00:00 2001 From: Alan Friedman Date: Thu, 16 Nov 2017 18:50:28 -0500 Subject: [PATCH 1/5] Add EB config --- .ebextensions/node-settings.config | 3 ++ .ebextensions/proxy.config | 66 ++++++++++++++++++++++++++++++ .gitignore | 5 +++ package.json | 2 +- 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 .ebextensions/node-settings.config create mode 100644 .ebextensions/proxy.config diff --git a/.ebextensions/node-settings.config b/.ebextensions/node-settings.config new file mode 100644 index 0000000..1632114 --- /dev/null +++ b/.ebextensions/node-settings.config @@ -0,0 +1,3 @@ +option_settings: + aws:elasticbeanstalk:container:nodejs: + NodeCommand: "npm run-script prod" \ No newline at end of file diff --git a/.ebextensions/proxy.config b/.ebextensions/proxy.config new file mode 100644 index 0000000..11070a6 --- /dev/null +++ b/.ebextensions/proxy.config @@ -0,0 +1,66 @@ +files: + /etc/nginx/conf.d/proxy.conf: + mode: "000644" + owner: root + group: root + content: | + upstream nodejs { + server 127.0.0.1:8081; + keepalive 256; + } + + server { + listen 8080; + + if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") { + set $year $1; + set $month $2; + set $day $3; + set $hour $4; + } + access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd; + access_log /var/log/nginx/access.log main; + + location /socket.io/ { + proxy_pass http://nodejs; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + location / { + proxy_pass http://nodejs; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + gzip on; + gzip_comp_level 4; + gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; + + location /static { + alias /var/app/current/static; + } + + } + + /opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh: + mode: "000755" + owner: root + group: root + content: | + #!/bin/bash -xe + rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf + service nginx stop + service nginx start + +container_commands: + removeconfig: + command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf" \ No newline at end of file diff --git a/.gitignore b/.gitignore index fa7e9cf..3947ab5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,8 @@ node_modules npm-debug.log src/public/main.js src/.secret + +# Elastic Beanstalk Files +.elasticbeanstalk/* +!.elasticbeanstalk/*.cfg.yml +!.elasticbeanstalk/*.global.yml diff --git a/package.json b/package.json index fd8c3ec..79837f9 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ }, "scripts": { "start": "node index.js", - "prod": "npm run bundle && forever start -c \"npm start\" ./", + "prod": "npm run bundle && npm run start", "dev": "npm run bundle && gulp start", "bundle": "gulp bundle", "test": "npm run bundle && gulp test", From 9e7081769631b0f8451f14bb181fdda7ba70cc7a Mon Sep 17 00:00:00 2001 From: Alan Friedman Date: Fri, 17 Nov 2017 06:33:23 -0500 Subject: [PATCH 2/5] Add Elastic Beanstalk config --- .ebextensions/proxy.config | 6 ++++++ .ebignore | 6 ++++++ package.json | 14 ++++++++------ 3 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 .ebignore diff --git a/.ebextensions/proxy.config b/.ebextensions/proxy.config index 11070a6..3983091 100644 --- a/.ebextensions/proxy.config +++ b/.ebextensions/proxy.config @@ -9,6 +9,12 @@ files: keepalive 256; } + server { + listen 80; + + return 301 https://$host$request_uri; + } + server { listen 8080; diff --git a/.ebignore b/.ebignore new file mode 100644 index 0000000..e09f477 --- /dev/null +++ b/.ebignore @@ -0,0 +1,6 @@ +node_modules/ +.DS_STORE +# Elastic Beanstalk Files +.elasticbeanstalk/* +!.elasticbeanstalk/*.cfg.yml +!.elasticbeanstalk/*.global.yml \ No newline at end of file diff --git a/package.json b/package.json index 79837f9..f38116e 100644 --- a/package.json +++ b/package.json @@ -21,12 +21,12 @@ "slug": "^0.9.1", "socket.io": "^1.4.0", "underscore": "^1.8.3", - "uuid": "^2.0.1" - }, - "devDependencies": { + "uuid": "^2.0.1", "babel-core": "^6.5.2", "babel-preset-es2015": "^6.3.13", - "babel-register": "^6.5.2", + "babel-register": "^6.5.2" + }, + "devDependencies": { "compression": "^1.6.0", "gulp": "^3.9.1", "gulp-nodemon": "^2.0.6", @@ -50,11 +50,13 @@ }, "scripts": { "start": "node index.js", - "prod": "npm run bundle && npm run start", + "prod": "npm run start", "dev": "npm run bundle && gulp start", "bundle": "gulp bundle", "test": "npm run bundle && gulp test", - "test-travis": "node_modules/mocha/bin/mocha test/unit/lint.js --compilers js:babel-core/register && node_modules/karma/bin/karma start --single-run && node_modules/nightwatch/bin/nightwatch --test test/acceptance/index.js --config test/acceptance/nightwatch.json -e chrome" + "test-travis": "node_modules/mocha/bin/mocha test/unit/lint.js --compilers js:babel-core/register && node_modules/karma/bin/karma start --single-run && node_modules/nightwatch/bin/nightwatch --test test/acceptance/index.js --config test/acceptance/nightwatch.json -e chrome", + "predeploy": "npm run bundle", + "deploy": "eb deploy --staged" }, "author": "Daniel Seripap", "license": "MIT" From 3a9cf86ea2384377e63d45543ab83d5bbb462a20 Mon Sep 17 00:00:00 2001 From: Alan Friedman Date: Fri, 17 Nov 2017 06:35:05 -0500 Subject: [PATCH 3/5] Revert prod script changes --- .ebextensions/node-settings.config | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.ebextensions/node-settings.config b/.ebextensions/node-settings.config index 1632114..655e99d 100644 --- a/.ebextensions/node-settings.config +++ b/.ebextensions/node-settings.config @@ -1,3 +1,3 @@ option_settings: aws:elasticbeanstalk:container:nodejs: - NodeCommand: "npm run-script prod" \ No newline at end of file + NodeCommand: "npm run-script start" \ No newline at end of file diff --git a/package.json b/package.json index f38116e..48235d9 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ }, "scripts": { "start": "node index.js", - "prod": "npm run start", + "prod": "npm run bundle && forever start -c \"npm start\" ./", "dev": "npm run bundle && gulp start", "bundle": "gulp bundle", "test": "npm run bundle && gulp test", From d637bd619416123306cf939a34dd774355800375 Mon Sep 17 00:00:00 2001 From: Alan Friedman Date: Fri, 17 Nov 2017 06:44:01 -0500 Subject: [PATCH 4/5] Fix lint --- src/app.js | 3 +-- src/js/app.js | 2 +- src/room.js | 42 +++++++++++++++++++++--------------------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/app.js b/src/app.js index 8305502..d5d7bed 100644 --- a/src/app.js +++ b/src/app.js @@ -44,8 +44,7 @@ function stripName(name) { } return chatName; -}; - +} app.get('/', (req, res) => generateNewRoom(req, res, stripName(shortid.generate()))); diff --git a/src/js/app.js b/src/js/app.js index 01f5f01..2e58c7b 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -265,7 +265,7 @@ export default class App { } this._chat.log(moment().format('MMMM Do YYYY, h:mm:ss a'), {info: true}); - + $('#roomName').text(this._roomId); $('#chatNameModal').text(this._roomId); diff --git a/src/room.js b/src/room.js index 92c88c6..2985754 100644 --- a/src/room.js +++ b/src/room.js @@ -1,7 +1,7 @@ -import _ from "underscore"; -import { EventEmitter } from "events"; -import util from "util"; -import uuid from "uuid"; +import _ from 'underscore'; +import {EventEmitter} from 'events'; +import util from 'util'; +import uuid from 'uuid'; class Room { constructor(io = {}, id = {}) { @@ -15,20 +15,20 @@ class Room { const thisIO = io.of(this._id); - thisIO.on("connection", socket => { + thisIO.on('connection', socket => { let addedUser = false; // when the client emits 'new message', this listens and executes - socket.on("new message", data => { - const { username } = socket; + socket.on('new message', data => { + const {username} = socket; const isRateLimited = this.isRateLimited(username); if (isRateLimited) { - return thisIO.emit("rated", { username, id: socket.user.id }); + return thisIO.emit('rated', {username, id: socket.user.id}); } // we tell the client to execute 'new message' - socket.broadcast.emit("new message", { + socket.broadcast.emit('new message', { username, id: socket.user.id, message: data.message, @@ -40,7 +40,7 @@ class Room { }); }); - socket.on("add user", data => { + socket.on('add user', data => { if (addedUser) { return; } @@ -58,7 +58,7 @@ class Room { addedUser = true; // Broadcast to ALL sockets, including this one - thisIO.emit("user joined", { + thisIO.emit('user joined', { username: socket.username, numUsers: this.numUsers, users: this.users @@ -66,23 +66,23 @@ class Room { }); // when the client emits 'typing', we broadcast it to others - socket.on("typing", () => { - socket.broadcast.emit("typing", { username: socket.username }); + socket.on('typing', () => { + socket.broadcast.emit('typing', {username: socket.username}); }); // when the client emits 'stop typing', we broadcast it to others - socket.on("stop typing", () => { - socket.broadcast.emit("stop typing", { username: socket.username }); + socket.on('stop typing', () => { + socket.broadcast.emit('stop typing', {username: socket.username}); }); // when the user disconnects.. perform this - socket.on("disconnect", () => { + socket.on('disconnect', () => { if (addedUser) { --this.numUsers; this.users = _.without(this.users, socket.user); // echo globally that this client has left - socket.broadcast.emit("user left", { + socket.broadcast.emit('user left', { username: socket.username, numUsers: this.numUsers, users: this.users, @@ -91,13 +91,13 @@ class Room { // remove room from rooms array if (this.numUsers === 0) { - this.emit("empty"); + this.emit('empty'); } } }); // Update user - socket.on("update user", data => { + socket.on('update user', data => { const newUsername = this.sanitizeUsername(data.newUsername); if (newUsername.length > 16) { @@ -112,7 +112,7 @@ class Room { socket.username = user.username = newUsername; socket.user = user; - thisIO.emit("user update", { + thisIO.emit('user update', { username: socket.username, id: socket.user.id }); @@ -151,7 +151,7 @@ class Room { } sanitizeUsername(str) { - return str.replace(/[^A-Za-z0-9]/g, "-"); + return str.replace(/[^A-Za-z0-9]/g, '-'); } roomId() { From f0412791fef17903015b45b7c035fb1d7924311c Mon Sep 17 00:00:00 2001 From: Alan Friedman Date: Fri, 17 Nov 2017 06:52:25 -0500 Subject: [PATCH 5/5] Disable nginx logging --- .ebextensions/proxy.config | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.ebextensions/proxy.config b/.ebextensions/proxy.config index 3983091..7a1e4b3 100644 --- a/.ebextensions/proxy.config +++ b/.ebextensions/proxy.config @@ -24,8 +24,13 @@ files: set $day $3; set $hour $4; } - access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd; - access_log /var/log/nginx/access.log main; + + # Turn off default AWS logging + # access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd; + # access_log /var/log/nginx/access.log main; + + access_log off; + error_log off; location /socket.io/ { proxy_pass http://nodejs;