Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,31 @@ Usage: fastify [opts] <file>
-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.

Expand Down
5 changes: 3 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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'
},
Expand Down
15 changes: 15 additions & 0 deletions examples/plugin-with-options.js
Original file line number Diff line number Diff line change
@@ -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'
}
}
2 changes: 2 additions & 0 deletions plugin.js → examples/plugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

module.exports = function (fastify, options, next) {
fastify.get('/', function (req, reply) {
reply.send({ hello: 'world' })
Expand Down
3 changes: 3 additions & 0 deletions help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ Usage: fastify [opts] <file>
-P, --pretty-logs
Prints pretty logs

-o, --options
Use custom options

-h, --help
Show this help message
18 changes: 17 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('should start the server', t => {

cli.start({
port: 3000,
_: ['./plugin.js']
_: ['./examples/plugin.js']
})

request({
Expand All @@ -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')
}
})