Skip to content

Instantly share code, notes, and snippets.

@dhigginbotham
Created January 30, 2013 18:29
Show Gist options
  • Save dhigginbotham/4675485 to your computer and use it in GitHub Desktop.
Save dhigginbotham/4675485 to your computer and use it in GitHub Desktop.
/**
* Module dependencies.
Current Version: 0.0.3
*/
var express = require('express')
, io = require('socket.io')
, sioHelper = require('./models/socketioLoader')
, routes = require('./routes')
, user = require('./routes/user')
, channel = require('./routes/channel')
, mongo = require('./models/mongo')
, ds = require('./models/datastore')
, http = require('http')
, https = require('https')
, fs = require('fs')
, routeData = require('./routes/routes')
, path = require('path');
var app = express(),
server = http.createServer(app),
io = io.listen(server);
var user;
server.listen(3000);
var hskey = fs.readFileSync('privkey.pem');
var hscert = fs.readFileSync('privcert.pem');
var options = {
key: hskey,
cert: hscert,
rejectUnauthorized: false
}
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('1aldwych'));
app.use(express.session());
app.use(app.router);
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.static(path.join(__dirname, 'tests')));
app.use(express.errorHandler());
});
//sioHelper.init(io,ds);
/*
prefix urls with /api/
data interactions sorted by role:
/api/account/
GET:
POST: Creates account, needs facebook auth for prepopulated fields, return token
parameters: usertype, facebookid, name, etc.
/api/properties/
GET: Get userid or listingid for some reason that will make more sense later
POST: Requires auth token. validate user type & id for security
DELETE:
params:
/api/log/
POST: Action or Cat values
*/
// Loop through our routeData function - it gives us the wiring for HTTP requests AND socket IO
var routeConfig = routeData.routeData(ds, routes);
// Begin a connection with socket io, apply same config
io.sockets.on('connection', function(socket){
console.log('Client Connected');
for ( var i =0; i < routeConfig.length; i++) {
console.dir(routeConfig[i].listener);
console.dir(routeConfig[i].path);
try {
socket.on(routeConfig[i].path, routeConfig[i].listener);
} catch(e) {
console.dir(e);
// e is our error details
}
// NOTE: SEE THE 'ELSE' FOR DEFAULT GET HANDLER
if (routeConfig[i].method.toLowerCase() == "post") {
app.post(routeConfig[i].path, routeConfig[i].listener);
} else if (routeConfig[i].method.toLowerCase() == "head") {
app.head(routeConfig[i].path, routeConfig[i].listener);
} else if (routeConfig[i].method.toLowerCase() == "options") {
app.options(routeConfig[i].path, routeConfig[i].listener);
} else if (routeConfig[i].method.toLowerCase() == "put") {
app.put(routeConfig[i].path, routeConfig[i].listener);
} else if (routeConfig[i].method.toLowerCase() == "delete") {
app.delete(routeConfig[i].path, routeConfig[i].listener);
} else {
// THIS IS THE DEFAULT HANDLER - ASSUME GET REQUEST!!!
app.get(routeConfig[i].path, routeConfig[i].listener);
}
}
});
app.get('/', routes.index);
app.post('/', routes.index);
app.get('/test/routes', function(req, res) {
res.end(JSON.stringify(routeConfig));
});
https.createServer(options, app).listen(443, function() {
console.log("SSL server listing on port 443");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment