Skip to content

Commit e61d6de

Browse files
Merge pull request #1 from noelmcloughlin/dev
Merge Dev to Master
2 parents 60fb3de + d131134 commit e61d6de

34 files changed

+1955
-418
lines changed

.DS_Store

6 KB
Binary file not shown.

.env

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
DB_URI="mongodb://<user>:<pass>@localhost/<dbname>"
2-
DB_HOST=localhost
3-
DB_PORT=27017
4-
cookie_password='jfkdjfsklfjkfsjksfjkfsjkfjsjlsfsecret'
5-
cookie_name='lkjflkdjfjlskfkjdkjfklsjfsklsfjklfs'
6-
db=mongodb://localhost/pointsOfInterest
1+
DB="mongodb+srv://noel:[email protected]/pointsOfInterest?retryWrites=true"
2+
3+
OAUTH_PROVIDER=github
4+
OAUTH_CLIENT_ID=eb67f11a7bd5c10dc6b1
5+
OAUTH_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
6+
COOKIE_NAME='nodejs-hapi-nunjunks-mongodb'
7+
BASE_URL='http://localhost'
8+
BASE_URL_PORT=3000

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
2+
#.env
23
node_modules
34
db
45
public/db

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
POINTS OF INTEREST
44

5-
Server-Rendered Web Application, release 1:
5+
Server-Rendered Web Application
66

77
* [x] Front End [ nunjunks, semantic-ui, jquery ]
8-
* [x] Components [boom, dotenv,, mongoose, mais-mongoose-seeder ]
8+
* [x] Components [ axios, boom, dotenv, lodash, mongoose, mongoose-long, mais-mongoose-seeder ]
99
* [x] Backend. [ hapi, joi, vision, inert ]
10-
* [x] Infrastructure. [ node, mongodb ]
10+
* [x] Infrastructure. [ node, mongodb cloud, oAuth ]
11+
* [x] Unit Tests [ chai, mocha ]
1112

1213
# ![](preview_r1.png)
1314

14-
15-
API Web Service, release 2:
16-
17-
* [x] Work in progress

api/pois.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const Poi = require('../mvc/models/poi');
2+
const Boom = require('Boom');
3+
4+
const Pois = {
5+
6+
findAll: {
7+
auth: false,
8+
handler: async function(request, h) {
9+
const pois = await Poi.findAll();
10+
return pois;
11+
}
12+
},
13+
14+
find: {
15+
auth: false,
16+
handler: async function(request, h) {
17+
try {
18+
const poi = await Poi.find({ _poi_id: request.params._id });
19+
if (!poi) {
20+
return Boom.notFound('No point-of-interest with this id');
21+
}
22+
return poi;
23+
} catch (err) {
24+
return Boom.notFound('No point-of-interest with that id');
25+
}
26+
}
27+
},
28+
29+
create: {
30+
auth: false,
31+
handler: async function(request, h) {
32+
const newPoi = new Poi(request.payload);
33+
const poi = await newPoi.save();
34+
if (poi) {
35+
// successful post
36+
return h.response(poi).code(201);
37+
}
38+
// failed post
39+
return Boom.badImplementation('error creating Point of Interest')
40+
}
41+
},
42+
43+
delete: {
44+
auth: false,
45+
handler: async function(request, h) {
46+
const poi = await Poi.deleteOne({ _poi_id: request.params.id });
47+
if (poi) {
48+
// delete success
49+
return { success: true };
50+
}
51+
// delete failed
52+
return Boom.notFound('POI id not found')
53+
}
54+
},
55+
56+
deleteAll: {
57+
auth: false,
58+
handler: async function(request, h) {
59+
const poi = await Poi.deleteMany({});
60+
if (poi) {
61+
// delete success
62+
return { success: true };
63+
}
64+
// delete failed
65+
return Boom.notFound('Pois deleteAll failed')
66+
}
67+
}
68+
};
69+
70+
module.exports = Pois;

api/regions.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const Region = require('../mvc/models/region');
2+
const Boom = require('Boom');
3+
4+
const Regions = {
5+
6+
findAll: {
7+
auth: false,
8+
handler: async function(request, h) {
9+
const regions = await Region.findAll();
10+
return regions
11+
}
12+
},
13+
14+
find: {
15+
auth: false,
16+
handler: async function(request, h) {
17+
try {
18+
const region = await Region.findOne({ _region_id: request.params.region_id });
19+
if (!region) {
20+
return Boom.notFound('No region with this id');
21+
}
22+
return region;
23+
} catch (err) {
24+
return Boom.notFound('No region with that id');
25+
}
26+
}
27+
},
28+
29+
create: {
30+
auth: false,
31+
handler: async function(request, h) {
32+
const newRegion = new Region(request.payload);
33+
const region = await newRegion.save();
34+
if (region) {
35+
// successful post
36+
return h.response(region).code(201);
37+
}
38+
// failed post
39+
return Boom.badImplementation('error creating Region')
40+
}
41+
},
42+
43+
delete: {
44+
auth: false,
45+
handler: async function(request, h) {
46+
const region = await Region.delete({ _region_id: request.params.id });
47+
if (region) {
48+
// delete success
49+
return { success: true };
50+
}
51+
// delete failed
52+
return Boom.notFound('Region id not found')
53+
}
54+
},
55+
56+
deleteAll: {
57+
auth: false,
58+
handler: async function(request, h) {
59+
const region = await Region.delete({});
60+
if (region) {
61+
// delete success
62+
return { success: true };
63+
}
64+
// delete failed
65+
return Boom.notFound('Region deleteAll failed')
66+
}
67+
}
68+
};
69+
70+
module.exports = Regions;

index.js

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
'use strict';
22

3+
const Bell = require('bell');
4+
const Boom = require('boom');
5+
const Cookie = require('hapi-auth-cookie');
36
const Dotenv = require('dotenv');
4-
const Result = Dotenv.config();
57
const Hapi = require('hapi');
6-
const HapiCookie = require('hapi-auth-cookie');
7-
const Vision = require('vision');
88
const Inert = require('inert');
9+
const Nanoid = require('nanoid');
910
const Nunjucks = require('nunjucks');
1011
const NunjucksHapi = require('nunjucks-hapi');
1112
const Routes = require('./routes');
12-
const server = Hapi.server({ port: process.env.PORT || 3000, });
13-
let Path = require('path');
14-
15-
require('./mvc/models/db');
13+
const RoutesApi = require('./routesApi');
14+
const Vision = require('vision');
1615

17-
const Checklist = [Result, Hapi, HapiCookie, Vision, Inert, Nunjucks, NunjucksHapi, Routes, server, Path]
16+
// Sanity check modules
17+
const Checklist = [Bell, Boom, Cookie, Hapi, Vision, Inert, Nanoid, Nunjucks, NunjucksHapi, Routes, RoutesApi];
1818
for (const o of Checklist) {
1919
if (o.error) {
2020
console.log(o.error.message);
2121
process.exit(1);
2222
}
2323
}
2424

25+
// Setup environment
26+
Dotenv.config();
27+
const server = Hapi.server({ port: process.env.BASE_URL_PORT || 3000, });
28+
require('./mvc/models/db');
29+
30+
// Setup Rendering engine
2531
Nunjucks.installJinjaCompat();
2632
Nunjucks.configure('views', {
2733
autoescape: true,
@@ -30,9 +36,10 @@ Nunjucks.configure('views', {
3036
});
3137

3238
async function provision() {
39+
await server.register(Bell);
40+
await server.register(Cookie);
3341
await server.register(Inert);
3442
await server.register(Vision);
35-
await server.register(HapiCookie);
3643

3744
server.views({
3845
engines: {
@@ -43,25 +50,31 @@ async function provision() {
4350
layoutPath: './mvc/views/layouts',
4451
partialsPath: './mvc/views/partials',
4552
isCached: false,
46-
layout: false // warning; true renders (unwanted) raw html! Need false.
53+
layout: false // warning; true renders (unwanted) raw html - nunjunks needs false.
4754
});
4855

49-
server.auth.strategy('standard', 'cookie', {
50-
password: process.env.cookie_password,
51-
cookie: process.env.cookie_name,
52-
isSecure: false,
53-
ttl: 24 * 60 * 60 * 1000,
54-
redirectTo: '/'
55-
});
56+
let authCookieOptions = {
57+
password: Nanoid() + Nanoid(), // String to encrypt auth cookie during authorization(min 32 chars)
58+
isSecure: false // 'true' in production (requires HTTPS)
59+
};
60+
61+
server.auth.strategy('cookie-auth', 'cookie', authCookieOptions);
62+
63+
let bellAuthOptions = {
64+
provider: process.env.OAUTH_PROVIDER,
65+
password: Nanoid() + Nanoid(), // String to encrypt temp cookie during authorization
66+
clientId: process.env.OAUTH_CLIENT_ID, // *** Replace with your app Client Id ****
67+
clientSecret: process.env.OAUTH_CLIENT_SECRET, // *** Replace with your app Client Secret ***
68+
isSecure: false // 'true' in production (requires HTTPS)
69+
};
5670

57-
server.auth.default({
58-
mode: 'required',
59-
strategy: 'standard'
60-
});
71+
server.auth.strategy('github-oauth', 'bell', bellAuthOptions);
72+
server.auth.default('cookie-auth');
6173

6274
server.route(Routes);
75+
server.route(RoutesApi);
6376
await server.start();
64-
console.log(`Server running at: ${server.info.uri}`);
77+
console.log(`server running at: ${server.info.uri}`);
6578
}
6679

6780
process.on('unhandledRejection', err => {

mvc/.DS_Store

6 KB
Binary file not shown.

mvc/controllers/about.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)