Skip to content

Commit

Permalink
Merge pull request #130 from firstandthird/hapi17
Browse files Browse the repository at this point in the history
Hapi17
  • Loading branch information
orthagonal authored Dec 7, 2017
2 parents 4d522ca + 78a2be6 commit 1830e90
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 227 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
language: node_js
node_js:
- "8"
- "6"
5 changes: 2 additions & 3 deletions conf/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ verbose: false
logType: '{{ENV.LOG_TYPE || "flat" }}'
server:
debug: false
connection:
routes:
state:
failAction: 'ignore'
port: "{{ENV.PORT || 8080}}"
address: '0.0.0.0'
routePrefix: "{{ ENV.PATH_PREFIX || '' }}"
plugins:
hapi-require-https:
"@firstandthird/hapi-require-https":
_enabled: '{{ENV.FORCE_HTTPS === "true"}}'
hapi-trailing-slash:
method: 'remove'
Expand All @@ -27,7 +26,7 @@ plugins:
hapi-logr:
_priority: -5
clientErrorsToWarnings: true
requests: '{{!!ENV.ACCESS_LOGS || false}}'
requests: '{{ENV.ACCESS_LOGS === "true" || ENV.ACCESS_LOGS === true }}'
reporters:
flat:
reporter: 'logr-flat'
Expand Down
37 changes: 13 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,31 @@ class Rapptor {
this.options.configUrl = process.env.RAPPTOR_CONFIG_URL;
}

// callback should be an async function
before(callback) {
this.options.before = callback;
}

start(done) {
hapiConfi(Hapi, this.options, (err, server, config) => {
if (err) {
return done(err);
}
this.config = config;
this.server = server;
server.start((serverErr) => {
if (!serverErr) {
const uri = process.env.VIRTUAL_HOST || server.info.uri;
server.log(['server', 'notice'], `Server started: ${uri}`);
}
done(serverErr, server, config);
});
});

async start() {
const { server, config } = await hapiConfi(Hapi, this.options);
this.config = config;
this.server = server;
const uri = process.env.VIRTUAL_HOST || server.info.uri;
process.on('SIGTERM', () => {
this.stop(() => { process.exit(0); });
});
await server.start();
server.log(['server', 'notice'], `Server started: ${uri}`);
return { server, config };
}

stop(done) {
done = done || function() {};

this.server.stop({ timeout: 5 * 1000 }, () => {
done();
});
async stop() {
await this.server.stop({ timeout: 5 * 1000 });
}
}

Rapptor.prototype.stop = function(callback) {
this.server.stop(callback);
Rapptor.prototype.stop = async function() {
await this.server.stop();
};

module.exports = Rapptor;
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Opinionated web framework built on top of hapi",
"homepage": "https://github.com/firstandthird/rapptor",
"author": "First+Third",
"version": "3.6.0",
"version": "4.0.0",
"keywords": [
"hapi",
"framework",
Expand All @@ -22,27 +22,27 @@
"rapptor": "bin/rapptor.js"
},
"dependencies": {
"hapi": "^16.6.2",
"hapi-confi": "^5.2.1",
"hapi-log-response": "^1.7.0",
"hapi-logr": "^3.7.0",
"hapi-method-loader": "^1.3.0",
"hapi-require-https": "^2.1.0",
"hapi-route-loader": "^3.0.0",
"hapi-trailing-slash": "^2.1.1",
"@firstandthird/hapi-require-https": "^2.1.0",
"hapi": "^17.1.1",
"hapi-confi": "^6.0.1",
"hapi-log-response": "^2.0.1",
"hapi-logr": "^4.0.0",
"hapi-method-loader": "^2.0.0",
"hapi-route-loader": "^4.0.0",
"hapi-trailing-slash": "^3.0.0",
"logr-console-color": "^1.3.1",
"logr-flat": "^3.0.0",
"logr-reporter-bell": "0.0.1"
},
"devDependencies": {
"boom": "5.2.0",
"code": "^4.0.0",
"code": "^5.1.2",
"eslint": "^4.9.0",
"eslint-config-firstandthird": "^4.0.1",
"eslint-plugin-import": "^2.1.0",
"handlebars": "^4.0.6",
"inert": "^4.1.0",
"lab": "^14.2.2",
"vision": "^4.1.1"
"inert": "^5.0.1",
"lab": "^15.1.2",
"vision": "^5.2.0"
}
}
33 changes: 12 additions & 21 deletions test/all-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,29 @@ lab.experiment('Rapptor#all', () => {
cwd: __dirname
});

lab.before((done) => {
rapptor.start((err) => {
if (err) {
throw err;
}
done();
});
lab.before(async() => {
await rapptor.start();
});

lab.after((done) => {
rapptor.stop(done);
lab.after(async() => {
await rapptor.stop();
});

lab.test('should use custom helper correctly', (done) => {
rapptor.server.inject({
lab.test('should use custom helper correctly', async() => {
const response = await rapptor.server.inject({
method: 'GET',
url: '/helper-test'
}, (response) => {
Code.expect(response.statusCode).to.equal(200);
Code.expect(response.result).to.include('bob:dave:ralph');
done();
});
Code.expect(response.statusCode).to.equal(200);
Code.expect(response.result).to.include('bob:dave:ralph');
});

lab.test('should use the all helper correctly', (done) => {
rapptor.server.inject({
lab.test('should use the all helper correctly', async() => {
const response = await rapptor.server.inject({
method: 'GET',
url: '/all-test'
}, (response) => {
Code.expect(response.statusCode).to.equal(200);
Code.expect(response.result).to.include('<p>true</p>');
done();
});
Code.expect(response.statusCode).to.equal(200);
Code.expect(response.result).to.include('<p>true</p>');
});
});
2 changes: 1 addition & 1 deletion test/conf/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ plugins:
hapi-logr:
_priority: -5
clientErrorsToWarnings: true
requests: '{{!!ENV.ACCESS_LOGS || false}}'
requests: '{{ENV.ACCESS_LOGS === "true" || ENV.ACCESS_LOGS === true }}'
reporters:
flat:
reporter: 'logr-flat'
Expand Down
3 changes: 2 additions & 1 deletion test/conf2/default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
verbose: false
connection:
port: '8080'
testValue: 'conf2!'
Expand All @@ -11,7 +12,7 @@ plugins:
vision:
inert:
hapi-method-loader:
_priority: 2
_priority: -2
cwd: '{{CWD}}/test'
path: '{{CWD}}/test/methods'
verbose: '{{verbose}}'
Expand Down
12 changes: 5 additions & 7 deletions test/initialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ const Lab = require('lab');
const lab = exports.lab = Lab.script();

lab.experiment('Rapptor#initialization', { timeout: 5000 }, () => {
lab.test('initializes a new instance of rapptor', (done) => {
lab.test('initializes a new instance of rapptor', async() => {
const rapptor = new Rapptor({
cwd: __dirname,
});
Code.expect(typeof rapptor).to.equal('object');
Code.expect(typeof rapptor.start).to.equal('function');
rapptor.start((err, server, config) => {
Code.expect(err).to.equal(undefined);
Code.expect(typeof server).to.equal('object');
Code.expect(typeof config).to.equal('object');
rapptor.stop(done);
});
const { server, config } = await rapptor.start();
Code.expect(typeof server).to.equal('object');
Code.expect(typeof config).to.equal('object');
await rapptor.stop();
});
});
22 changes: 7 additions & 15 deletions test/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,18 @@ lab.experiment('Rapptor#routes', () => {
configPath: `${__dirname}/conf`
});

lab.before((done) => {
rapptor.start((err, server) => {
Code.expect(err).to.equal(undefined);
done();
});
lab.before(async() => {
const { server, config } = await rapptor.start();
});

lab.test('should automatically load methods from the appropriate folder', (done) => {
lab.test('should automatically load methods from the appropriate folder', async() => {
const server = rapptor.server;
Code.expect(typeof server.methods.randomNumber).to.equal('function');
server.methods.randomNumber((err, value) => {
Code.expect(err).to.equal(null);
Code.expect(typeof value).to.equal('number');
});
done();
const value = await server.methods.randomNumber();
Code.expect(typeof value).to.equal('number');
});

lab.after((done) => {
rapptor.stop(() => {
done();
});
lab.after(async () => {
await rapptor.stop();
});
});
8 changes: 4 additions & 4 deletions test/methods/random-number.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
module.exports = {
method: (done) => {
setTimeout(() => {
done(null, Math.random());
}, 500);
method: async() => {
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));
wait(500);
return Math.random();
}
};
Loading

0 comments on commit 1830e90

Please sign in to comment.