Chapter 11: update example microservice
This commit is contained in:
@@ -56,7 +56,3 @@ profile-*
|
|||||||
profile*
|
profile*
|
||||||
*clinic*
|
*clinic*
|
||||||
*flamegraph*
|
*flamegraph*
|
||||||
|
|
||||||
# generated code
|
|
||||||
*/**/*.js
|
|
||||||
dist
|
|
||||||
|
|||||||
@@ -16,11 +16,11 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"fastify": "^3.0.0",
|
"fastify": "^3.0.0",
|
||||||
"fastify-plugin": "^2.0.0",
|
"fastify-plugin": "^3.0.0",
|
||||||
"fastify-autoload": "^3.0.2",
|
"fastify-autoload": "^3.0.2",
|
||||||
"fastify-cli": "^2.0.2"
|
"fastify-cli": "^2.5.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"tap": "^14.0.0"
|
"tap": "^14.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,6 @@ that will then be used in the rest of your application.
|
|||||||
|
|
||||||
Check out:
|
Check out:
|
||||||
|
|
||||||
* [The hitchhiker's guide to plugins](https://github.com/fastify/fastify/blob/master/docs/Plugins-Guide.md)
|
* [The hitchhiker's guide to plugins](https://www.fastify.io/docs/latest/Plugins-Guide/)
|
||||||
* [Fastify decorators](https://www.fastify.io/docs/latest/Decorators/).
|
* [Fastify decorators](https://www.fastify.io/docs/latest/Decorators/).
|
||||||
* [Fastify lifecycle](https://www.fastify.io/docs/latest/Lifecycle/).
|
* [Fastify lifecycle](https://www.fastify.io/docs/latest/Lifecycle/).
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const fp = require('fastify-plugin')
|
||||||
|
|
||||||
|
// the use of fastify-plugin is required to be able
|
||||||
|
// to export the decorators to the outer scope
|
||||||
|
|
||||||
|
module.exports = fp(async function (fastify, opts) {
|
||||||
|
fastify.decorate('someSupport', function () {
|
||||||
|
return 'hugs'
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -23,4 +23,5 @@ If you need to share functionality between services, place that
|
|||||||
functionality into the `plugins` folder, and share it via
|
functionality into the `plugins` folder, and share it via
|
||||||
[decorators](https://www.fastify.io/docs/latest/Decorators/).
|
[decorators](https://www.fastify.io/docs/latest/Decorators/).
|
||||||
|
|
||||||
If you have a bit confused about using `async/await` write services, you would better take a look at [Promise resolution](https://github.com/fastify/fastify/blob/master/docs/Routes.md#promise-resolution) for more details.
|
If you're a bit confused about using `async/await` to write services, you would
|
||||||
|
better take a look at [Promise resolution](https://www.fastify.io/docs/latest/Routes/#promise-resolution) for more details.
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = async function (fastify, opts) {
|
||||||
|
fastify.get('/', async function (request, reply) {
|
||||||
|
return 'this is an example'
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = async function (fastify, opts) {
|
||||||
|
fastify.get('/', async function (request, reply) {
|
||||||
|
return { root: true }
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
// This file contains code that we reuse
|
||||||
|
// between our tests.
|
||||||
|
|
||||||
|
const Fastify = require('fastify')
|
||||||
|
const fp = require('fastify-plugin')
|
||||||
|
const App = require('../app')
|
||||||
|
|
||||||
|
// Fill in this config with all the configurations
|
||||||
|
// needed for testing the application
|
||||||
|
function config () {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// automatically build and tear down our instance
|
||||||
|
function build (t) {
|
||||||
|
const app = Fastify()
|
||||||
|
|
||||||
|
// fastify-plugin ensures that all decorators
|
||||||
|
// are exposed for testing purposes, this is
|
||||||
|
// different from the production setup
|
||||||
|
app.register(fp(App), config())
|
||||||
|
|
||||||
|
// tear down our app after we are done
|
||||||
|
t.tearDown(app.close.bind(app))
|
||||||
|
|
||||||
|
return app
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
config,
|
||||||
|
build
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const { test } = require('tap')
|
||||||
|
const Fastify = require('fastify')
|
||||||
|
const Support = require('../../plugins/support')
|
||||||
|
|
||||||
|
test('support works standalone', async (t) => {
|
||||||
|
const fastify = Fastify()
|
||||||
|
fastify.register(Support)
|
||||||
|
|
||||||
|
await fastify.ready()
|
||||||
|
t.equal(fastify.someSupport(), 'hugs')
|
||||||
|
})
|
||||||
|
|
||||||
|
// You can also use plugin with opts in fastify v2
|
||||||
|
//
|
||||||
|
// test('support works standalone', (t) => {
|
||||||
|
// t.plan(2)
|
||||||
|
// const fastify = Fastify()
|
||||||
|
// fastify.register(Support)
|
||||||
|
//
|
||||||
|
// fastify.ready((err) => {
|
||||||
|
// t.error(err)
|
||||||
|
// t.equal(fastify.someSupport(), 'hugs')
|
||||||
|
// })
|
||||||
|
// })
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const { test } = require('tap')
|
||||||
|
const { build } = require('../helper')
|
||||||
|
|
||||||
|
test('example is loaded', async (t) => {
|
||||||
|
const app = build(t)
|
||||||
|
|
||||||
|
const res = await app.inject({
|
||||||
|
url: '/example'
|
||||||
|
})
|
||||||
|
t.equal(res.payload, 'this is an example')
|
||||||
|
})
|
||||||
|
|
||||||
|
// inject callback style:
|
||||||
|
//
|
||||||
|
// test('example is loaded', (t) => {
|
||||||
|
// t.plan(2)
|
||||||
|
// const app = build(t)
|
||||||
|
//
|
||||||
|
// app.inject({
|
||||||
|
// url: '/example'
|
||||||
|
// }, (err, res) => {
|
||||||
|
// t.error(err)
|
||||||
|
// t.equal(res.payload, 'this is an example')
|
||||||
|
// })
|
||||||
|
// })
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const { test } = require('tap')
|
||||||
|
const { build } = require('../helper')
|
||||||
|
|
||||||
|
test('default root route', async (t) => {
|
||||||
|
const app = build(t)
|
||||||
|
|
||||||
|
const res = await app.inject({
|
||||||
|
url: '/'
|
||||||
|
})
|
||||||
|
t.deepEqual(JSON.parse(res.payload), { root: true })
|
||||||
|
})
|
||||||
|
|
||||||
|
// inject callback style:
|
||||||
|
//
|
||||||
|
// test('default root route', (t) => {
|
||||||
|
// t.plan(2)
|
||||||
|
// const app = build(t)
|
||||||
|
//
|
||||||
|
// app.inject({
|
||||||
|
// url: '/'
|
||||||
|
// }, (err, res) => {
|
||||||
|
// t.error(err)
|
||||||
|
// t.deepEqual(JSON.parse(res.payload), { root: true })
|
||||||
|
// })
|
||||||
|
// })
|
||||||
Reference in New Issue
Block a user