diff --git a/.ebextensions/node-settings.config b/.ebextensions/node-settings.config new file mode 100644 index 0000000..655e99d --- /dev/null +++ b/.ebextensions/node-settings.config @@ -0,0 +1,3 @@ +option_settings: + aws:elasticbeanstalk:container:nodejs: + NodeCommand: "npm run-script start" \ No newline at end of file diff --git a/.ebextensions/proxy.config b/.ebextensions/proxy.config new file mode 100644 index 0000000..7a1e4b3 --- /dev/null +++ b/.ebextensions/proxy.config @@ -0,0 +1,77 @@ +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 80; + + return 301 https://$host$request_uri; + } + + 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; + } + + # 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; + 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/.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/.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..48235d9 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", @@ -54,7 +54,9 @@ "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" 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() {