Skip to content

Commit 8daed9e

Browse files
committed
first commit
0 parents  commit 8daed9e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+26357
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
NODE_ENV=development
2+
MONGODB_URL=mongodb://admin:foo12345@ds139334.mlab.com:39334/your_db_name

.eslintignore

Whitespace-only changes.

.eslintrc.json

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"parserOptions": {
3+
"ecmaVersion": 8
4+
},
5+
"parser": "babel-eslint",
6+
"plugins": [
7+
"jest"
8+
],
9+
"env": {
10+
"node": true,
11+
"es6": true,
12+
"mocha": true,
13+
"phantomjs": true,
14+
"jest/globals": true
15+
},
16+
"globals": {
17+
"expect": true
18+
},
19+
"extends": [
20+
"plugin:jest/recommended"
21+
],
22+
"rules": {
23+
"no-reserved-keys": 0,
24+
"quote-props": 0,
25+
"no-extra-boolean-cast": 0,
26+
"guard-for-in": 0,
27+
"valid-jsdoc": 0,
28+
"no-console": 0,
29+
"no-debugger": 1,
30+
"no-constant-condition": 1,
31+
"no-empty": 1,
32+
"no-sparse-arrays": 1,
33+
"no-multi-str": 1,
34+
"comma-dangle": 2,
35+
"no-cond-assign": 2,
36+
"no-dupe-args": 2,
37+
"no-dupe-keys": 2,
38+
"no-duplicate-case": 2,
39+
"no-ex-assign": 2,
40+
"no-extra-parens": 2,
41+
"no-extra-semi": 2,
42+
"no-inner-declarations": 2,
43+
"no-invalid-regexp": 2,
44+
"no-irregular-whitespace": 2,
45+
"no-negated-in-lhs": 2,
46+
"no-obj-calls": 2,
47+
"no-regex-spaces": 2,
48+
"no-unreachable": 2,
49+
"use-isnan": 2,
50+
"valid-typeof": 2,
51+
"no-unexpected-multiline": 2,
52+
"block-scoped-var": 2,
53+
"eqeqeq": 2,
54+
"no-eq-null": 2,
55+
"no-eval": 2,
56+
"no-extra-bind": 2,
57+
"no-fallthrough": 2,
58+
"no-floating-decimal": 2,
59+
"no-labels": 2,
60+
"no-lone-blocks": 2,
61+
"no-loop-func": 2,
62+
"no-native-reassign": 2,
63+
"no-new-func": 2,
64+
"no-new-wrappers": 2,
65+
"no-new": 2,
66+
"no-redeclare": 2,
67+
"no-return-assign": 2,
68+
"no-script-url": 2,
69+
"no-self-compare": 2,
70+
"no-sequences": 2,
71+
"no-throw-literal": 2,
72+
"no-unused-expressions": 2,
73+
"no-with": 2,
74+
"radix": 2,
75+
"vars-on-top": 2,
76+
"no-label-var": 2,
77+
"no-shadow-restricted-names": 2,
78+
"no-undef": 2,
79+
"no-undef-init": 2,
80+
"no-undefined": 2,
81+
"no-unused-vars": 2,
82+
"no-use-before-define": 2,
83+
"comma-spacing": 2,
84+
"comma-style": 2,
85+
"indent": [
86+
"error",
87+
2,
88+
{
89+
"SwitchCase": 1
90+
}
91+
],
92+
"lines-around-comment": 2,
93+
"new-cap": 0,
94+
"new-parens": 1,
95+
"newline-after-var": 2,
96+
"no-array-constructor": 2,
97+
"no-inline-comments": 1,
98+
"no-lonely-if": 2,
99+
"no-mixed-spaces-and-tabs": 2,
100+
"no-multiple-empty-lines": 2,
101+
"no-new-object": 2,
102+
"no-spaced-func": 2,
103+
"no-trailing-spaces": 2,
104+
"operator-assignment": 2,
105+
"operator-linebreak": 2,
106+
"quotes": [
107+
2, "single"
108+
],
109+
"semi-spacing": 2,
110+
"semi": 2,
111+
"keyword-spacing": 0,
112+
"space-before-blocks": 2,
113+
"spaced-comment": 2,
114+
"wrap-regex": 2,
115+
"no-var": 0,
116+
"prefer-const": 0
117+
}
118+
}

.gitattributes

Whitespace-only changes.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.DS_STORE

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<p align="center">
2+
<h1 align="center">NodeJS - HapiJS API Worker - Monolith</h1>
3+
</p>
4+
5+
## Developing at NodeJS - HapiJS API Worker - Monolith
6+
7+
> NodeJS - HapiJS API Worker - Monolith built for study proposes, it provides **RESTFUL** services on resource oriented architecture projects (ROA).
8+
9+
> The barebone is located under `app/api` and `app/core`, it is desgined by default covered by an Users API which is the authentecion is supported by a JWT plugin (app/core/plugins). There is also a Workers layer which comes with the `webapi` as default but it also ables you to create your own workers such Notification (notify) or Transactions (shake data in db) workers. If you need more details of how apply in your business feel free to get in touch and I'll be happy to help.
10+
11+
## Configuring development environment
12+
13+
#### Step 1: installing tools
14+
15+
* install git: [git-scm](http://git-scm.com/)
16+
* install NodeJS@12+: [nodejs.org](http://nodejs.org)
17+
18+
#### Step 2: preparing repository
19+
20+
Clone repository:
21+
```
22+
git clone https:/github.com/felipekm/nodejs-hapijs-monolith
23+
```
24+
25+
Install project packages
26+
```
27+
cd nodejs-hapijs-monolith
28+
npm install
29+
```
30+
### Step 3: run tests to check if things are fine, the tests file must be created in app/core/tests:
31+
32+
To run your tests:
33+
```
34+
npm test
35+
```
36+
37+
## How to debug it
38+
39+
To debug we are explicity using the [Visual Code](https://code.visualstudio.com/):
40+
41+
* Open your the project in VS Code;
42+
* Press `F5` or symply runs `node server.js` in this project folder;
43+
44+
### Development environment variables
45+
46+
For development, you must set this variables and values in your OS:
47+
* __NODE_ENV__: development
48+
* __ENGINE__: The engine to start the process, the defined value matches the folder name at `app/core/workers/NAME`, the defaut is `webapi, you can have several others such a Notificator or Transactions worker that keeps running every time.
49+
50+
### NPM tasks
51+
52+
Several tasks are scripted to make things easier to dev team. In this project, you will find:
53+
* ```npm start``` : this task is only used on AWS EC2 machine.
54+
* ```npm test``` : runs all automated tests and coding best practices check.
55+
* ```npm stop``` : kill the process.
56+
* ```npm dev``` : debug (dev) mode using nodemon.
57+
58+
### Support
59+
60+
If you find any problem or question please raise an issue
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// App dependenciess
2+
const pkg = require('../../../../package.json');
3+
4+
exports.config = {
5+
auth: false,
6+
tags: [ 'main', 'debug' ],
7+
notes: 'Provides details about environment. Needs debug flag set as true in conf file.',
8+
description: 'Debug route'
9+
};
10+
11+
/**
12+
* @function {handle} - Manage requests
13+
* @param {object} request { The request object }
14+
* @param {function} reply { The callback function }
15+
* @return {object} { Retorno da função invocada }
16+
*/
17+
exports.handle = ({ reply }) => {
18+
const data = {
19+
name: global.nodename,
20+
kernel: global.config.kernel.process,
21+
version: pkg.version,
22+
loaded_apis: global.apis,
23+
loaded_routes: global.routes,
24+
loaded_plugins: global.plugins
25+
};
26+
27+
return reply(data);
28+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const pkg = require('../../../../package.json');
2+
3+
exports.config = {
4+
auth: false,
5+
tags: [ 'main', 'ping' ],
6+
notes: 'Ping route to check if server is up or not.',
7+
description: 'Ping route!'
8+
};
9+
10+
/**
11+
* @function {handle} - Função de Ping que retorna informações do web sever
12+
* @param {object} request { The request object }
13+
* @param {function} reply { The callback function }
14+
* @return {object} { Retorno da função invocada }
15+
*/
16+
exports.handle = () => {
17+
const welcomeData = {
18+
name: pkg.description,
19+
version: pkg.version,
20+
environment: process.env.NODE_ENV
21+
};
22+
23+
return welcomeData;
24+
};

app/api/main/main-routes.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const rootHandler = require('./controllers/root-controller');
2+
const debugHandler = require('./controllers/debug-controller');
3+
4+
module.exports = [
5+
{
6+
path: '/',
7+
method: 'GET',
8+
config: rootHandler.config,
9+
handler: rootHandler.handle
10+
},
11+
12+
{
13+
path: '/debug',
14+
method: 'GET',
15+
config: debugHandler.config,
16+
handler: debugHandler.handle
17+
}
18+
];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const Joi = require('joi');
4+
5+
const generalTemplate = {
6+
tags: [ 'v1', 'authentication' ],
7+
description: 'Authentication routes',
8+
auth: 'jwt',
9+
notes: 'API to perform login/logout.'
10+
};
11+
12+
exports.login = {
13+
...generalTemplate,
14+
auth: false,
15+
validate: {
16+
payload: {
17+
email: Joi.string().email().required(),
18+
password: Joi.string().required()
19+
}
20+
}
21+
};
22+
23+
exports.logout = {
24+
...generalTemplate
25+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const usersHelper = require('../../../core/helpers/users-helper');
4+
const authenticationHelper = require('../../../core/helpers/authentication-helper');
5+
6+
exports.login = async(request, h) => {
7+
try {
8+
9+
const userData = await authenticationHelper.authenticate(request.payload.email, request.payload.password);
10+
const token = await authenticationHelper.generateToken(userData, request);
11+
12+
await usersHelper.registerLastLogin(userData._id);
13+
14+
return h.response(token);
15+
16+
} catch(err) {
17+
console.error(err);
18+
return h.response(err);
19+
}
20+
};
21+
22+
exports.logout = async(request, h) => {
23+
try {
24+
25+
const userToken = request.auth.token;
26+
const logoutResponse = await authenticationHelper.logout(userToken);
27+
28+
return h.response(logoutResponse);
29+
30+
} catch(err) {
31+
console.error(err);
32+
return h.response(err);
33+
}
34+
};
35+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const config = require('./authentication-config');
4+
const controller = require('./authentication-controller');
5+
6+
module.exports = [
7+
8+
{
9+
path: '/api/v1/login',
10+
method: 'POST',
11+
handler: controller.login,
12+
options: config.login
13+
},
14+
15+
{
16+
path: '/api/v1/logout',
17+
method: 'GET',
18+
handler: controller.logout,
19+
options: config.logout
20+
}
21+
22+
];

0 commit comments

Comments
 (0)