Skip to content

Commit

Permalink
Adding support for handlers on routes
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Aug 2, 2014
1 parent 8b15cad commit 4cb2300
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 23 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,49 @@ Tensō

Tensō is a REST framework for node.js, designed to simplify the implementation of APIs.

Coming soon!
## Example
Creating an API with Tensō is as simple as three statements.

```javascript
"use strict";

var tenso = require( "tenso" ),
config = require( __dirname + "/config.json" ),
app = tenso( config );
```

## Creating Routes
Routes are loaded as a module, affording a very customizable API server. The following example will return an empty `Array` at `/`, a `String` at `/reports/tps`, & a random number at `/random`.

```javascript
var random = require( "keigai" ).util.number.random,
prepare = require( "./lib/tenso" ).prepare;

module.exports.get = {
"/": [],
"/reports/tps": "TPS Cover Sheet not attached",
"/random": function ( req, res ) {
this.respond( req, res, prepare( random() ) );
}
}
```

## Configuration
This is a sample configuration for Tensō, without authentication or SSL. This would be ideal for development, but not production! Enabling is as easy as providing file paths for the two keys.

```json
{
"hostname": "localhost",
"pageSize": 5,
"port": 8000,
"routes": "routes.js",
"log": {
"dtrace": false,
"syslog": false
},
"ssl": {
"key": null,
"cert": null
}
}
```
8 changes: 4 additions & 4 deletions doc/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ <h4 class="name" id="error"><span class="type-signature"></span>error<span class

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-67">line 67</a>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-72">line 72</a>
</li></ul></dd>


Expand Down Expand Up @@ -441,7 +441,7 @@ <h4 class="name" id="factory"><span class="type-signature"></span>factory<span c

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-77">line 77</a>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-82">line 82</a>
</li></ul></dd>


Expand Down Expand Up @@ -628,7 +628,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-104">line 104</a>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-109">line 109</a>
</li></ul></dd>


Expand Down Expand Up @@ -702,7 +702,7 @@ <h5>Returns:</h5>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Fri Aug 01 2014 23:32:19 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
on Fri Aug 01 2014 23:54:57 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
Expand Down
22 changes: 20 additions & 2 deletions doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,25 @@
<section>
<article><h1>Tensō</h1>
<p>Tensō is a REST framework for node.js, designed to simplify the implementation of APIs.</p>
<p>Coming soon!</p></article>
<h2>Example</h2>
<p>Creating an API with Tensō is as simple as three statements.</p>
<pre><code class="lang-javascript">&quot;use strict&quot;;

var tenso = require( &quot;tenso&quot; ),
config = require( __dirname + &quot;/config.json&quot; ),
app = tenso( config );</code></pre>
<h2>Creating Routes</h2>
<p>Routes are loaded as a module, affording a very customizable API server. The following example will return an empty <code>Array</code> at <code>/</code>, a <code>String</code> at <code>/reports/tps</code>, &amp; a random number at <code>/random</code>.</p>
<pre><code class="lang-javascript">var random = require( &quot;keigai&quot; ).util.number.random,
prepare = require( &quot;tenso&quot; ).prepare;

module.exports.get = {
&quot;/&quot;: [],
&quot;/reports/tps&quot;: &quot;TPS Cover Sheet not attached&quot;,
&quot;/random&quot;: function ( req, res ) {
this.respond( req, res, prepare( random() ) );
}
}</code></pre></article>
</section>


Expand All @@ -123,7 +141,7 @@

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Fri Aug 01 2014 23:32:19 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
on Fri Aug 01 2014 23:54:57 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
Expand Down
2 changes: 1 addition & 1 deletion doc/module-tenso.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ <h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Fri Aug 01 2014 23:32:19 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
on Fri Aug 01 2014 23:54:57 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
Expand Down
2 changes: 1 addition & 1 deletion doc/modules.list.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ <h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Fri Aug 01 2014 23:32:19 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
on Fri Aug 01 2014 23:54:57 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
Expand Down
16 changes: 11 additions & 5 deletions doc/tenso.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,14 @@ <h1 class="page-title">Source: tenso.js</h1>
if ( config.routes instanceof Object ) {
iterate( config.routes, function ( routes, method ) {
iterate( routes, function ( arg, route ) {
obj.server[method]( route, function ( res, req ) {
this.respond( res, req, prepare( arg ) );
} );
if ( typeof arg == "function" ) {
obj.server[method]( route, arg );
}
else {
obj.server[method]( route, function ( req, res ) {
this.respond( req, res, prepare( arg ) );
} );
}
} );
} );
}
Expand Down Expand Up @@ -203,7 +208,8 @@ <h1 class="page-title">Source: tenso.js</h1>
};
}

module.exports = factory;
factory.prepare = prepare;
module.exports = factory;
} )();
</pre>
</article>
Expand All @@ -226,7 +232,7 @@ <h1 class="page-title">Source: tenso.js</h1>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Fri Aug 01 2014 23:32:19 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
on Fri Aug 01 2014 23:54:57 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
Expand Down
14 changes: 10 additions & 4 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ function bootstrap( obj, config ) {
if ( config.routes instanceof Object ) {
iterate( config.routes, function ( routes, method ) {
iterate( routes, function ( arg, route ) {
obj.server[method]( route, function ( res, req ) {
this.respond( res, req, prepare( arg ) );
} );
if ( typeof arg == "function" ) {
obj.server[method]( route, arg );
}
else {
obj.server[method]( route, function ( req, res ) {
this.respond( req, res, prepare( arg ) );
} );
}
} );
} );
}
Expand Down Expand Up @@ -122,5 +127,6 @@ function prepare ( data, error, status ) {
};
}

module.exports = factory;
factory.prepare = prepare;
module.exports = factory;
} )();
8 changes: 7 additions & 1 deletion routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
var random = require( "keigai" ).util.number.random,
prepare = require( "./lib/tenso" ).prepare;

module.exports.get = {
"/": [],
"/reports/tps": "TPS Cover Sheet not attached"
"/reports/tps": "TPS Cover Sheet not attached",
"/random": function ( req, res ) {
this.respond( req, res, prepare( random() ) );
}
}
11 changes: 8 additions & 3 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ function bootstrap( obj, config ) {
if ( config.routes instanceof Object ) {
iterate( config.routes, function ( routes, method ) {
iterate( routes, function ( arg, route ) {
obj.server[method]( route, function ( res, req ) {
this.respond( res, req, prepare( arg ) );
} );
if ( typeof arg == "function" ) {
obj.server[method]( route, arg );
}
else {
obj.server[method]( route, function ( req, res ) {
this.respond( req, res, prepare( arg ) );
} );
}
} );
} );
}
Expand Down
3 changes: 2 additions & 1 deletion src/outro.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module.exports = factory;
factory.prepare = prepare;
module.exports = factory;
} )();

0 comments on commit 4cb2300

Please sign in to comment.