Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Caligone committed Aug 2, 2015
0 parents commit f748a3a
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
125 changes: 125 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Hapi-Brick

***Hapi-Brick*** allows you to functionally split your Hapi server into Bricks.

A Brick is a Hapi plugin that includes a full MVC structure and increases the maintainability and the reusability of your Hapi development.


## Getting started

**1. Install *hapi-brick***

```
npm install --save hapi-brick
```

**2. Load the Brick Engine**
```js
server.register({register: require('hapi-brick')}, function (err) {
// Handle errors
});
```

**3. Create your Brick**

Here is the required structure :
```
├── UserBrick
│   ├── index.js # Entry point of your Brick
│   ├── models # Folder of your models (soon)
│   │   └── User.model.js # A model of your Brick (soon)
│   └── routes # Folder of your routes
│      └── User.get.route.js # A route of your Brick
└── index.js # The Hapi server
```

The `index.js` describes the Brick and builds it from the Brick Engine :
```js
'use strict';

module.exports.register = function (server, options, next) {
// Retrieve the Brick Engine from the Hapi server object
var Brick = server.plugins.Brick.Engine;
// Create the current Brick
var UserBrick = new Brick(__dirname, options);
// Register the current Brick
UserBrick.register(server, options, next);
};

module.exports.register.attributes = {
// Name of the Brick
name: 'UserBrick',
// Version of the Brick
version: '1.0.0',
// Dependencies of the Brick (you can add any Hapi plugin here)
dependency: ['hapi-brick']
};
```

A simple `User.get.route.js` route file :
```js
'use strict';

module.exports = [
{
method: 'GET',
path: '/users',
handler: function (request, reply) {
var users = [{
id_user: 1,
fistname: "John",
lastname: "Doe"
}];
return reply(users);
}
}
];
```

**4. Load your Brick**

Finally load your Brick from its folder
```js
server.register({register: require('./UserBrick')}, function (err) {
// Handle errors
});
```

## More complex Brick structure

```
├── UserBrick
│   ├── index.js
│   ├── auth
│   │   └── JWT.auth.js
│   ├── models
│   │   └── User.model.js
│   ├── routes
│   │   ├── User.get.route.js
│   │   └── User.post.route.js
│   │   └── User.put.route.js
│   │   └── User.delete.route.js
│   ├── test
│   │   ├── User.create.spec.js
│   │   └── User.login.spec.js
│   │   └── User.update.spec.js
│   │   └── User.get.spec.js
│   │   └── User.delete.spec.js
│   └── views
│   └── validation.email.html
```

## How does it works

The Brick Engine simply load differents kind of file from their extension. Here are the available extensions :
* **.route.js* : A route file
* **.model.js* : A model file (soon)
* **.spec.js* : A test file (soon)

## Next things to do

* **Add the database handler** (*Mongoose* or *Sequelize* or *Custom*)
* **Add the tests handler**
* Add some documentation
* Create a demo project
* Create a Yeoman generator
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib');
42 changes: 42 additions & 0 deletions lib/Brick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

var async = require('async'),
glob = require('glob'),
_ = require('underscore');

function Brick (dirname, opts) {
// Check the use of new
if (!(this instanceof Brick)) {
throw new Error('You have to use ’new’ to create an instance of Brick');
}

// Check the dirname
if(!(dirname)) {
throw new Error('dirname have to be defined');
}

this.register = function (server, options, next) {
async.auto({
// Load the routes
routes: function (endLoadRoutes) {
// Get the route files
glob(dirname + "/**/*.route.js", options, function (err, files) {
var routes = [];
_.each(files, function (f) {
routes = routes.concat(f);
});
server.route(routes);
endLoadRoutes(null);
});
}
}, function (err, results) {
next();
});
};

this.register.attributes = opts.attributes;

return this;
}

module.exports = Brick;
14 changes: 14 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var Brick = require('./Brick'),
version = require('../package.json').version;

module.exports.register = function (server, options, next) {
server.expose('Engine', Brick);
return next();
};

module.exports.register.attributes = {
name: 'Brick',
version: version
};
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "hapi-brick",
"version": "0.1.0",
"description": "A MVC overlay plugin for Hapi",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@github.com:Caligone/hapi-brick.git"
},
"keywords": [
"hapi",
"brick",
"plugin",
"mvc"
],
"author": "Caligone",
"license": "MIT",
"bugs": {
"url": "https://github.com/Caligone/hapi-brick/issues"
},
"homepage": "https://github.com/Caligone/hapi-brick"
}

0 comments on commit f748a3a

Please sign in to comment.