-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f748a3a
Showing
5 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |