From f748a3a9b1361e72550a3f6670d713d532e3256c Mon Sep 17 00:00:00 2001 From: Caligone Date: Sun, 2 Aug 2015 11:59:18 +0200 Subject: [PATCH] First commit --- README.md | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 1 + lib/Brick.js | 42 +++++++++++++++++ lib/index.js | 14 ++++++ package.json | 25 +++++++++++ 5 files changed, 207 insertions(+) create mode 100644 README.md create mode 100644 index.js create mode 100644 lib/Brick.js create mode 100644 lib/index.js create mode 100644 package.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..5a45c28 --- /dev/null +++ b/README.md @@ -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 \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..4cc88b3 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib'); \ No newline at end of file diff --git a/lib/Brick.js b/lib/Brick.js new file mode 100644 index 0000000..1541928 --- /dev/null +++ b/lib/Brick.js @@ -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; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..0e72209 --- /dev/null +++ b/lib/index.js @@ -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 +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..2d49a32 --- /dev/null +++ b/package.json @@ -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" +}