Skip to content

Commit a7cfa7e

Browse files
committedJul 7, 2016
update hapijs example with support for postgres
1 parent 14e2e6b commit a7cfa7e

File tree

9 files changed

+136
-3
lines changed

9 files changed

+136
-3
lines changed
 

‎hapijs/LICENSE

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2016 Saravjeet 'Aman' Singh
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎hapijs/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
hapijs - sample app for benchmarking
2+
-----------------------------------
3+
4+
## Setup
5+
6+
run
7+
```
8+
npm install
9+
```
10+
11+
and you're set!
12+
13+
## Running
14+
15+
```
16+
node index.js
17+
```
18+
19+
## Note:
20+
update the postgres server uri in config/manifest.js
21+
22+
## Known Issues
23+
If the postgres uri is not correct, the app will return 500 for any request.
24+
I'm unsure at this moment why the server doesn't report the error. Will look
25+
into it in the future.

‎hapijs/app/controllers/core.js

+26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
11
'use strict';
22

3+
const Boom = require('boom');
4+
35
exports.index = {
46
description: 'index page',
57
handler: (request, reply) => {
68
reply('Hello, world!');
79
}
810
};
11+
12+
exports.fortunesJSON = {
13+
description: 'returns a JSON representation of the fortunes',
14+
handler: (request, reply) => {
15+
request.pg.client.query('select * from fortunes', (err, result) => {
16+
if ( err ) {
17+
reply(Boom.wrap(err, 500));
18+
}
19+
reply(result.rows);
20+
});
21+
}
22+
};
23+
24+
exports.fortunes = {
25+
description: 'generates fortunes page',
26+
handler: (request, reply) => {
27+
request.pg.client.query('select * from fortunes', (err, result) => {
28+
if ( err ) {
29+
reply(Boom.wrap(err, 500));
30+
}
31+
reply.view('fortunes', { fortunes: result.rows });
32+
});
33+
}
34+
};

‎hapijs/app/routes/core.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@ exports.register = (server, options, next) => {
66
core: require('../controllers/core.js')
77
};
88

9-
server.route([{
9+
server.route([
10+
{
1011
method: 'GET',
1112
path: '/',
1213
config: Controllers.core.index
13-
}]);
14+
},
15+
{
16+
method: 'GET',
17+
path: '/fortunes',
18+
config: Controllers.core.fortunes
19+
},
20+
{
21+
method: 'GET',
22+
path: '/fortunes.json',
23+
config: Controllers.core.fortunesJSON
24+
}
25+
]);
1426

1527
next();
1628
};

‎hapijs/app/templates/fortunes.hbs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<html>
2+
<head>
3+
<title>Fortune Cookie Messages</title>
4+
</head>
5+
<body>
6+
<table>
7+
{{#each fortunes}}
8+
<tr>
9+
<td>{{this.id}}</td>
10+
<td>{{this.message}}</td>
11+
</tr>
12+
{{/each}}
13+
</table>
14+
</body>
15+
</html>

‎hapijs/config/manifest.js

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ module.exports = {
66
port: 8000
77
}],
88
registrations: [{
9+
plugin: 'vision'
10+
}, {
11+
plugin:{
12+
register: 'visionary',
13+
options: {
14+
engines: {
15+
hbs: 'handlebars',
16+
},
17+
path: './app/templates'
18+
}
19+
}
20+
}, {
21+
plugin: {
22+
register: './lib/postgres.js',
23+
options: {
24+
connectionString: ''
25+
}
26+
}
27+
}, {
928
plugin: './app/routes/core.js'
1029
}]
1130
};

‎hapijs/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const Options = {
88

99
Glue.compose(Manifest, Options, (err, server) => {
1010
if ( err ) throw err;
11+
1112
server.start((err) => {
1213
if ( err ) throw err;
1314
console.info('server running at ' + server.info.uri);

‎hapijs/lib/postgres.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
exports.register = (server, options, next) => {
4+
5+
let plugin = {
6+
register: require('hapi-node-postgres'),
7+
options: options
8+
}
9+
10+
server.register(plugin, (err) => {
11+
if ( err ) {
12+
console.log('error loading "hapi-node-postgres" plugin');
13+
}
14+
});
15+
next();
16+
};
17+
18+
exports.register.attributes = {
19+
name: 'postgres adapter',
20+
version: require('../package.json').version
21+
};

‎hapijs/package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
"author": "Saravjeet 'Aman' Singh",
1010
"license": "MIT",
1111
"dependencies": {
12-
"hapi": "^13.5.0"
12+
"glue": "^3.3.0",
13+
"handlebars": "^4.0.5",
14+
"hapi": "^13.5.0",
15+
"hapi-node-postgres": "^3.0.2",
16+
"pg": "^4.5.6",
17+
"vision": "^4.1.0",
18+
"visionary": "^6.0.0"
1319
}
1420
}

0 commit comments

Comments
 (0)
Please sign in to comment.