diff --git a/README.md b/README.md index 34dbeb0..4626603 100644 --- a/README.md +++ b/README.md @@ -30,46 +30,8 @@ Install `Node.js`: Install Node Package Manager (`npm`): See instructions at http://github.com/isaacs/npm. -Install `Express.js` and `Connect` (included automatically): - npm install express - -Compile the daemon add-on if you plan on letting the server daemonize itself: - cd server/libs/daemon - node-waf configure build - cp build/default/daemon.node . - rm -rf build - -## Installation for Development - -If you want to test Ajax IM as a standalone app for development, you will need -to install [`Jade`](http://github.com/visionmedia/jade) as well. - -To install `Jade`: - npm install jade - -## Starting up the server - -Starting the server in _development_ mode is as simple as: - node server/app.js - -To start the server in _production_ mode: - EXPRESS_ENV=production node server/app.js - -To start the server as a _daemon_ in _production_ mode: - node server/app.js start production - -## Testing it out - -Once the server is up and running in _development_ mode, you can test it out -through the included development testing app. The below instructions are -assuming that you have left all default configuration options. If not, please -replace the host/port values with the correct ones. - -To get started, first initialize a session cookie by going to: - http://localhost:8000/dev/cookie - -Then head over to the development page that will initialize the client: - http://localhost:8000/dev/ +Install `Socket.io` (included automatically): + npm install socket-io That's it! @@ -82,7 +44,7 @@ That's it! ## Node Compatibility The `master` branch of Ajax IM is compatible with node --version: - v0.2.5 + v0.4.1 ## Contributing diff --git a/server/app.js b/server/app.js index cb9d37a..ae06ebf 100755 --- a/server/app.js +++ b/server/app.js @@ -1,30 +1,84 @@ #!/usr/bin/env node var http = require('http'), - io = require('./libs/socket.io'), + url = require('url'), + fs = require('fs'), + io = require('socket.io'), + uglifyjs, o_ = require('./libs/utils'), + client = { + 'im.js': [ + 'intro.js', + 'cookies.js', 'dateformat.js', 'json.js', 'md5.js', 'store.js', + /*'templates.js',*/ 'im.js', + 'outro.js' + ] + }, + contentTypes = { + js: 'text/javascript' + }, + _clientFiles = {}, server, socket; +try { var uglifyjs = require('uglify-js'); } catch(e) {} o_.merge(global, require('./settings')); try { o_.merge(global, require('./settings.local')); } catch(e) {} -server = http.createServer(function(req, res) { - // Any other server code goes here. - res.writeHead(200, {'Content-Type': 'text/html'}); - res.end(''); -}); +server = http.createServer(_serveClient); server.listen(APP_PORT, APP_HOST); -var auth_handler = require('./auth/' + AUTH_LIBRARY)(), - session_store = require('./session/' + SESSION_STORE)(), - msg_handler = require('./message/' + MESSAGE_HANDLER)(auth_handler, session_store); +var authHandler = require('./auth/' + AUTH_LIBRARY)(), + sessionStore = require('./session/' + SESSION_STORE)(), + msgHandler = require('./message/' + MESSAGE_HANDLER)(authHandler, sessionStore); +// setup socket.io socket = io.listen(server); socket.on('connection', function(client) { client.on('message', function(message) { - msg_handler.message(client, message); + msgHandler.message(client, message); }); client.on('disconnect', function() { - msg_handler.disconnect(client, SESSION_TIMEOUT); + msgHandler.disconnect(client, SESSION_TIMEOUT); }); }); + +// compile client javascript +for(var file in client) { + var fileData = ""; + for(var i = 0, fl = client[file].length; i < fl; i++) + fileData += fs.readFileSync( + __dirname + '/../client/js/' + client[file][i], + 'utf8' + ); + var ext = file.split('.').pop(); + if(ext == 'js' && uglifyjs) { + // if uglify-js is installed, let's compress + fileData = uglifyjs.parser.parse(fileData); + fileData = uglifyjs.uglify.ast_mangle(fileData); + fileData = uglifyjs.uglify.ast_squeeze(fileData); + fileData = uglifyjs.uglify.gen_code(fileData); + } + _clientFiles[file] = { + headers: { + 'Content-Length': fileData.length, + 'Content-Type': contentTypes[ext] + // Should use ETag + }, + content: fileData, + encoding: 'utf8' + }; +}; + +// serve client javascript +function _serveClient(req, res) { + var path = url.parse(req.url).pathname, + file = path.substr(1); + + if(req.method == 'GET' && file in _clientFiles) { + res.writeHead(200, _clientFiles[file].headers); + res.end(_clientFiles[file].content, _clientFiles[file].encoding); + } else { + res.writeHead(404); + res.end('404'); + } +} \ No newline at end of file diff --git a/server/settings.js b/server/settings.js index d9f6796..8cd001d 100644 --- a/server/settings.js +++ b/server/settings.js @@ -14,7 +14,7 @@ APP_HOST = 'localhost'; APP_PORT = 8000; // Document me! -AUTH_LIBRARY = 'default'; +AUTH_LIBRARY = 'example'; SESSION_STORE = 'memory'; MESSAGE_HANDLER = 'default';