diff --git a/app.js b/app.js index 6c50cd3..df575b5 100644 --- a/app.js +++ b/app.js @@ -1,25 +1,14 @@ // Requires var favicon = require('serve-favicon'); +var pack = require("./package.json"); var s = require("underscore.string"); var readline = require('readline'); var express = require('express'); var sockjs = require('sockjs'); var https = require('https'); var chalk = require('chalk'); -var path = require('path'); var fs = require('fs'); -//Library Wrappers -var pack = require("./package.json"); -var logger = require('./lib/logger/'); -var mysql = require('./lib/mysql/'); - -//Middleware -var loginCheck = require('./middleware/loginCheck.js'); - -//Routes -var routes = require('./routes/index.js'); - //App var app = express(); @@ -27,11 +16,17 @@ var app = express(); var config = { log: true, readline: false, //This is breaking on some machines, also to be deprecated with express routes. - ipadr: '127.0.0.1', - port: 3000, ssl: false }; +// Config +if(config.ssl) { + config.key = fs.readFileSync('/path/to/your/ssl.key'); + config.cert = fs.readFileSync('/path/to/your/ssl.crt') +} + +var port = normalizePort(process.env.PORT || '3000'); + var logInfo = chalk.bold.blue('[Info] '); var logStop = chalk.bold.red.dim('[Stop] '); var logPM = chalk.bold.yellow.dim('[PM] '); @@ -53,16 +48,6 @@ var uid = 1; var alphanumeric = /^\w+$/; - -// Config -if(config.ssl) { - var options = { - key: fs.readFileSync('/path/to/your/ssl.key'), - cert: fs.readFileSync('/path/to/your/ssl.crt') - }, - server = https.createServer(options); -} - if(config.readline) { var rl = readline.createInterface(process.stdin, process.stdout); rl.setPrompt('[--:--:--][CONSOLE] '); @@ -71,35 +56,21 @@ if(config.readline) { // Express -//app.use(logger.express); +app.set('port', port); app.set('view engine', 'ejs'); app.use(favicon(__dirname + '/public/img/favicon.png')); - -//Login Check -app.use(loginCheck); -/* -Sets up res.locals.user object to be used. -res.locals.user.logged_in contains boolean for logged_in user. -*/ +app.locals.version = pack.version; //Routes app.use('/chat', express.static(__dirname + '/public')); app.get('/chat', function (req, res) { - res.render('pages/index', {version:pack.version}); + res.render('index', {version:pack.version}); }); app.use('/', function(req, res, next){ res.redirect('/chat'); //Temp redirect pre-login }); - -// Connections -var server = app.listen(config.port, config.ipadr, function() { - var host = server.address().address, - port = server.address().port; - - consoleLog(logStart, 'Listening at http://' + host + ':' + port); -}); chat.on('connection', function(conn) { consoleLog(logSocket, chalk.underline(conn.id) +': connected'); @@ -189,9 +160,6 @@ chat.on('connection', function(conn) { }); }); -chat.installHandlers(server, {prefix:'/socket',log:function(){}}); - - // Util function updateUser(id, name) { if(name.length > 2 && name.length < 17 && name.indexOf(' ') < 0 && !checkUser(name) && name.match(alphanumeric) && name != 'Console' && name != 'System') { @@ -431,3 +399,71 @@ function readLine() { process.exit(0); }); } + +var server; + +if (!config.ssl){ + var http = require('http'); + server = http.createServer(app); +} else { + var https = require('https'); + var opt = { + key: config.key, + cert: config.cert + }; + server = https.createServer(opt, app); +} + +server.listen(port); +server.on('error', onError); +server.on('listening', onListening); + +function normalizePort(val) { + var port = parseInt(val, 10); + + if (isNaN(port)) { + // named pipe + return val; + } + + if (port >= 0) { + // port number + return port; + } + + return false; +} + +function onError(error) { + if (error.syscall !== 'listen') { + throw error; + } + + var bind = typeof port === 'string' + ? 'Pipe ' + port + : 'Port ' + port; + + // handle specific listen errors with friendly messages + switch (error.code) { + case 'EACCES': + console.error(bind + ' requires elevated privileges'); + process.exit(1); + break; + case 'EADDRINUSE': + console.error(bind + ' is already in use'); + process.exit(1); + break; + default: + throw error; + } +} + +function onListening() { + var addr = server.address(); + var bind = typeof addr === 'string' + ? 'pipe ' + addr + : 'port ' + addr.port; + consoleLog(logStart, 'Listening at '+bind); +} + +chat.installHandlers(server, {prefix:'/socket',log:function(){}}); \ No newline at end of file diff --git a/lib/logger/index.js b/lib/logger/index.js deleted file mode 100644 index d9521fe..0000000 --- a/lib/logger/index.js +++ /dev/null @@ -1,7 +0,0 @@ -var logger = require('jethro'); - -logger.init({ - quickStart: true -}); - -module.exports = logger; diff --git a/lib/mysql/index.js b/lib/mysql/index.js deleted file mode 100644 index 3476c36..0000000 --- a/lib/mysql/index.js +++ /dev/null @@ -1,14 +0,0 @@ -var mysql = require('mysql'); -var config = require('../../config/mysql.json'); -var logger = require('jethro'); - -var connection = mysql.createConnection({ - host : config.host, - user : config.username, - password : config.password, - database : config.database -}); - -connection.connect(); - -module.exports = connection; diff --git a/middleware/loginCheck.js b/middleware/loginCheck.js deleted file mode 100644 index 070a9ea..0000000 --- a/middleware/loginCheck.js +++ /dev/null @@ -1,5 +0,0 @@ -var cookie = require('cookie'); -module.exports = function(req, res, next){ - console.log(req); - next(); -}; \ No newline at end of file diff --git a/package.json b/package.json index d619eb8..e7fec80 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,11 @@ { "name": "node-chat", - "version": "0.21.9", + "version": "0.22.0", "private": true, "scripts": { "start": "node app.js" }, "dependencies": { - "bcryptjs": "^2.1.0", "body-parser": "~1.12.0", "cookie": "^0.1.2", "cookie-parser": "~1.3.4", @@ -14,9 +13,6 @@ "ejs": "~2.3.1", "express": "~4.12.2", "jade": "~1.9.2", - "jethro": "^1.2.7", - "jsonwebtoken": "^4.2.2", - "mysql": "^2.6.0", "readline": "0.0.7", "serve-favicon": "~2.2.0", "sockjs": "~0.3.15", diff --git a/public/js/chat.js b/public/js/chat.js index b6eae76..b1e09bf 100644 --- a/public/js/chat.js +++ b/public/js/chat.js @@ -31,12 +31,11 @@ var settings = { 'desktop': false, 'synthesis': false, 'recognition': false -} - +}; // Connection var connect = function() { - socket = new WebSocket('ws://localhost:3000/socket/websocket'); + socket = new WebSocket('ws://'+ window.location.host +'/socket/websocket'); socket.onopen = function() { var ping = setInterval(function(){ diff --git a/routes/index.js b/routes/index.js deleted file mode 100644 index e69de29..0000000 diff --git a/views/pages/index.ejs b/views/index.ejs similarity index 100% rename from views/pages/index.ejs rename to views/index.ejs diff --git a/views/pages/login.ejs b/views/pages/login.ejs deleted file mode 100644 index e69de29..0000000