Skip to content

Commit

Permalink
v0.22.0 - Cleanup, removed unused variables, libs and deps. Rewrote s…
Browse files Browse the repository at this point in the history
…erver initialisation.
  • Loading branch information
Sam Mills (Henchman) committed May 26, 2015
1 parent f5f0ca2 commit e9ba3ca
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 78 deletions.
124 changes: 80 additions & 44 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
// 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();

// Variables
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] ');
Expand All @@ -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] ');
Expand All @@ -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');
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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(){}});
7 changes: 0 additions & 7 deletions lib/logger/index.js

This file was deleted.

14 changes: 0 additions & 14 deletions lib/mysql/index.js

This file was deleted.

5 changes: 0 additions & 5 deletions middleware/loginCheck.js

This file was deleted.

6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
{
"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",
"debug": "~2.1.1",
"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",
Expand Down
5 changes: 2 additions & 3 deletions public/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
Empty file removed routes/index.js
Empty file.
File renamed without changes.
Empty file removed views/pages/login.ejs
Empty file.

0 comments on commit e9ba3ca

Please sign in to comment.