Opinionated feature pack (of packages) to bootstrap a fastify app
- decorates your app with
name
,version
androot
-path read from package.json - decorates your app with any given
config
- provides OpenAPI 3.0 docs by use of @fastify/swagger
- enables extra security headers as provided by helmet @fastify/helmet
- provides extra sensible defaults provided by @fastify/sensible
- uses @fastify/autoload to load plugins, models, services, whatever from configurable directories
- provides monitoring healthcheck endpoint by use of under-pressure
All those features are ready setup with defaults, that may be customized further to your likings.
Easy start by yarn create
cli command to create a new fastify-app from scratch instead of manually setting up a new fastify instance, like so:
$ yarn create @uscreen.de/fastify-app new-app
and follow instructions. It will create a directory called new-app
inside the current folder.
Inside that directory, it will generate the initial project structure and install any dependencies:
new-app
├── Makefile
├── README.md
├── app
│ ├── app.js
│ ├── config.js
│ ├── plugins
│ │ └── noop.js
│ ├── schemas.js
│ ├── server.js
│ └── services
│ └── noop.js
├── package.json
├── pm2-dev.config.js
├── pm2.config.js
├── test
│ ├── helper.js
│ └── noop.test.js
└── yarn.lock
$ yarn add @uscreen.de/fastify-app # or use npm -i
Minimal example:
import defaultApp from '@uscreen.de/fastify-app'
// register with defaults
fastify.register(defaultApp)
With default server options for logging, etc.
import Fastify from 'fastify'
import defaultApp, { options } from '@uscreen.de/fastify-app'
// create fastify instance with default options
const fastify = Fastify(options())
// register with defaults
fastify.register(defaultApp)
This enables all default features and exposes some extra routes, like:
GET /documentation
- autogenerated OpenAPI 3.0 documentation of all endpointGET /status
- healthcheck endpoint return HTTP 200{"status":"ok"}
All options get validated and defaulted to a defined json-schema you can check in config.js Overview of options:
option | Description | Default | Example |
---|---|---|---|
autoloads | array of directories @fastify/autoload should load your fastify plugins from. Please consider reading Loading order of your plugins | [] |
['./plugins', './services'] |
swagger | object configuring @fastify/swagger to generate Swagger2/OpenAPI3 docs from your routes | {exposeRoute: true, openapi:{}} |
{exposeRoute: '/docs'} |
health (alias: healthCheck ) |
object configuring under-pressure to provide a monitoring healthcheck route for your app | {exposeStatusRoute: true} |
{exposeStatusRoute: '/health'} |
contentSecurityPolicy | object configuring helmet to set CSR headers on each response | {contentSecurityPolicy: false} |
{contentSecurityPolicy: {directives: {defaultSrc: ["'self'"]}}} |
decorate your healthCheck
option with a custom function returning truthy on success, ie.:
import fastifyApp from '@uscreen.de/fastify-app'
import fp from 'fastify-plugin'
import schemas from './schemas.js'
export default fp(async (fastify, opts, next) => {
/**
* add schemas
*/
fastify.register(schemas)
/**
* configure healthcheck
*/
opts.healthCheck = {
...opts.healthCheck,
healthCheck: async () => {
/**
* check for proper mongo conenction
*/
const collections = await fastify.mongo.db.collections()
/**
* check for proper nats connection
*/
const natsConnected = await fastify.nats.testConnection()
/**
* check for proper redis connection
*/
const redisConnected = await fastify.redis.ping()
/**
* true if all tests passed
*/
return collections && natsConnected && redisConnected && true
}
}
/**
* register app
*/
fastify.register(fastifyApp, opts)
next()
})
- TBD
- upgrade to fastify@5.x
- make
options
method pass throughajv
property.
- switch to ESM only
- upgrade to fastify@4.x
- a server options factory providing defaults for logging and generateId options([config])
- replaced fastify-[module] with their scoped versions (@fastify/autoload, @fastify/helmet, @fastify/sensible, @fastify/swagger)
- fixed deprecation warning
- fixed swagger /docs routes
- upgrade all deps including major versions, like fastify-helmet@4.x
- replace fastify-swagger with fastify-swagger
- upgrade node LTS (16)
- upgraded all deps including major versions, like fastify-plugin@3.x
- upgrade node LTS (14)
- upgraded all deps including major versions, like fastify@3.6.x, env-schem@2.0.0
- uses shared config @uscreen.de/eslint-config-prettystandard-node
- fastify 3.6.x decorates
fastify.version
which broke
- decorate
fastify.app.version
,fastify.app.name
, andfastify.app.root
- skip decoration of any
fastify.<decorator>
if already declared
- upgraded to fastify 3.x with backward compatible defaults
- added alias of
health
<->healthCheck
- moved cli to separate package
@uscreen.de/create-fastify-app
- added cli
- added tests (100% coverage)
- project setup, linting, guidlines
Licensed under MIT.
Published, Supported and Sponsored by u|screen