Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add barista #184

Merged
merged 4 commits into from Sep 5, 2012
View
@@ -30,7 +30,7 @@ var fs = require('fs')
, helpers = require('./template/helpers')
, actionHelpers = require('./template/helpers/action')
, FunctionRouter = require('./routers/function_router').FunctionRouter
- , RegExpRouter = require('./routers/regexp_router').RegExpRouter
+ , RegExpRouter = require('barista').Router
, BaseController = require('./base_controller').BaseController
, sessions = require('./sessions')
, CookieCollection = require('./cookies').CookieCollection
@@ -621,7 +621,7 @@ var App = function () {
}
}
}
- // Either static or 404
+ // Either static, 404, or 405
else {
// Get the path to the file
staticPath = geddy.config.staticFilePath + reqUrl;
@@ -630,15 +630,39 @@ var App = function () {
// Decode path (e.g. %20)
staticPath = decodeURIComponent(staticPath);
- if (utils.file.existsSync(staticPath)) {
+ if ( utils.file.existsSync(staticPath) && fs.statSync(staticPath).isFile() ) {
staticResp = new response.Response(resp);
staticResp.sendFile(staticPath);
}
else {
- err = new errors.NotFoundError(reqUrl + ' not found.');
- errResp = new response.Response(resp);
- errResp.send(err.message, err.statusCode,
- {'Content-Type': 'text/html'});
+
+ // 405?
+ var non_method_routes = router.all(reqUrl);
+ if ( non_method_routes.length > 0 ){
+
+ // build a unique list of acceptable methods for this resource
+ var acceptable_methods = {};
+ non_method_routes.map(function(params){
+ acceptable_methods[params.method] = true;
+ });
+ acceptable_methods = Object.keys(acceptable_methods);
+
+ // send a friendly error response
+ err = new errors.MethodNotAllowedError(
+ method + ' method not allowed. Please consider ' +
+ acceptable_methods.join(', ').replace(/,\s(\w+)$/," or $1") +
+ ' instead.');
+ errResp = new response.Response(resp);
+ errResp.send( err.message, err.statusCode, {'Content-Type': 'text/html'} );
+
+ // 404
+ }else{
+ err = new errors.NotFoundError(reqUrl + ' not found.');
+ errResp = new response.Response(resp);
+ errResp.send(err.message, err.statusCode,
+ {'Content-Type': 'text/html'});
+ }
+
}
}
});
View
@@ -11,7 +11,8 @@
"author": "Matthew Eernisse <mde@fleegix.org> (http://fleegix.org)",
"dependencies": {
"jake": "0.3.x",
- "utilities": "0.0.x"
+ "utilities": "0.0.x",
+ "barista": "0.0.x"
},
"bin": {
"geddy": "./bin/cli.js"
View
@@ -17,20 +17,24 @@
*/
-var router = new geddy.RegExpRouter();
-router.match('/').to({controller: 'Main', action: 'index'});
+var router = exports.router = new geddy.RegExpRouter();
+
+router.get('/').to('Main.index');
// Basic routes
-// router.match('/moving/pictures/:id').to(
-// {controller: 'Moving', action: 'pictures'});
-// router.match('/farewells/:farewelltype/kings/:kingid').to(
-// {controller: 'Farewells', action: 'kings'});
+// router.match('/moving/pictures/:id', 'GET').to('Moving.pictures');
+//
+// router.match('/farewells/:farewelltype/kings/:kingid', 'GET').to('Farewells.kings');
+//
// Can also match specific HTTP methods only
-// router.match('/xandadu', 'get').to(
-// {controller: 'Xandadu', action: 'specialHandler'});
+// router.get('/xandadu').to('Xanadu.specialHandler');
+// router.del('/xandadu/:id').to('Xanadu.killItWithFire');
//
// Resource-based routes
// router.resource('hemispheres');
-
-exports.router = router;
-
+//
+// Nested Resource-based routes
+// router.resource('hemispheres', function(){
+// this.resource('countries');
+// this.get('/print(.:format)').to('Hemispheres.print');
+// });