From cd11486bef5b013e3df331f533265398a9b5355a Mon Sep 17 00:00:00 2001 From: delvedor Date: Thu, 12 Jan 2017 09:22:29 +0100 Subject: [PATCH 1/4] Updated README --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 94fd2a90..cf577b5c 100644 --- a/README.md +++ b/README.md @@ -37,10 +37,31 @@ Usage: fastify [opts] -P, --pretty-logs Prints pretty logs + -o, --options + Use custom options + -h, --help Show this help message ``` +If you want to use custom options, just export an options object with your route and run the cli command with the `--options` flag. +```js +// plugin.js +module.exports = function (fastify, options, next) { + fastify.get('/', function (req, reply) { + reply.send({ hello: 'world' }) + }) + next() +} + +module.exports.options = { + https: { + key: 'key', + cert: 'cert' + } +} +``` + ## Contributing If you feel you can help in any way, be it with examples, extra testing, or new features please open a pull request or open an issue. From 9dde969b70c17adb1791f523d7717d3da6eb1e32 Mon Sep 17 00:00:00 2001 From: delvedor Date: Thu, 12 Jan 2017 09:22:41 +0100 Subject: [PATCH 2/4] Options support --- cli.js | 5 +++-- help.txt | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 4f8431c9..03a32a3b 100755 --- a/cli.js +++ b/cli.js @@ -53,7 +53,7 @@ function runFastify (opts) { options.logger.stream = pretty } - const fastify = Fastify(options) + const fastify = Fastify(opts.options ? Object.assign(options, file.options) : options) fastify.register(file, function (err) { if (err) { @@ -74,11 +74,12 @@ function runFastify (opts) { if (require.main === module) { start(minimist(process.argv.slice(2), { integer: ['port'], - boolean: ['pretty-logs'], + boolean: ['pretty-logs', 'options'], string: ['log-level'], alias: { port: 'p', help: 'h', + options: 'o', 'log-level': 'l', 'pretty-logs': 'P' }, diff --git a/help.txt b/help.txt index 41519a8a..a6354ad7 100644 --- a/help.txt +++ b/help.txt @@ -8,5 +8,8 @@ Usage: fastify [opts] -P, --pretty-logs Prints pretty logs + -o, --options + Use custom options + -h, --help Show this help message From 7cc82a3782a731791a850c9cd9c6ab81e4187a19 Mon Sep 17 00:00:00 2001 From: delvedor Date: Thu, 12 Jan 2017 09:23:06 +0100 Subject: [PATCH 3/4] Added examples folder --- examples/plugin-with-options.js | 15 +++++++++++++++ plugin.js => examples/plugin.js | 2 ++ 2 files changed, 17 insertions(+) create mode 100644 examples/plugin-with-options.js rename plugin.js => examples/plugin.js (91%) diff --git a/examples/plugin-with-options.js b/examples/plugin-with-options.js new file mode 100644 index 00000000..75edbb1b --- /dev/null +++ b/examples/plugin-with-options.js @@ -0,0 +1,15 @@ +'use strict' + +module.exports = function (fastify, options, next) { + fastify.get('/', function (req, reply) { + reply.send({ hello: 'world' }) + }) + next() +} + +module.exports.options = { + https: { + key: 'key', + cert: 'cert' + } +} diff --git a/plugin.js b/examples/plugin.js similarity index 91% rename from plugin.js rename to examples/plugin.js index fec3233f..e521d97b 100644 --- a/plugin.js +++ b/examples/plugin.js @@ -1,3 +1,5 @@ +'use strict' + module.exports = function (fastify, options, next) { fastify.get('/', function (req, reply) { reply.send({ hello: 'world' }) From dd2907254cfe6d3585aa23cb5865662c1e7f1995 Mon Sep 17 00:00:00 2001 From: delvedor Date: Thu, 12 Jan 2017 09:23:16 +0100 Subject: [PATCH 4/4] Updated test --- test.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/test.js b/test.js index 744b2ca2..9c77d981 100644 --- a/test.js +++ b/test.js @@ -10,7 +10,7 @@ test('should start the server', t => { cli.start({ port: 3000, - _: ['./plugin.js'] + _: ['./examples/plugin.js'] }) request({ @@ -24,3 +24,19 @@ test('should start the server', t => { cli.stop(0) }) }) + +test('should start fastify with custom options', t => { + t.plan(1) + // here the test should fail because of the wrong certificate + // or because the server is booted without the custom options + try { + cli.start({ + port: 3000, + options: true, + _: ['./examples/plugin-with-options.js'] + }) + t.fail('Custom options') + } catch (e) { + t.pass('Custom options') + } +})