Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
fix(fastify): fixes fastify app loading
Browse files Browse the repository at this point in the history
  • Loading branch information
fox1t committed Sep 28, 2019
1 parent fe3ecae commit c4c1bcb
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 83 deletions.
30 changes: 0 additions & 30 deletions app.ts

This file was deleted.

12 changes: 6 additions & 6 deletions azure-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ trigger:
- master

jobs:
- template: build/azure-pipelines-npm.yml
- template: azure/azure-pipelines-npm.yml
parameters:
name: Linux_npm
vmImage: ubuntu-16.04
- template: build/azure-pipelines-npm.yml
- template: azure/azure-pipelines-npm.yml
parameters:
name: Windows_npm
vmImage: vs2017-win2016
- template: build/azure-pipelines-npm.yml
- template: azure/azure-pipelines-npm.yml
parameters:
name: macOs_npm
vmImage: macOS-10.14
- template: build/azure-pipelines-yarn.yml
- template: azure/azure-pipelines-yarn.yml
parameters:
name: Linux_yarn
vmImage: ubuntu-16.04
- template: build/azure-pipelines-yarn.yml
- template: azure/azure-pipelines-yarn.yml
parameters:
name: Windows_yarn
vmImage: vs2017-win2016
- template: build/azure-pipelines-yarn.yml
- template: azure/azure-pipelines-yarn.yml
parameters:
name: macOS_yarn
vmImage: macOS-10.14
File renamed without changes.
File renamed without changes.
44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"predev": "npm run lint",
"dev": "cross-env NODE_ENV=development nodemon -e ts -w \"./src\" -x \"npm run debug\"",
"debug": "node -r ts-node/register -r dotenv/config --inspect ./src/index.ts | pino-colada",
"update": "npx npm-check -u",
"update": "yarn upgrade-interactive --latest",
"prelint": "npm run prettier",
"lint": "eslint --fix \"./src/**/*.ts\"",
"prettier": "prettier --loglevel warn --write \"./**/*.{ts,tsx}\"",
Expand All @@ -28,15 +28,15 @@
"author": "Maksim Sinik <[email protected]>",
"license": "MIT",
"dependencies": {
"cross-env": "~5.2.1",
"cross-env": "^6.0.0",
"fastify": "^2.0.0",
"fastify-autoload": "^1.0.0",
"fastify-blipp": "~2.1.0",
"fastify-blipp": "^2.1.0",
"fastify-cli": "^1.2.0",
"fastify-cors": "~2.1.3",
"fastify-helmet": "~3.0.1",
"fastify-no-icon": "~3.0.0",
"qs": "~6.8.0"
"fastify-cors": "^2.1.3",
"fastify-helmet": "^3.0.1",
"fastify-no-icon": "^3.0.0",
"qs": "^6.9.0"
},
"devDependencies": {
"@commitlint/cli": "^8.2.0",
Expand All @@ -48,27 +48,27 @@
"@semantic-release/github": "^5.4.3",
"@semantic-release/release-notes-generator": "^7.3.0",
"@types/node": "^12.7.4",
"@types/qs": "~6.5.3",
"@typescript-eslint/eslint-plugin": "2.2.0",
"@typescript-eslint/parser": "2.2.0",
"@types/qs": "^6.5.3",
"@typescript-eslint/eslint-plugin": "2.3.1",
"@typescript-eslint/parser": "2.3.1",
"commitizen": "^4.0.3",
"commitlint-config-cz": "^0.12.1",
"cz-conventional-changelog": "^3.0.2",
"dotenv": "~8.1.0",
"eslint": "~6.3.0",
"eslint-config-prettier": "~6.2.0",
"eslint-plugin-prettier": "~3.1.0",
"fastify-plugin": "~1.6.0",
"dotenv": "^8.1.0",
"eslint": "^6.4.0",
"eslint-config-prettier": "^6.3.0",
"eslint-plugin-prettier": "^3.1.0",
"fastify-plugin": "^1.6.0",
"husky": "^3.0.5",
"nodemon": "~1.19.2",
"pino-colada": "~1.4.5",
"prettier": "~1.18.2",
"rimraf": "~3.0.0",
"nodemon": "^1.19.2",
"pino-colada": "^1.4.5",
"prettier": "^1.18.2",
"rimraf": "^3.0.0",
"semantic-release": "^15.13.24",
"tap": "^14.6.1",
"tap-mocha-reporter": "~4.0.1",
"tap-mocha-reporter": "^4.0.1",
"ts-node": "^8.3.0",
"typescript": "^3.6.2"
"typescript": "~3.6.2"
},
"config": {
"commitizen": {
Expand All @@ -78,7 +78,7 @@
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "npm test"
"pre-commit": "npm run lint && npm test"
}
}
}
20 changes: 19 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import { join } from 'path'
import AutoLoad from 'fastify-autoload'
import { FastifyInstance } from 'fastify'
import { nextCallback } from 'fastify-plugin'
import noIcon from 'fastify-no-icon'
import helmet from 'fastify-helmet'
import qs from 'qs'
import cors from 'fastify-cors'

function App(fastify: FastifyInstance, opts: {}, next: nextCallback) {
fastify.register(cors, {
allowedHeaders: ['Content-Type', 'Authorization'],
})
fastify.register(helmet)
fastify.register(noIcon)

export = function(fastify: FastifyInstance, opts: {}, next: nextCallback) {
// This loads all application wide plugins defined in plugins folder
fastify.register(AutoLoad, {
dir: join(__dirname, 'plugins'),
Expand All @@ -20,3 +30,11 @@ export = function(fastify: FastifyInstance, opts: {}, next: nextCallback) {

next()
}

App.options = {
querystringParser: (str: string) => qs.parse(str),
logger: true,
ignoreTrailingSlash: true,
}

export = App
29 changes: 5 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
// we need this file because of this issue: https://github.com/fastify/fastify-cli/issues/131
import 'make-promises-safe'
import blipp from 'fastify-blipp'

import Fastify from 'fastify'
import cors from 'fastify-cors'
import noIcon from 'fastify-no-icon'
import helmet from 'fastify-helmet'
import qs from 'qs'

import app from './app'

console.time('Boot Time')
const fastify = Fastify({
querystringParser: str => qs.parse(str),
logger: true,
ignoreTrailingSlash: true,
})

fastify.register(cors, {
allowedHeaders: ['Content-Type', 'Authorization'],
})

if (process.env.NODE_ENV !== 'production') {
const blipp = require('fastify-blipp') // eslint-disable-line
fastify.register(blipp)
}
const fastify = Fastify(app.options)

fastify.register(helmet)

fastify.register(noIcon)
fastify.register(blipp)
fastify.register(app)

fastify.listen((process.env.PORT as any) || 3000, '0.0.0.0', err => {
if (err) {
console.log(err)
process.exit(1)
}
if (process.env.NODE_ENV !== 'production') {
fastify.blipp()
}
fastify.blipp()

console.timeEnd('Boot Time')
console.log(`Server listening on port ${(fastify.server as any).address().port}`)
})

0 comments on commit c4c1bcb

Please sign in to comment.