Commit 6bac45ef authored by John Crepezzi's avatar John Crepezzi
Browse files

More work on the server

parent b35c89a4
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+2 −0
Original line number Diff line number Diff line
npm-debug.log
node_modules

package.json

0 → 100644
+50 −0
Original line number Diff line number Diff line
{

	"name": "haste",
	"version": "0.0.1",

	"private": true,

	"description": "Private Paste",

	"keywords": [ "paste", "pastebin" ],

	"author": {
		"name": "John Crepezzi",
		"email": "john.crepezzi@gmail.com",
		"url": "http://seejohncode.com/"
	},

	"main": "haste",

	"dependencies": {
		"winston": "*",
    "hashlib": "*"
	},

	"devDependencies": {
    "jasmine-node": "*"
  },

	"bundledDependencies": [],

	"engines": {
		"node": "*"
	},

	"bin": {
		"haste-server": "./server.js"
	},

	"files": [ "server.js", "lib", "static" ],

	"directories": {
		"lib": "./lib"
	},

	"scripts": {
		"start": "node server.js",
    "test": "jasmine-node spec"
	}

}
+59 −27
Original line number Diff line number Diff line
@@ -3,8 +3,14 @@ var fs = require('fs');
var path = require('path');
var url = require('url');

var winston = require('winston');


/////////////
// Configure loggin
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, { colorize: true, level: 'verbose' });

// TODO logging
// TODO preparse static instead of using exists
// TODO split into files
// TODO only parse url once for static files
@@ -21,7 +27,10 @@ StaticHandler.contentTypeFor = function(ext) {
  else if (ext == '.css') return 'text/css';
  else if (ext == '.html') return 'text/html';
  else if (ext == '.ico') return 'image/ico';
  else console.log(ext);
  else {
    winston.error('unable to determine content type for static asset with extension: ' + ext);
    return 'text/plain';
  }
};

StaticHandler.prototype.handle = function(request, response) {
@@ -31,10 +40,9 @@ StaticHandler.prototype.handle = function(request, response) {
    if (exists) {
      fs.readFile(filePath, function(error, content) {
        if (error) {
          // TODO make nice
          console.log(error);
          response.writeHead(500);
          response.end();
          winston.error('unable to read file', { path: filePath, error: error.message });
          response.writeHead(500, { 'content-type': 'application/json' });
          response.end(JSON.stringify({ message: 'IO: Unable to read file' }));
        }
        else {
          response.writeHead(200, { 'content-type': StaticHandler.contentTypeFor(path.extname(filePath)) });
@@ -43,9 +51,9 @@ StaticHandler.prototype.handle = function(request, response) {
      });    
    }
    else {
      // TODO make nice
      response.writeHead(404);
      response.end();
      winston.warn('file not found', { path: filePath });
      response.writeHead(404, { 'content-type': 'application/json' });
      response.end(JSON.stringify({ message: 'file not found' }));
    }
  }); 
};
@@ -58,11 +66,20 @@ var DocumentHandler = function() {

};

DocumentHandler.prototype.handle = function(request, response) {
  if (request.method == 'GET') {
   
DocumentHandler.prototype.handleGet = function(key, response) {
  if (documents[key]) {
    winston.verbose('retrieved document', { key: key });
    response.writeHead(200, { 'content-type': 'application/json' });
    response.end(JSON.stringify({ data: documents[key] }));
  }
  else if (request.method == 'POST') {
  else {
    winston.warn('document not found', { key: key });
    response.writeHead(400, { 'content-type': 'application/json' });
    response.end(JSON.stringify({ message: 'document not found' }));
  }
};

DocumentHandler.prototype.handlePost = function(request, response) {
  var key = '123';
  request.on('data', function(data) {
    if (!documents[key]) {
@@ -71,9 +88,14 @@ DocumentHandler.prototype.handle = function(request, response) {
    documents[key] += data.toString();
  });
  request.on('end', function(end) {
    winston.verbose('added document', { key: key });
    response.end(JSON.stringify({ uuid: key }));
  });
  }
  request.on('error', function(error) {
    // TODO handle error
    // TODO rename key to uuid everywhere behind the scenes
    // TODO finish all TODOs
  });
};

///////////
@@ -81,15 +103,25 @@ DocumentHandler.prototype.handle = function(request, response) {
http.createServer(function(request, response) {

  var incoming = url.parse(request.url, false);

  var handler = null;
  if (incoming.pathname.indexOf('/documents') === 0) {
  var match;

  // Looking to add a new doc
  if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
    handler = new DocumentHandler();
    handler.handlePost(request, response);
  }
  else {
    handler = new StaticHandler('./static');

  // Looking up a doc
  else if ((match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/)) && request.method == 'GET') {
    handler = new DocumentHandler();
    handler.handleGet(match[1], response);
  }

  // Otherwise, look for static file
  else {
    handler = new StaticHandler('./static');
    handler.handle(request, response);
  }

}).listen(7777);
+1 −0
Original line number Diff line number Diff line
@@ -109,6 +109,7 @@ heist.prototype.configureShortcuts = function() {
};


// TODO handle not found gracefully
// TODO refuse to lock empty documents
// TODO support for browsers without pushstate
// TODO support for push state navigation