Skip to content

Commit

Permalink
minor fixes. new examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
cainus committed Aug 11, 2012
1 parent 74e67be commit 2c9de0f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
9 changes: 9 additions & 0 deletions nextsimplest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var Percolator = require('./percolator');

var server = new Percolator();
server.routeDirectory(__dirname + '/resources', function(err){
if (!!err) {console.log(err);}
server.listen(function(err){
console.log('server is listening on port ', server.port);
});
});
35 changes: 18 additions & 17 deletions percolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,32 @@ var JsonResponder = require('./StatusManager').JsonResponder;
var _ = require('underscore');

Percolator = function(options){
options = options || {};
this.statusman = new StatusManager();
this.options = options;
this.options = _.extend({port : 3000,
protocol : 'http',
resourcePath : '/'
}, options);
this.mediaTypes = new Reaper();
this.port = options.port || 80;
this.protocol = options.protocol || 'http';
var protocol = this.protocol;
this.resourceDir = options.resourceDir || './resources';
this.resourcePath = options.resourcePath || '/api';
this.staticDir = options.staticDir || './static';
this.options = _.extend(options,
{port : this.port,
protocol : this.protocol,
resourcePath : this.resourcePath,
staticDir : this.staticDir,
resourceDir : this.resourceDir});
this.port = this.options.port;
this.protocol = this.options.protocol;
this.resourcePath = this.options.resourcePath;
this.router = new Router(this.resourcePath);
var router = this.router;
var protocol = this.protocol;
this.router.onRequest = function(handler, req, res, cb){
handler.uri = new UriUtil(router, req.url, protocol, req.headers.host);
cb(null, handler);
};
this.assignErrorHandlers();
this.registerMediaTypes();
var that = this;
this.server = express.createServer();
this.expressServer = express.createServer();
if (!!options.staticDir){
this.staticDir = options.staticDir;
this.expressServer.use(express['static'](this.staticDir));
}
this.expressServer.use(express.bodyParser()); // TODO does this work for PUT?!?!
this.router.on("route", function(resource){
that.decorateResource(resource);
});
Expand Down Expand Up @@ -169,12 +170,12 @@ Percolator.prototype.registerMediaTypes = function(){
};

Percolator.prototype.use = function(middleware){
this.server.use(middleware);
this.expressServer.use(middleware);
};

Percolator.prototype.listen = function(port, cb){
Percolator.prototype.listen = function(cb){
this.use(this.router.connectMiddleware);
this.server.listen(port, cb);
this.expressServer.listen(this.port, cb);
};

module.exports = Percolator;
5 changes: 5 additions & 0 deletions resources/_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

exports.handler = {
GET : function(req, res){ res.end("Hello World!"); }

}
9 changes: 9 additions & 0 deletions simplest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var Percolator = require('./percolator');

var server = new Percolator();
server.router.route('/', { GET : function(req, res){
res.end("Hello World!");
}});
server.listen(function(err){
console.log('server is listening on port ', server.port);
});
27 changes: 18 additions & 9 deletions test_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,39 @@ var express = require('express');

var app = {
protocol : 'http',
resourceDir : __dirname + '/test/test_fixtures/resources',
resourcePath : '/api',
staticDir : __dirname + '/static',
port : 8080
};
var server = new Percolator(app);

server.use(express.favicon());
server.use(express['static'](app.staticDir));
server.use(express.bodyParser());
server.use(express.bodyParser()); // TODO does this work for PUT?!?!
server.use(function(req, res, next){
console.log(req.method, ' ', req.url);
next();
});

server.routeDirectory(app.resourceDir, function(err){
console.log("routing resources in " + app.resourceDir);
server.expressServer.get('/asdfasdf', function(req, res){
res.send("booyakabooyaka");
});


var resourceDir = __dirname + '/test/test_fixtures/resources';
server.routeDirectory(resourceDir, function(err){
console.log("routed resources in " + resourceDir);

server.router.route('/inside',
{ GET : function(req, res){
res.send("muahahah!");
}
}).as('inside');

if (err) {
console.log("Routing error");
console.log(err);
return;
}
server.listen(app.port, function(err){
server.listen(function(err){
if (err) {console.log(err);throw err;}
console.log('Percolator running on ' + app.port);
console.log('Percolator running on ' + server.port);
});
});

0 comments on commit 2c9de0f

Please sign in to comment.