Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 824458a2d4eb285841f9bd88418ac68d7431f8f3 @ernie committed Aug 7, 2011
@@ -0,0 +1,116 @@
+var http = require('http'),
+ sys = require('sys'),
+ nodeStatic = require('node-static/lib/node-static'),
+ faye = require('faye/faye-node'),
+ url = require('url');
+
+function LiveStats(options) {
+ if (! (this instanceof arguments.callee)) {
+ return new arguments.callee(arguments);
+ }
+
+ var self = this;
+
+ self.settings = {
+ port: options.port,
+ geoipServer: {
+ hostname: options.geoipServer.hostname
+ , port: options.geoipServer.port || 80
+ }
+ };
+
+ self.init();
+};
+
+LiveStats.prototype.init = function() {
+ var self = this;
+
+ self.bayeux = self.createBayeuxServer();
+ self.httpServer = self.createHTTPServer();
+
+ self.bayeux.attach(self.httpServer);
+ self.httpServer.listen(self.settings.port);
+
+ sys.log('Server started on PORT ' + self.settings.port);
+};
+
+LiveStats.prototype.createHTTPServer = function() {
+ var self = this;
+
+ var server = http.createServer(function(request, response) {
+ var file = new nodeStatic.Server('./public', {
+ cache: false
+ });
+
+ request.addListener('end', function() {
+ var location = url.parse(request.url, true),
+ params = (location.query || request.headers);
+ if (location.pathname == '/config.json' && request.method == 'GET') {
+ response.writeHead(200, {
+ 'Content-Type': 'application/x-javascript'
+ });
+ var jsonString = JSON.stringify({
+ port: self.settings.port
+ });
+ response.write(jsonString);
+ response.end();
+ } else if (location.pathname == '/stat' && request.method == 'GET') {
+ self.ipToPosition(params.ip, function(latitude, longitude, city) {
+ self.bayeux.getClient().publish('/stat', {
+ title: params.title
+ , latitude: latitude
+ , longitude: longitude
+ , city: city
+ , ip: params.ip
+ });
+ });
+
+ response.writeHead(200, {
+ 'Content-Type': 'text/plain'
+ });
+ response.write('OK');
+ response.end();
+ } else {
+ file.serve(request, response);
+ }
+ });
+ });
+
+ return server;
+};
+
+LiveStats.prototype.createBayeuxServer = function() {
+ var self = this;
+
+ var bayeux = new faye.NodeAdapter({
+ mount: '/faye',
+ timeout: 45
+ });
+
+ return bayeux;
+};
+
+LiveStats.prototype.ipToPosition = function(ip, callback) {
+ var self = this;
+
+ var request = http.get({
+ host: self.settings.geoipServer.hostname
+ , path: '/geoip/api/locate.json?ip=' + ip
+ }, function(response) {
+ response.setEncoding('utf8');
+
+ var body = '';
+ response.on('data', function(chunk) {
+ body += chunk;
+ });
+
+ response.on('end', function() {
+ var json = JSON.parse(body);
+ if (json.latitude && json.longitude) {
+ callback(json.latitude, json.longitude, json.city);
+ }
+ });
+ });
+};
+
+module.exports = LiveStats;
@@ -0,0 +1,15 @@
+body {
+ background-color: #111;
+ font-family: "Helvetica";
+ overflow: hidden;
+ margin: 0;
+}
+
+p {
+ color: #999;
+}
+
+#map {
+
+}
+
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <link rel="stylesheet" href="/css/application.css" type="text/css" charset="utf-8">
+ </head>
+ <body>
+ <div id="map"></div>
+
+ <script type="text/javascript" src="/js/jquery.js"></script>
+ <script type="text/javascript" src="/faye.js"></script>
+ <script type="text/javascript" src="/js/raphael.js"></script>
+ <script type="text/javascript" src="/js/images.js"></script>
+ <script type="text/javascript" src="/js/jquery.livestats.js"></script>
+ </body>
+</html>

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 824458a

Please sign in to comment.