Skip to content

Instantly share code, notes, and snippets.

@jpotts18
Created August 27, 2013 17:10
Show Gist options
  • Save jpotts18/6356300 to your computer and use it in GitHub Desktop.
Save jpotts18/6356300 to your computer and use it in GitHub Desktop.
// creating global parameters and start
// listening to 'port', we are creating an express
// server and then we are binding it with socket.io
var express = require('express'),
app = express(),
util = require('util'),
env = process.env.NODE_ENV || 'development',
server = require('http').createServer(app),
io = require('socket.io').listen(server),
gendb = require('./test/generator'),
// ROUTING
api = require('./routes/api_routes'),
storeApi = require('./routes/store_routes'),
secured = require('./routes/secured'),
// CONTROLLERS
auth = require('./controllers/auth'),
conn = require('./controllers/connection'),
chat = require('./controllers/chat'),
coin = require('./controllers/coin'),
power = require('./controllers/power'),
like = require('./controllers/like'),
flag = require('./controllers/flag'),
push = require('./controllers/push'),
// CONFIGS AND GLOBALS
Config = require('./config/config.'+env+'.json'),
Manager = require('./Manager');
coinTimer = Config.coinTimer;
port = Config.port;
server.listen(port);
console.log('Current ENV = '+env);
/* COMMAND LINE INTERFACE */
var arg1 = process.argv[2];
if(env === 'production'){
if(arg1 === 'migrate'){ models.migrate(); }
}
if(env === 'development' || env == 'test'){
console.log('CLI ARG = '+arg1);
//REBUILD THE database from scratch
if(arg1 === 'rebuild'){
models.synchronize(function(success){
if(success){
console.log('*** SYNC FINISHED ***')
gendb.createFixtures();
}else{
console.log('database error not synched');
}
});
}
if(arg1 === 'clean'){
models.synchronize(function(success){
if(success){
console.log('*** SYNC FINISHED ***');
gendb.createClean();
}else{
console.log('database not synched');
}
})
}
}
/** EXPRESS CONFIG **/
app.use(express.bodyParser());
app.use("/styles", express.static(__dirname + '/public/styles'));
app.use("/scripts", express.static(__dirname + '/public/scripts'));
app.use("/images", express.static(__dirname + '/public/images'));
/** ROUTING */
// authentication endpoints
app.post('/api/auth/register', auth.postRegister);
app.post('/api/auth/login', auth.postLogin);
app.post('/api/auth/twitter/register', auth.postTwitterRegister);
app.post('/api/auth/twitter/login', auth.postTwitterLogin);
app.post('/api/auth/fb/register', auth.postFbRegister);
app.post('/api/auth/fb/login', auth.postFbLogin);
// accessing models
app.get('/api/games/upcoming', api.getGamesUpcoming);
app.get('/api/games/:id/rooms', api.getRoomsByGame);
// app.get('/api/teams', api.getTeams);
app.get('/api/powerups', api.getPowers);
app.get('/api/inventory', api.getInventory);
app.get('/api/comments/:id', api.getCommentById);
// SECURED
app.get('/api/user/powerups', secured.getUserPowers);
app.get('/api/users/:id', secured.getUserById);
app.post('/api/users/:id/follow', secured.postUserFollow);
app.post('/api/users/:id/unfollow', secured.postUserUnfollow);
app.get('/api/notifications', secured.getUserNotifications);
app.post('/api/transactions/powerups', secured.postTransPowers);
app.post('/api/transactions/coins', secured.postCoinTrans);
app.post('/api/devices/apple', secured.postAppleDevices);
app.post('/api/comments/:id/like', secured.postCommentLike);
app.post('/api/comments/:id/flag', secured.postCommentFlag);
app.put('/api/user', secured.updateUser);
app.get('/api/user', secured.getUser);
//test suite
app.get('/testing/debug', gendb.getDebug);
/////////////////////////////
// Socket.IO Configuration //
/////////////////////////////
/* Production configuration */
io.configure('production', function(){
io.enable('browser client etag');
io.set('log level', 1);
io.set('transports', [
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
});
/* Development configuration */
io.configure('development', function(){
io.set('transports', ['websocket']);
io.set('log level',2);
});
/* IO Bindings Events */
io.sockets.on('connection', function(socket){
socket.on( 'connect', function(data) { conn.connect (socket, data); });
socket.on( 'disconnect', function() { conn.disconnect (socket); });
socket.on( 'message', function(data) { chat.message (socket, data); });
socket.on( 'addCoins' , function() { coin.addCoins (socket) });
socket.on( 'usePower' , function(data) { power.usePower (socket, data); });
socket.on( 'likeComment', function(data) { like.comment (socket, data); });
socket.on( 'flagComment', function(data) { flag.comment (socket, data); })
});
// show a message in console
console.log('Chat server is running and listening to port %d...', port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment